1. Intro
The else if ladder (also called an if-else if chain) is used when a program must choose among more than two mutually exclusive outcomes based on a series of related conditions. Instead of nesting one if-else inside another, the ladder chains conditions at the same level, one after another, making the logic read top-to-bottom like a list of cases — for example, converting a numeric score into a letter grade.
Cricket analogy: An else if ladder is like classifying a batsman's strike rate into categories — 'Explosive' (>150), else 'Aggressive' (>120), else 'Steady' (>90), else 'Defensive' — checked top-to-bottom instead of nesting one classification inside another.
2. Syntax
if (condition1) {
// runs if condition1 is true
} else if (condition2) {
// runs if condition1 is false AND condition2 is true
} else if (condition3) {
// runs if condition1, condition2 are false AND condition3 is true
} else {
// runs if none of the above conditions are true
}3. Explanation
Conditions are evaluated strictly in order, from top to bottom. As soon as one condition evaluates to true, its corresponding block executes and the rest of the ladder is skipped entirely — even if a later condition would also have been true. If none of the if/else if conditions match, the final else block (if present) runs as the default case. Because evaluation stops at the first match, the order of conditions matters: broader or more general conditions placed earlier can accidentally shadow more specific conditions that follow, so conditions should generally be ordered from most specific to most general.
Cricket analogy: Conditions run strictly top to bottom, like an umpire checking review criteria in order — the moment 'clear edge detected' matches, DRS confirms out and stops checking further criteria; placing a broad rule like 'benefit of doubt to batsman' too early would wrongly shadow a specific 'clear edge' case that comes after, so specific conditions must be ordered first.
4. Example
#include <iostream>
using namespace std;
int main() {
int marks = 82;
if (marks >= 90) {
cout << "Grade: A" << endl;
} else if (marks >= 75) {
cout << "Grade: B" << endl;
} else if (marks >= 60) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}
return 0;
}5. Output
Grade: B6. Key Takeaways
- An
else ifladder chains multiple conditions to model several mutually exclusive outcomes. - Conditions are checked top to bottom; only the first
truecondition's block runs. - Once a match is found, the remaining conditions in the ladder are never evaluated.
- The trailing
elseacts as the default case when no condition in the ladder matches.
Practice what you learned
1. In an `else if` ladder, in what order are the conditions evaluated?
2. What happens once a condition in the ladder evaluates to true?
3. Given the grading example in this lesson, what would `marks = 95` print?
4. Why does the order of conditions matter in an `else if` ladder?
5. What runs if none of the conditions in an `else if` ladder are true and there's a trailing `else`?
Was this page helpful?
You May Also Like
if-else Statement in C++
Understand how the C++ `if-else` statement provides a two-way branch, executing one block when the condition is true and another when false.
switch Statement in C++
Master the C++ `switch` statement — matching an expression against multiple case labels, the role of break, fall-through behavior, and default.
Relational Operators in C++
Understand C++ relational (comparison) operators such as ==, !=, <, >, <=, and >=, and how they always evaluate to a bool result.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics