4.1.3. The else-if Statement, Chapter - 4, Control Structures, HSSC-II (12th Class) Computer Science Key Point Notes

4.1.3. The else-if Statement, Chapter - 4, Control Structures, HSSC-II (12th Class) Computer Science Key Point Notes

12th Class (HSSC-II) Computer SLO Based Key Point Notes

(National Book Foundation - As Federal Textbook Board, Islamabad 
Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards) 

{Contact WhatsApp 03339719149 for Class-XII Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation for preparing/delivering the Lectures}

************************************

👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************

Unit 4: Control Structures


4.1.3. The else-if Statement:

  • The else-if statement is used to choose one block of statements out of many blocks.
  • It is used when there are many options and only one should be selected on the basis of a condition.

General Syntax

if (condition1)
{
        Block of statement 1
}
else if (condition2)
{
        Block of statement 2
}
else if (condition n)
{
        Block of statement n
}
else
{
        Block of statement n
}


Explanation

    • In this structure, if any of the conditions is true, the program executes the statements under that if or else-if.
    • If none of the conditions is true, the program executes the statements under the final else.


    Flow Chart


    Explanation

    • Condition 1 is evaluated.
    • If it is true, the block of statement 1 is executed and control is transferred to the next statement.
    • If condition 1 is false then condition 2 is evaluated. If it is true then the block of statements 2 following condition 2 is executed.
    • In this manner, conditions are evaluated one by one. When any condition is true, the block of the statement following that condition is executed, the rest of the code is skipped and control is transferred to the next statement.
    • If none of the conditions is true then the last block of statements following the keyword else is executed. The else block is optional.
    • If a single statement is to be executed then braces are not required.


    Example 1

    Write a program that inputs the marks of a student and prints his Grade according to the following scheme:

    80 – 100             A
    70 – 79               B
    60 – 69               C
    50 – 59               D
    Below 50             F

    #include <iostream.h>
    #include <conio.h>
    void main()
    {
                clrscr();
                int marks;
                cout<<“Enter your Marks: “;
                cin>>marks;
                if (marks >= 80)
                        cout<<“Your Grade is A\n”;
                else if (marks >= 70)
                        cout<<“Your Grade is B\n”;
                else if (marks >= 60)
                        cout<<“Your Grade is C\n”;
                else if (marks >= 50)
                        cout<<“Your Grade is D\n”;
                else
                        cout<<“Your Grade is F\n”;
                getch();
    }



    Assignment

    • Write a program that inputs two numbers and performs an arithmetic operation on those numbers by entering the choice. i.e.
    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division

    #include <iostream.h>
    #include <conio.h>
    void main()
    {
            clrscr();
            int num1, num2, result;
            int option;
            cout<<“Enter 1st Number”;
            cin>>num1;
            cout<<“Enter 2nd Number”;
            cinn>>num2;
            cout<<“1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n”;
            cout<<“Enter option 1-4: “;
            cin>>option;
            if(option == 1)
                        cout<<“Sum of numbers is: “<<num1+num2;
             else if(option ==2)
                        cout<<“Difference of numbers is: “<<num1-num2;
             else if(option == 3)
                        cout<<“Product of numbers is: “<<num1*num2;
            else if (option == 4)
                        cout<<“Division of numbers is: “<<num1/num2;
             else
                        cout<<“you have entered the wrong option”;
             getch();
    }

    ************************************
    👉👉👉 Computer Science 12th Class Notes (Main Page)

    ************************************


    ************************************
    Shortcut Links For:

    1.  5th Class All Subjects Notes

    2.  8th Class All Subjects Notes

    3.  Easy English Grammar Notes



    1. Website for School and College Level Physics   
    2. Website for School and College Level Mathematics  
    3. Website for Single National Curriculum Pakistan - All Subjects Notes 

    © 2023 & onwards Academic Skills and Knowledge (ASK  

    Note:  Write me in the comments box below for any query and also Share this information with your class-fellows and friends.

    Post a Comment

    0 Comments

    cwebp -q 80 image.png -o image.webp