This statement allows the user to have multiple options to check for different conditions. Here, if one of the if
or else-if
condition is true then that part of the code will be executed and the rest will be skipped. if none of the conditions are true then the final else
statement present at the end will be executed.
The syntax of the if-else-if ladder
statement in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if(condition1) { //code to be executed if condition1 is true } else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //final else if all the above condition are false } |
Flowchart diagram for if-else if statement:

Example of C++ if-else-if ladder statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> using namespace std; int main() { int a = 100; // check condition if (a > 50) { cout << "Value of a less than 10." << endl; } else if (a == 10) { cout << "Value of a is equal to 20." << endl; } else if (a < 30) { cout << "Value of a is greater than 30." << endl; } else { cout << "None of the condition is true" << endl; } cout << "Value of a: " << a << endl; return 0; } |
Output:
1 2 | Value of a less than 10. Value of a: 100 |