Home | | Computer Science 11th std | C++ if -else-if ladder

Flow of Control | C++ - C++ if -else-if ladder | 11th Computer Science : Chapter 10 : Flow of Control

Chapter: 11th Computer Science : Chapter 10 : Flow of Control

C++ if -else-if ladder

The if-else ladder is a multi-path decision making statement.

if -else-if ladder

 

The if-else ladder is a multi-path decision making statement. In this type of statement 'if' is followed by one or more else if statements and finally end with an else statement.

The syntax of if-else ladder:

if (expression 1)

{

      Statemet-1

}

else

      if( expression 2)

      {

           Statemet-2

      }

      else

           if ( expression 3)

           {

           Statemet-3

           }

           else

           {

                   Statement-4

           }

When the respective expression becomes true, the statement associated with block is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.


 

Illustration 10.6 C++ program to find your grade using if-else ladder.

#include <iostream>

using namespace std;

int main ()

{

int marks;

cout<<" Enter the Marks :";

cin>>marks;

if( marks >= 60 )

      cout<< "Your grade is 1st class !!" <<endl;

           else if( marks >= 50 && marks < 60)

                   cout<< "your grade is 2nd class !!" <<endl;

                             else if( marks >= 40 && marks < 50)

                                      cout<< "your grade is 3rd class !!" <<endl;

else

      cout<< "You are fail !!" <<endl;

return 0;

}

Output

Enter the Marks :60

Your grade is 1st class !!

When the marks are greater than or equal to 60, the message "Your grade is 1st class !!" is displayed and the rest of the ladder is bypassed. When the marks are between 50 and 59, the message "Your grade is 2nd class !!" is displayed, and the other ladder is bypassed. When the mark between 40 to 49, the message "Your grade is 3nd class !!" is displayed, otherwise, the message "You are fail !!" is displayed.

 


Tags : Flow of Control | C++ , 11th Computer Science : Chapter 10 : Flow of Control
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 10 : Flow of Control : C++ if -else-if ladder | Flow of Control | C++


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.