C++ else-if Ladder
In this page:
Basic else-if Ladder
An else-if ladder checks a sequence of conditions from top to bottom and runs only the block belonging to the first one that evaluates to true, skipping every condition after it. This makes it the natural choice whenever you have several mutually exclusive possibilities to test in order, like assigning a letter grade based on a numeric score.
Example: Basic else-if Ladder
#include <iostream>
int main() {
int score = 75;
if (score >= 90) {
std::cout << "A" << std::endl;
} else if (score >= 70) {
std::cout << "B" << std::endl;
} else {
std::cout << "C" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple else-if Blocks
You can chain as many else if blocks as needed after the initial if, which scales well for checking many distinct categories or numeric ranges without deeply nested if statements. Each additional else if only gets evaluated if every condition above it was false.
Example: Multiple else-if Blocks
#include <iostream>
int main() {
int score = 65;
if (score >= 90) {
std::cout << "A" << std::endl;
} else if (score >= 80) {
std::cout << "B" << std::endl;
} else if (score >= 70) {
std::cout << "C" << std::endl;
} else if (score >= 60) {
std::cout << "D" << std::endl;
} else {
std::cout << "F" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Order of Conditions matters
Because the ladder stops at the first true condition, ordering matters enormously — placing a broad condition like score >= 60 before a narrower one like score >= 90 would incorrectly catch high scores in the wrong branch. Always order conditions from most specific to most general when ranges overlap.
Example: Order of Conditions matters
#include <iostream>
int main() {
int score = 95;
if (score >= 60) {
std::cout << "Pass (matches too early)" << std::endl;
} else if (score >= 90) {
std::cout << "A (never reached)" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Ladder vs Single Ifs
A single else-if ladder guarantees at most one block runs, since evaluation stops the moment a true condition is found, whereas writing several separate, independent if statements would test every condition regardless and could execute more than one block. Choose a ladder specifically when the branches represent mutually exclusive outcomes.
Example: Ladder vs Single Ifs
#include <iostream>
int main() {
int score = 95;
if (score >= 90) std::cout << "A" << std::endl;
else if (score >= 70) std::cout << "B" << std::endl;
if (score >= 90) std::cout << "Also true (separate check)" << std::endl;
if (score >= 70) std::cout << "Also true (separate check)" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Default Fallback with Else
A trailing else with no condition acts as the ladder's catch-all, running only if every single condition above it turned out false — this is the natural place to handle unexpected or invalid input. Omitting it means that if none of the conditions match, the program simply does nothing at all, which can silently hide bugs.
Example: Default Fallback with Else
#include <iostream>
int main() {
int score = 40;
if (score >= 90) {
std::cout << "A" << std::endl;
} else if (score >= 70) {
std::cout << "B" << std::endl;
} else {
std::cout << "Fail" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: