The Wayback Machine - https://web.archive.org/web/20241227220301/https://www.geeksforgeeks.org/c-c-if-else-if-ladder-with-examples/
Open In App

C++ if else if Ladder

Last Updated : 12 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In C++, the if-else-if ladder helps the user decide from among multiple options. The 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 statement will be executed.

Let’s take a look at an example:

C++
#include <iostream> 
using namespace std; 
  
int main()  { 
    int i = 20;
  
    // If - else ladder
    if (i == 10) 
        cout << "i is 10"; 
    else if (i == 15) 
        cout << "i is 15"; 
    else if (i == 20) 
        cout << "i is 20"; 
    else
        cout << "i is not present"; 
  
    return 0; 
} 

Output
i is 20

Explanation: In this program, compiler first checks if i is 10. Since i is not 10, it then moves to the next condition and checks if i is 15. Since i is also not 15, it checks if i is 20. Here the condition becomes true and the associated block is executed.

Syntax if-else-if Ladder

if (condition1) {
// First set of statements
}
else if (condition2) {
// Second set of statements
}
else {
// Last set of statements
}

An if-else-if ladder can exclude else block.

Working of the if-else-if Ladder

  1. Control falls into the if block.
  2. The flow jumps to Condition 1.
  3. Condition is tested.
    1. If Condition yields true, goto Step 4.
    2. If Condition yields false, goto Step 5.
  4. The present block is executed. Goto Step 7.
  5. The flow jumps to Condition 2.
    1. If Condition yields true, goto step 4.
    2. If Condition yields false, goto Step 6.
  6. The flow jumps to Condition 3.
    1. If Condition yields true, goto step 4.
    2. If Condition yields false, execute else block. Goto Step 7.
  7. Exits the if-else-if ladder.

Flowchart if-else-if Ladder

if-else-if-ladder

Examples of if else if the ladder

The below examples demonstrate the common usage of if else if ladder in C++ programs:

Check whether a number is positive, negative or 0

C++
#include <iostream>
using namespace std;

int main() {
    int n = -8;

    // Check if the number is positive
    if (n > 0)
        cout << "Positive";
  
    // Check if the number is negative
    else if (n < 0)
        cout << "Negative";
  
    // If the number is neither positive nor negative
    else
        cout << "Zero";

    return 0;
}

Output
Negative

As you may also have noticed, if the body contains only single statement, we can skip the braces {}.

Print the day name using day number

C++
#include <iostream>
using namespace std;

int main() {
    int day = 4;

    // Determine the day name using if-else if ladder
    if (day == 1) {
        cout << "Monday";
    } else if (day == 2) {
        cout << "Tuesday";
    } else if (day == 3) {
        cout << "Wednesday";
    } else if (day == 4) {
        cout << "Thursday";
    } else if (day == 5) {
        cout << "Friday";
    } else if (day == 6) {
        cout << "Saturday";
    } else if (day == 7) {
        cout << "Sunday";
    } else {
        cout << "Invalid day number";
    }

    return 0;
}

Output
Thursday


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg