Open In App

C++ if else if Ladder

Last Updated : 02 Jan, 2025
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) {
// Statements 1
}
else if (condition2) {
// Statements 2
}
else {
// Else body
}

where,

  • condition1, condition2: Contitions to be tested.
  • statements 1, statements 2: Code block corresponding to each condition.

An if-else ladder can exclude else block.

Working of the if-else-if Ladder

The working of if else if ladder can be understood using the following flowchart:

if-else-if-ladder-flow-chart

Flowchart of if else if Ladder in C++

  • If Condition 1 evaluates to true, Statement 1 is executed, and the rest of the else if and else conditions are skipped.
  • If Condition 1 is false, Condition 2 is evaluated.
  • If Condition 2 evaluates to true, Statement 2 is executed, and the else block is skipped.
  • If Condition 2 is also false, the Else Body is executed.
  • After completing the if-else-if ladder, the statement just below the if are executed.

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
  翻译: