Social Icons

Selection

  • one way statement 

    one way statement usually used in the program which is there have only one choice. in other word, "take it or leave it".
    e.g:
    "write a C++ program that will display a message "pass" if the students mark is more than 50." - In this situation, the output is only execute one statement , and if the user input mark below than 50, no statement are there.

        if(mark>50)
    cout<<"pass";   
  • two way statement 

    two way statement is used when there have two option. 
    e.g:
    "write a C++ program that will display a message "pass" if the student mark is more than 50. Otherwise, display "fail". - in this stituation, there are two option.

    if (mark>50)
    cout<<"pass";
    else if(mark<=50)
    cout <<"fail";

  • multiple way statement 

    But, when we have more than two option, we need to write a multiple way statement..
    e.g:
     " write a C++ program that  will display students grade. A - 80 and above, B - 70 and above, C - 50 and above, D - 40 and above, else, grade is F". - in this situation, we have more than 2 option (5 options).

    if (mark>=80)
    grade = 'A';
    else if(mark>=70)
    grade = 'B';
    else if(mark>=50)
    grade = 'C';
    else if(mark>=40)
    grade = "D';
    else
    grade = 'F';

    cout<<grade;  
  • Nested statement

No comments:

Post a Comment