C/C++ if else if ladder with Examples

22 Nov, 2019
Decision Making in C/C++ helps to write decision driven statements and execute a particular set of code based on certain conditions.
In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax:
if (condition)
    statement 1;
else if (condition)
    statement 2;
.
.
else
    statement;
Working of the if-else-if ladder:
Control falls into the if block.The flow jumps to Condition 1.Condition is tested.If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.The present block is executed. Goto Step 7.The flow jumps to Condition 2.If Condition yields true, goto step 4.If Condition yields false, goto Step 6.The flow jumps to Condition 3.If Condition yields true, goto step 4.If Condition yields false, execute else block. Goto Step 7.Exits the if-else-if ladder.
Control falls into the if block.
The flow jumps to Condition 1.
Condition is tested.If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.
If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.
If Condition yields true, goto Step 4.
If Condition yields false, goto Step 5.
The present block is executed. Goto Step 7.
The flow jumps to Condition 2.If Condition yields true, goto step 4.If Condition yields false, goto Step 6.
If Condition yields true, goto step 4.If Condition yields false, goto Step 6.
If Condition yields true, goto step 4.
If Condition yields false, goto Step 6.
The flow jumps to Condition 3.If Condition yields true, goto step 4.If Condition yields false, execute else block. Goto Step 7.
If Condition yields true, goto step 4.If Condition yields false, execute else block. Goto Step 7.
If Condition yields true, goto step 4.
If Condition yields false, execute else block. Goto Step 7.
Exits the if-else-if ladder.
Flowchart if-else-if ladder:
Example 1:
C
C++
// C program to illustrate nested-if statement  #include <stdio.h>  int main(){    int i = 20;      // Check if i is 10    if (i == 10)        printf("i is 10");      // Since i is not 10    // Check if i is 15    else if (i == 15)        printf("i is 15");      // Since i is not 15    // Check if i is 20    else if (i == 20)        printf("i is 20");      // If none of the above conditions is true    // Then execute the else statement    else        printf("i is not present");      return 0;}
// C++ program to illustrate if-else-if ladder#include <iostream>using namespace std;  int main(){    int i = 20;      // Check if i is 10    if (i == 10)        cout << "i is 10";      // Since i is not 10    // Check if i is 15    else if (i == 15)        cout << "i is 15";      // Since i is not 15    // Check if i is 20    else if (i == 20)        cout << "i is 20";      // If none of the above conditions is true    // Then execute the else statement    else        cout << "i is not present";      return 0;}
i is 20

Dry running Example 1:
1. Program starts.
2. i is initialized to 20.
3. condition 1 is checked. 20 == 10, yields false.
4. condition 2 is checked. 20 == 15, yields false.
5. condition 3 is checked. 20 == 20, yields true.
  5.a) "i is 20" gets printed.
6. "Outside if-else-if" gets printed.
7. Program ends.
Example 2:
C
C++
// C program to illustrate nested-if statement  #include <stdio.h>  int main(){    int i = 25;      // Check if i is between 0 and 10    if (i >= 0 && i <= 10)        printf("i is between 0 and 10");      // Since i is not between 0 and 10    // Check if i is between 11 and 15    else if (i >= 11 && i <= 15)        printf("i is between 11 and 15");      // Since i is not between 11 and 15    // Check if i is between 16 and 20    else if (i >= 16 && i <= 20)        printf("i is between 16 and 20");      // Since i is not between 0 and 20    // It means i is greater than 20    else        printf("i is greater than 20");}
// C++ program to illustrate if-else-if ladder  #include <iostream>using namespace std;  int main(){    int i = 25;      // Check if i is between 0 and 10    if (i >= 0 && i <= 10)        cout << "i is between 0 and 10" << endl;      // Since i is not between 0 and 10    // Check if i is between 11 and 15    else if (i >= 11 && i <= 15)        cout << "i is between 11 and 15" << endl;      // Since i is not between 11 and 15    // Check if i is between 16 and 20    else if (i >= 16 && i <= 20)        cout << "i is between 16 and 20" << endl;      // Since i is not between 0 and 20    // It means i is greater than 20    else        cout << "i is greater than 20" << endl;}
i is greater than 20

Related Articles:
Decision Making in C / C++C/C++ if else statement with ExamplesC/C++ if statement with ExamplesSwitch Statement in C/C++Break Statement in C/C++Continue Statement in C/C++goto statement in C/C++return statement in C/C++ with Examples
Decision Making in C / C++
C/C++ if else statement with Examples
C/C++ if statement with Examples
Switch Statement in C/C++
Break Statement in C/C++
Continue Statement in C/C++
goto statement in C/C++
return statement in C/C++ with Examples
C Language
C++
School Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Unordered Sets in C++ Standard Template Library
Operators in C / C++
Exception Handling in C++
What is the purpose of a function prototype?
TCP Server-Client implementation in C
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
Priority Queue in C++ Standard Template Library (STL)