Decision tracer Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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';
Same conditions, different structure (marks = 95 and marks = 60)
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

  1. Conditions are checked from the top, one after another.
  2. Only the FIRST true condition runs; the rest is skipped.
  3. Put the most demanding condition first (marks >= 90 before marks >= 75).
  4. A final else handles every case that no condition caught.
  5. Separate if statements are NOT a ladder: each is checked, so several can run.

Nested if

  1. An if can be inside another if (or inside its else).
  2. The inner condition is checked only when the outer path reaches it.
  3. Nested ifs form a decision tree: one question, then another.
  4. Often two levels can be combined: if (age >= 18 && hasId).
  5. Keep nesting shallow — deep nesting is hard to read.

The dangling else

  1. An else belongs to the NEAREST preceding if that does not have an else.
  2. Indentation does not change this — C ignores it.
  3. if (a) if (b) x(); else y(); → the else belongs to if (b).
  4. Use braces to make the owner clear: if (a) { if (b) x(); } else y();
  5. gcc -Wall warns: "suggest explicit braces to avoid ambiguous else".
Walkthrough

A ticket price: ladder + nested if

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int age = 30;
6 char day = 'S'; 'S' stands for Sunday.
7 int price;
8 if (age < 5) { 30 < 5 is false…
9 price = 0;
10 } else if (age < 18) { 30 < 18 is false…
11 price = 50;
12 } else if (age >= 60) { 30 >= 60 is false…
13 price = 70;
14 } else { …so the final else runs (an adult under 60).
15 if (day == 'S') A nested if: is it Sunday? Yes.
16 price = 120; Weekend price: 120.
17 else This else belongs to if (day == 'S') — the braces of the outer else make that clear.
18 price = 100;
19 }
20 printf("Ticket: %d\n", price); Prints Ticket: 120
21 return 0;
22 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Separate ifs instead of a ladder

✗ Wrong
if (m >= 90) puts("A");
if (m >= 75) puts("B");
if (m >= 50) puts("C");
✓ Right
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

✗ Wrong
if (m >= 50) grade = 'C';
else if (m >= 90) grade = 'A';
✓ Right
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

✗ Wrong
if (m >= 75) puts("B");
else (m >= 50) puts("C");
✓ Right
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

✗ Wrong
if (a > 0)
    if (b > 0) puts("both");
else
    puts("a <= 0");
✓ Right
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

✗ Wrong
int price;
if (age < 18) price = 50;
else if (age >= 60) price = 70;
printf("%d", price);
✓ Right
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.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

int m = 80; if (m >= 90) puts("A"); else if (m >= 75) puts("B"); else puts("C"); — output?

Question 2

In an else-if ladder, how many branches can run?

Question 3

With three separate if statements (no else), how many can run?

Question 4

if (a) if (b) x(); else y(); — which if does the else belong to?

Question 5

Which is the same as if (a > 0) { if (b > 0) x(); } ?

Assessment

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.