C++ else-if Ladder
In this page:
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
}
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 75
int score = 75;
// Check whether score >= 90
if (score >= 90) {
// Print to the console with cout
std::cout << "A" << std::endl;
} else if (score >= 70) {
// Print to the console with cout
std::cout << "B" << std::endl;
} else {
std::cout << "C" << std::endl;
}
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 65
int score = 65;
// Check whether score >= 90
if (score >= 90) {
// Print to the console with cout
std::cout << "A" << std::endl;
} else if (score >= 80) {
// Print to the console with cout
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 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 95
int score = 95;
// Check whether score >= 60
if (score >= 60) {
// Print to the console with cout
std::cout << "Pass (matches too early)" << std::endl;
} else if (score >= 90) {
// Print to the console with cout
std::cout << "A (never reached)" << std::endl;
}
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 95
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 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 40
int score = 40;
// Check whether score >= 90
if (score >= 90) {
// Print to the console with cout
std::cout << "A" << std::endl;
} else if (score >= 70) {
// Print to the console with cout
std::cout << "B" << std::endl;
} else {
std::cout << "Fail" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Ordering conditions from broad to specific, like
score >= 50beforescore >= 90, so the first test always wins. - Writing
else ifaselseif, which is not valid C++. - Using separate
ifstatements instead of a ladder, so more than one branch can run.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: