else-if Ladder & Nested if
Many choicesWith else if you can choose between many options — grades, prices, menu choices. And an if can sit inside another if. In the lab below you watch a ladder being checked from the top, see what happens when the conditions are in the wrong order, walk a decision tree, and meet the dangling else — an else that belongs to a different if than it seems.
- Ladderthe first true condition wins
- Ordercheck the strictest condition first
- elsepairs with the nearest if
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
The else-if ladder
An else-if ladder is a chain of if … else if … else if … else. C checks the conditions from the top. The FIRST condition that is true runs its statement, and the rest of the ladder is skipped — later conditions are not even checked. The final else (optional) catches everything else.
if (marks >= 90)
grade = 'A';
else if (marks >= 75) // reached only if marks < 90
grade = 'B';
else if (marks >= 50) // reached only if marks < 75
grade = 'C';
else // everything below 50
grade = 'F';
| Version | Code | marks = 95 | marks = 60 |
|---|---|---|---|
| else-if ladder | if (m >= 90) A else if (m >= 75) B else if (m >= 50) C else F | A | C |
| separate ifs | if (m >= 90) A if (m >= 75) B if (m >= 50) C | A B C | C |
| ladder, wrong order | if (m >= 50) C else if (m >= 75) B else if (m >= 90) A | C | C |
| ladder with ranges | if (m >= 50 && m < 75) C else if (m >= 75 && m < 90) B … | A | C |
else-if ladder
- Conditions are checked from the top, one after another.
- Only the FIRST true condition runs; the rest is skipped.
- Put the most demanding condition first (marks >= 90 before marks >= 75).
- A final else handles every case that no condition caught.
- Separate if statements are NOT a ladder: each is checked, so several can run.
Nested if
- An if can be inside another if (or inside its else).
- The inner condition is checked only when the outer path reaches it.
- Nested ifs form a decision tree: one question, then another.
- Often two levels can be combined: if (age >= 18 && hasId).
- Keep nesting shallow — deep nesting is hard to read.
The dangling else
- An else belongs to the NEAREST preceding if that does not have an else.
- Indentation does not change this — C ignores it.
- if (a) if (b) x(); else y(); → the else belongs to if (b).
- Use braces to make the owner clear: if (a) { if (b) x(); } else y();
- gcc -Wall warns: "suggest explicit braces to avoid ambiguous else".
A ticket price: ladder + nested if
Every line, explained.
#include <stdio.h>
int main(void)
{
int age = 30;
char day = 'S';
'S' stands for Sunday.
int price;
if (age < 5) {
30 < 5 is false…
price = 0;
} else if (age < 18) {
30 < 18 is false…
price = 50;
} else if (age >= 60) {
30 >= 60 is false…
price = 70;
} else {
…so the final else runs (an adult under 60).
if (day == 'S')
A nested if: is it Sunday? Yes.
price = 120;
Weekend price: 120.
else
This else belongs to if (day == 'S') — the braces of the outer else make that clear.
price = 100;
}
printf("Ticket: %d\n", price);
Prints Ticket: 120
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
Separate ifs instead of a ladder
if (m >= 90) puts("A");
if (m >= 75) puts("B");
if (m >= 50) puts("C");
if (m >= 90) puts("A");
else if (m >= 75) puts("B");
else if (m >= 50) puts("C");
Separate ifs are all checked: 95 prints A, B and C. else if stops after the first match.
The wrong order
if (m >= 50) grade = 'C'; else if (m >= 90) grade = 'A';
if (m >= 90) grade = 'A'; else if (m >= 50) grade = 'C';
With m >= 50 first, a 95 already matches it, so the A branch is never reached.
A condition after else
if (m >= 75) puts("B");
else (m >= 50) puts("C");
if (m >= 75) puts("B");
else if (m >= 50) puts("C");
A plain else never has a condition. The first version does not compile: expected ';' before 'puts'.
The dangling else
if (a > 0)
if (b > 0) puts("both");
else
puts("a <= 0");
if (a > 0) {
if (b > 0) puts("both");
} else
puts("a <= 0");
Without braces the else belongs to if (b > 0), so "a <= 0" is printed when a is positive.
No final else
int price;
if (age < 18) price = 50;
else if (age >= 60) price = 70;
printf("%d", price);
int price; if (age < 18) price = 50; else if (age >= 60) price = 70; else price = 100;
For ages 18 to 59 no branch runs, so price is never set and prints garbage.
Check your understanding
Answer each question — you will see the explanation straight away.
int m = 80; if (m >= 90) puts("A"); else if (m >= 75) puts("B"); else puts("C"); — output?
80 >= 90 is false, 80 >= 75 is true → B; the rest is skipped.
In an else-if ladder, how many branches can run?
Only the first true one (or the final else).
With three separate if statements (no else), how many can run?
Each separate if is checked on its own.
if (a) if (b) x(); else y(); — which if does the else belong to?
An else pairs with the nearest preceding if without an else.
Which is the same as if (a > 0) { if (b > 0) x(); } ?
x() runs only when both conditions are true: &&.
else-if Ladder & Nested if quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.