C++ If Else Ladder Statement

  • The if else ladder statement in C++ programming language is used to check set of conditions in sequence.
  • This is useful when we want to selectively executes one code block(out of many) based on certain conditions.
  • It allows us to check for multiple condition expressions and execute different code blocks for more than two conditions.
  • A condition expression is tested only when all previous if conditions in if-else ladder is false.
  • If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else ladder.

Syntax of if else ladder statement

if(condition_expression_One) {
    statement1;// Statements executes when the condition_expression_One is true
} else if (condition_expression_Two) {
    statement2;// Statements executes when the condition_expression_Two is true
} else if (condition_expression_Three) {
    statement3;// Statements executes when the condition_expression_Three is true
} else {
    statement4;// Statements executes when none of the above conditions evaluates to true
}
  • First condition_expression_One is tested and if it is true then following code block will execute and control comes out out of whole if else ladder.
  • If condition_expression_One is false then only condition_expression_Two is tested. Control will keep on flowing downward, If none of the conditional expression is true.
  • The last else is the default block of code which will gets executed if none of the conditional expression is true.
  • If none of the conditional expressions evaluates to true then last else block will executes.
C++ If Else Ladder Statement Control Flow Diagram

C++ If Else Ladder Statement Example Program

#include <iostream>
using namespace std;
 
int main(){
    int score;
     
    cout << "Enter your score between 0-100\n";
    cin >> score;
    /* Using if else ladder statement to print
       Grade of a Student */
    if(score >= 90){
        // Marks between 90-100 
        cout << "YOUR GRADE : A\n";
    } else if (score >= 70 && score < 90){
        // Marks between 70-89 
        cout << "YOUR GRADE : B\n";
    } else if (score >= 50 && score < 70){
        // Marks between 50-69 
        cout << "YOUR GRADE : C\n";
    } else {
        // Marks less than 50 
        // if none of the conditions is true
       cout << "YOUR GRADE : Failed\n";
    }

    return 0;
}

Output
Enter your score between 0-100
72
YOUR GRADE : B
Enter your score between 0-100
10
YOUR GRADE : Failed

Points to Remember about If else ladder statement
  • The condition_expression must be a boolean expression. It must evaluate to true or false value(In C++, all non-zero values are considered as true and zero is considered as false).
  • An if block can have zero or more else if blocks. All else if blocks must be before else block(if any).
  • We may use more than one condition inside if else ladder statement
    For Example:
    if(condition_one && condition_two) {
        /* if code block */
    } else if(condition_three) {
        /* else code block */
    }
  • Default else block is optional in if-else ladder statement.
  • Only of the else-if code block will execute. If none of the else if conditions evaluates to true then default else code block executes.
  • Opening and Closing Braces are optional, If the block of code of if else statement contains only one statement.
    For Example:
    if(condition_expression)
        statement1;
    else if(condition_expression)
        statement2;
    In above example only statement1 will gets executed if condition_expression is true.