Loading....
Coupon Accepted Successfully!

 

Selection Statement

  • As selection statement is explained in detail in the previous section. Let us learn their types in detail.
  • There are many types of C++ selection statements.
  • They are
    • if statement
    • if __ else statement
    • nested if statement
    • switch statement
Note: Selection statement is also called as conditional statement.

Simple if Statement

  • This statement is used to execute or skip one statement or group of statements for particular conditions.
  • The general format is given in the following table.
if(test condition)

{

Statement block;

}

next statement;

  • When this statement is executed, the computer first evaluates the value of the test condition.
  • It the value is true the statement block and next statement are executed.
  • If the value is false, statement block is skipped and execution starts from next statement.
  • The block diagram of if statement
Program 1: A program to check the grade of a student.
#include<iostream.h>

void main()

{

int mark;

char grade;

cin >> mark >> grade;

if (grade=='A')

{

mark=mark +10;

}

cout << mark;

}

if .........else Statement

  • This statement is used to execute one group of statements if the test condition is true or other groups if the test condition is false.

  • The general form is

if(test condition)

{

Statement block-1;

}

else

{

Statement block-2;

}

next statement;

  • The block diagram of if—else is given below

  • Program 2: To check the status of a student in his examination
    #include<iostream.h>

    void main()

    {

    int mark;

    cin >> mark ;

    if (mark > 35)

    cout << "Pass";

    else

    cout <<" Fail";

    }

    Nested if statement

    • A nested if is an if that has another if in its body statement.

    • The general format is

    if (expression 1)

    {

    if (expression 2)

    statement 1;

    else

    statement 2;

    }

    else

    statement 3;

    }

    • The nested if has the following block diagram

    Program 3: to feed a character and to print whether a given character is an alphabet.

     

    #include<iostream.h>

    void main()

    {

    char a;

    cin >> a;

    if (((a > 'A' ) && (a <= 'Z')) || ((a >='a') && (a <= 'z')))

    cout << " the alphabet is "<<"\n";

    else

    if (a>='0' && a <='9')

    cout << the given input is a number";

    else

    cout << "the character is not an alphabet or a number";

    }

    Switch Case Statement

    • Switch case statement is the extension of if....else statement.
    • This permits any number of branches.
    • The general form is

    switch (expression)

    {

    case label1:

    statement1;

    break;

    case label2:

    statement1;

    break;

    case label3:

    statement1;

    break;

    default:

    default statement;

    break;

    }

    next statement;

    • When this statement is executed the computer first executes the value of the expression in the keyword switch.
    • The value is successively compared with each case.
    • If a case label matches with the value, the statement block associated with the case label is executed.
    • Then the control is transferred to the next statement.
    • If none of the case value matches with the value, the default statement is executed.
    • The block diagram

  • The following example shows the work of switch case statement.
  • Program 4: To display the day depending upon the numbered entered.
    #include<iostream.h>

    void main()

    {

    int day;

    cout<< "enter the number between 1 and 7";

    cin>> day;

    switch(day)

    {

    case 1:

    cout "Monday";

    break;

    case 2:

    cout "Tuesday";

    break;

    case 3:

    cout "Wednesday";

    break;

    case 4:

    cout "Thursday";;

    break;

    case 5:

    cout "Friday";

    break;

    case 6:

    cout "Saturday";

    break;

    case 7:

    cout "Sunday";

    break;

    default:

    cout<< " enter the correct number';

    break;

    }

    }

    Note: Switch case statement may also be nested.




    Test Your Skills Now!
    Take a Quiz now
    Reviewer Name