CS Atlas GATE 2027
PDS course
0 / 103 complete

PDS/Phase 1 · Programming in C

Lesson 1.2.1 · Item 14 of 103

if / else if / else; nested conditions; switch and fall-through

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the bottom.

1 · The Hook

🎣

A railway ticket costs half for children under 12, full for adults, and 40% less for seniors over 60. A program that prints fares must take different paths for different ages. Until now every statement ran exactly once, top to bottom. This lesson adds the fork in the road.

2 · Predict First

🤔

What does this print?

int x = 0;
if (x = 5)
    printf("yes %d\n", x);
else
    printf("no\n");

Make a guess in your head. The answer opens Section 3.

3 · Core Concept

💡

Predict First — the answer

Output

yes 5

The condition is x = 5, an assignment, not a comparison. It stores 5 and its value is 5, which is non-zero, so it counts as true. The intended test was x == 5.

if and else

if (condition)
    statement1;       /* runs when condition is non-zero */
else
    statement2;       /* runs when condition is 0 */

The condition can be any expression: non-zero means true. To run several statements, group them in braces { … }, which form one compound statement. Without braces, only the single next statement belongs to the if.

else if ladders

Conditions are tested top to bottom; the first true one runs and the rest are skipped.

int marks = 72;
if (marks >= 90)
    printf("A\n");
else if (marks >= 75)
    printf("B\n");
else if (marks >= 60)
    printf("C\n");
else
    printf("F\n");

Output

C

Nested if and the dangling else

An else always belongs to the nearest unmatched if above it, no matter how the code is indented.

int a = 5, b = 0;
if (a > 10)
    if (b == 0)
        printf("one\n");
else
    printf("two\n");
printf("end\n");

Despite the indentation, else pairs with if (b == 0). Since a > 10 is false, the whole inner if-else is skipped:

Output

end

switch

switch (integer expression) {
    case constant1: statements;  break;
    case constant2: statements;  break;
    default:        statements;
}

The expression is evaluated once and execution jumps to the matching case label (or to default if none match; with no default, nothing runs). From there it continues downward, through later case labels, until a break or the closing brace. Running on into the next case is called fall-through.

  • Case labels must be integer constants (a char like 'A' is fine; 2.5 or a variable is not), and must all differ.
  • default may appear anywhere; it is only where execution jumps when nothing matches.

4 · Visual

switch (x) with x = 2, no breaks:

   jump in here
        |
        v
 case 1:  x += 1;      (skipped: we entered below it)
 case 2:  x += 2;      x = 4
 case 3:  x += 3;      x = 7     <- falls through: labels do not stop execution
 default: x += 4;      x = 11
 }                     -> prints 11

Dangling else:  if (A) if (B) S1; else S2;     ==     if (A) { if (B) S1; else S2; }

A case label is only an entry point; break is the only exit inside a switch.

Diagram: switch (x) · evaluate x once · jump to case x · or default · run downward · through later cases · stop · at break or }

How switch runs.

5 · Worked Examples

Example 1 Easy

What does this print?

int marks = 72;
if (marks >= 90)
    printf("A\n");
else if (marks >= 75)
    printf("B\n");
else if (marks >= 60)
    printf("C\n");
else
    printf("F\n");

Solution

72 ≥ 90? no. 72 ≥ 75? no. 72 ≥ 60? yes → C. The ladder stops at the first true condition.

Output

C

Example 2 Medium

What does this print?

char g = 'B';
switch (g) {
    case 'A': printf("Excellent ");
    case 'B': printf("Good ");
    case 'C': printf("Fair ");
              break;
    default:  printf("Fail ");
}
printf("\n");

Solution

g is 'B', so execution enters at case 'B', prints "Good ", falls into case 'C' and prints "Fair ", then break leaves the switch.

Output

Good Fair

Example 3 GATE

What does this print?

int x = 2;
switch (x) {
    case 1: x += 1;
    case 2: x += 2;
    case 3: x += 3;
    default: x += 4;
}
printf("%d\n", x);

Solution

Enter at case 2 with x = 2. No breaks: x += 2 → 4, x += 3 → 7, x += 4 → 11.

Output

11

6 · GATE Traps

