Quiz Entry - updated: 2026.07.14
What three special cases must a compiler handle when translating a switch statement?
Multiple labels mapping to one block, fall-through between cases, and missing case values that route to default.
* The three tricky switch cases and how the compiler handles each. *
A switch looks simple in C but hides three irregularities the code generator must respect. Take this example:
switch (x) {
case 1: w = y*z; break;
case 2: w = y/z; // no break -> falls through!
case 3: w += z; break;
case 5:
case 6: w -= z; break; // two labels, one body
default: w = 2;
}
| Challenge | How it's handled |
|---|---|
| Multiple labels (5 & 6) | Both jump-table slots point at the same code block |
| Fall-through (2 → 3) | Case 2's block simply doesn't end in a jump, so control flows into case 3 |
| Missing case (4) | That jump-table slot points at the default block |
Why this is interesting: these three behaviors are exactly what makes switch more than syntactic sugar for an if-chain — a jump table elegantly handles holes and duplicate tags, and ordinary code-sequencing handles fall-through "for free."
Go deeper:
Switch statement (Wikipedia) — the switch statement, including fall-through.
Branch table / jump table (Wikipedia) — the jump table that handles holes and duplicate tags.