An else-if ladder helps you check many conditions one after another. It lets your program choose the right action based on different situations.
Else–if ladder in C++
if (condition1) { // code if condition1 is true } else if (condition2) { // code if condition2 is true } else if (condition3) { // code if condition3 is true } else { // code if none of the above conditions are true }
The conditions are checked from top to bottom.
Only the first true condition's code runs; the rest are skipped.
int num = 10; if (num > 0) { // positive number } else if (num == 0) { // zero } else { // negative number }
char grade = 'B'; if (grade == 'A') { // excellent } else if (grade == 'B') { // good } else if (grade == 'C') { // average } else { // needs improvement }
This program asks for a score and prints the grade using an else-if ladder.
#include <iostream> using namespace std; int main() { int score; cout << "Enter your score (0-100): "; cin >> score; if (score >= 90) { cout << "Grade: A" << endl; } else if (score >= 80) { cout << "Grade: B" << endl; } else if (score >= 70) { cout << "Grade: C" << endl; } else if (score >= 60) { cout << "Grade: D" << endl; } else { cout << "Grade: F" << endl; } return 0; }
Make sure conditions are ordered from most specific to least specific to avoid wrong results.
The else part is optional but good to handle unexpected cases.
Else-if ladder checks multiple conditions one by one.
Only the first true condition runs its code.
Use it when you have many choices to decide between.
Practice
What does an else-if ladder do in C++?
Solution
Step 1: Understand the purpose of else-if ladder
An else-if ladder is used to check multiple conditions in order, one after another.Step 2: Identify how conditions are evaluated
It runs the code block for the first condition that is true and skips the rest.Final Answer:
Checks multiple conditions one by one and runs the first true block -> Option DQuick Check:
Else-if ladder = first true condition runs [OK]
- Thinking all conditions run
- Believing else always runs
- Confusing else-if with separate ifs
Which of the following is the correct syntax for an else-if ladder in C++?
int x = 10;
if (x < 5) {
// code A
} _____ (x < 15) {
// code B
} else {
// code C
}Solution
Step 1: Recall correct else-if syntax in C++
The correct keyword iselse ifwith a space, no underscores or parentheses after else.Step 2: Check each option
Options A, B, and C are invalid syntax in C++.Final Answer:
else if -> Option AQuick Check:
Correct syntax = else if [OK]
- Writing 'elseif' as one word
- Using underscores like 'else_if'
- Adding parentheses after else
What will be the output of this C++ code?
int num = 7;
if (num > 10) {
std::cout << "Greater than 10";
} else if (num > 5) {
std::cout << "Greater than 5";
} else {
std::cout << "5 or less";
}Solution
Step 1: Evaluate first condition (num > 10)
7 is not greater than 10, so this block is skipped.Step 2: Evaluate second condition (num > 5)
7 is greater than 5, so this block runs and prints "Greater than 5".Final Answer:
Greater than 5 -> Option AQuick Check:
7 > 5 = true, prints "Greater than 5" [OK]
- Choosing first condition output incorrectly
- Ignoring else-if order
- Assuming else runs always
Find the error in this else-if ladder code snippet:
int score = 85;
if (score >= 90) {
std::cout << "Grade A";
} else if score >= 80 {
std::cout << "Grade B";
} else {
std::cout << "Grade C";
}Solution
Step 1: Check syntax of else-if condition
In C++, conditions must be inside parentheses. The else-if line lacks parentheses.Step 2: Verify other parts
Semicolons and operators are correct; only parentheses are missing.Final Answer:
Missing parentheses around condition in else-if -> Option BQuick Check:
Conditions need parentheses [OK]
- Forgetting parentheses in if/else-if
- Missing semicolons after statements
- Confusing else and else-if syntax
Write an else-if ladder to print the day of the week given an integer day (1 to 7). What will be printed if day = 5?
int day = 5;
if (day == 1) {
std::cout << "Monday";
} else if (day == 2) {
std::cout << "Tuesday";
} else if (day == 3) {
std::cout << "Wednesday";
} else if (day == 4) {
std::cout << "Thursday";
} else if (day == 5) {
std::cout << "Friday";
} else if (day == 6) {
std::cout << "Saturday";
} else if (day == 7) {
std::cout << "Sunday";
} else {
std::cout << "Invalid day";
}Solution
Step 1: Match the value of day with conditions
day is 5, so the else-if conditionday == 5is true.Step 2: Identify the output for day == 5
The code prints "Friday" for day 5.Final Answer:
Friday -> Option CQuick Check:
day 5 = Friday [OK]
- Mixing up day numbers and names
- Forgetting else block for invalid days
- Using wrong comparison operators
Ready to prove it?
Pass the test and this pattern moves unknown
