An else if ladder checks a list of conditions from top to bottom. The first condition that is true runs its block, and every branch after it is skipped. A final else is optional, and it runs only when none of the conditions were true.
if (condition1) {
// runs if condition1 is true
} else if (condition2) {
// runs if condition1 is false and condition2 is true
} else if (condition3) {
// runs if the two above are false and condition3 is true
} else {
// runs if every condition above is false
}
else if is not a separate keyword. It is an if statement placed in the else part of the previous if. The layout is written flat, which is why it reads like a ladder.
A grade example
This program turns a score into a letter grade:
#include <iostream>
int main() {
int score = 84;
if (score >= 90) {
std::cout << "Grade: A\n";
} else if (score >= 80) {
std::cout << "Grade: B\n";
} else if (score >= 70) {
std::cout << "Grade: C\n";
} else if (score >= 60) {
std::cout << "Grade: D\n";
} else {
std::cout << "Grade: F\n";
}
return 0;
}
The output is:
Grade: B
The score of 84 is not 90 or more, so the first test fails. It is 80 or more, so the B block runs. The later tests are never evaluated, even though 84 would also pass them. Each test uses one of the c comparison operators, such as >=, and the others work the same way.
Order matters
Because the first true condition wins, the order of the tests decides the result. This version checks the lowest threshold first:
int score = 84;
if (score >= 60) {
std::cout << "Grade: D\n";
} else if (score >= 80) {
std::cout << "Grade: B\n";
}
The output is Grade: D. The test score >= 60 is true, so its block runs and the later test is skipped. Put the highest threshold first, and each later test only sees scores that failed the earlier ones.
Separate ifs are not a ladder
Writing if in place of else if makes each test independent:
int score = 95;
if (score >= 90) {
std::cout << "Excellent\n";
}
if (score >= 80) {
std::cout << "Good\n";
}
This prints both Excellent and Good. When only one result should run, use else if so the chain stops at the first match.
A ladder without a final else
A ladder does not need a final else. If none of the conditions match, no block runs. That is the right behavior when doing nothing is an acceptable default.
The same structure in C
The if, else if, and else syntax is the same in C. Only the input and output calls change. The grade example in C looks like this:
#include <stdio.h>
int main(void) {
int score = 84;
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}
return 0;
}
It prints Grade: B, the same as the C++ version.
When a switch is a better fit
A long ladder that compares one integer or character with fixed values can be easier to read as a switch case statement in c. A switch compares against exact constant values, so an else if ladder is the better fit for ranges such as 80 to 89 or other conditions.
Before building a ladder, it helps to know the rules for a single if. They are covered in what is the syntax of the if statement.
Key points
- An else if ladder runs the first block whose condition is true, then skips the rest.
- Put the highest threshold first, or an early test can catch values meant for a later branch.
- Use
else if, not a separateif, when only one branch should run. - A final
elseis optional, and the same syntax works in C and C++.