⚠️

  • if (x = 0) assigns 0 and is always false; if (x = 5) is always true. Use ==.
  • Without braces only one statement belongs to an if: indentation means nothing to the compiler.
  • An else pairs with the nearest unmatched if (dangling else).
  • switch falls through until break; a matching case does not stop at the next label.
  • Case labels must be distinct integer constants: no floats, no variables, no ranges.
  • A semicolon right after if (c); makes an empty body: the next statement always runs.

8 · Memory Hook

🧠

“Else marries the nearest if.” And a switch is a slide with several entry ladders: climb on at your case, slide down until a break catches you.

9 · Connect the Dots

Lessons 1.1.x built expressions; Lesson 1.1.2 showed that comparisons give 1 or 0, which is exactly what if tests. Choosing is half of control flow. Next lesson answers: how can a program repeat statements many times without writing them out again?

10 · Self-Test

GATE format. Try every question before opening the vault.

Q1MCQ1 markDangling else

What does this print?

int a = 3, b = 3;
if (a == b)
    if (a > 5)
        printf("X");
    else
        printf("Y");
else
    printf("Z");
printf("\n");
  • (A) nothing
  • (B) X
  • (C) Y
  • (D) Z

Q2NAT2 marksSwitch

What value does this print?

int k = 3, s = 0;
switch (k * 2 - 1) {
    case 4: s += 4;
    case 5: s += 5;
    case 6: s += 6; break;
    case 7: s += 7;
    default: s += 1;
}
printf("%d\n", s);

Numerical answer: ________

Q3MCQ1 markSwitch rules

Which case label is not allowed in a switch on an int?

  • (A) case 'A':
  • (B) case 2.5:
  • (C) case -1:
  • (D) case 3 + 4:

Q4MCQ1 markOutput

What does this print?

int x = 10, y = 20;
if (x > y)
    printf("A");
    printf("B");
printf("\n");
  • (A) AB
  • (B) B
  • (C) nothing
  • (D) A

Q5MCQ2 marksSwitch

What does this print?

int n = 9;
switch (n % 4) {
    default: printf("D");
    case 0: printf("0");
    case 2: printf("2"); break;
    case 3: printf("3");
}
printf("\n");
  • (A) D023
  • (B) 02
  • (C) D02
  • (D) D

11 · Rule Card

  • if (cond) S1; else S2; — cond non-zero → S1.
  • Braces group statements; without them one statement belongs to the if.
  • else ladder: first true branch runs, rest skipped.
  • Dangling else binds to the nearest unmatched if.
  • switch: evaluate once, jump to matching case/default, run down until break or }.
  • Case labels: distinct integer constants (chars allowed).
  • if (x = 5) assigns; compare with ==.

12 · Concept Ledger

Unlocked in this lesson. From here on, these terms may be used.

if / elsecompound statement { }else-if laddernested ifdangling elseswitchcase labeldefaultbreak (in switch)fall-through

13 · Answer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Recall Warm-up

  • 0: −1 is converted to unsigned (4,294,967,295) before comparing.
  • 5: an assignment has the value that was stored.

Self-Test

Q1 Answer: (C)1 mark

a == b, so the inner if-else runs; a > 5 is false → Y.

Output

Y
  • (A) is wrong: The inner else runs.
  • (B) is wrong: a is 3, not greater than 5.
  • (D) is wrong: The outer condition is true.

Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through

Q2 Answer: 112 marks

k*2 − 1 = 5: enter case 5: s = 5, fall into case 6: s = 11, break.

Output

11

Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through

Q3 Answer: (B)1 mark

Case labels must be integer constants; 2.5 is not an integer.

  • (A) is wrong: 'A' is the integer 65.
  • (C) is wrong: Negative integers are fine.
  • (D) is wrong: 3 + 4 is a constant integer expression.

Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through

Q4 Answer: (B)1 mark

Without braces only printf("A") belongs to the if. The second printf always runs.

Output

B
  • (A) is wrong: A is not printed.
  • (C) is wrong: B is outside the if.
  • (D) is wrong: x > y is false.

Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through

Q5 Answer: (C)2 marks

9 % 4 = 1 matches no case, so execution jumps to default (written first), prints D, falls through to case 0 (prints 0) and case 2 (prints 2), then break.

Output

D02
  • (A) is wrong: break after case 2 stops it.
  • (B) is wrong: Nothing matched, so default runs.
  • (D) is wrong: default falls through too.

Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.