switch Statements
The switch statement, case labels, fallthrough as both a bug and a tool, and default.
Choosing by Value
When a decision hinges on which value an integer expression holds, rather than which range or condition, C offers a dedicated statement. switch compares one expression against a set of constant labels and jumps straight to the match. Done right it reads like a table; its one sharp edge, fallthrough, is both a famous bug and a deliberate tool, and exams test the two faces relentlessly.
The Shape
The controlling expression in parentheses must be an integer type, int or char in practice; switch cannot test a double or a range. Each case label is a constant expression, a literal, a #defined value, or a character constant, never a variable. Execution jumps to the matching label; default catches everything unmatched, and while it works anywhere in the block, last is where readers expect it. Duplicate case values are a compile error.
break, and What Happens Without It
break exits the switch. Here is the part that separates switch from an else-if ladder: without a break, execution continues into the next case's statements. The labels are entry points, not walls. This behaviour is called fallthrough, and forgetting it is the classic switch bug:
/* WRONG: missing breaks, every later message also prints */
switch (day) {
case 1:
printf("Monday\n");
case 2:
printf("Tuesday\n");
default:
printf("unknown day\n");
}
With day equal to 1, all three lines print: execution enters at case 1 and simply keeps going. On an exam, "predict the output of a switch with missing breaks" is a fallthrough question in costume; trace it as straight-line code from the entry label down, stopping only at a break or the closing brace.
Fallthrough on Purpose
The same behaviour, used deliberately, lets several values share one body, which is the idiomatic way to say "these cases are the same":
Empty cases stacked on one body are universally accepted style; fallthrough past statements is where trouble lives, and when veteran code does it on purpose it carries a comment saying so. Course code stacks labels freely and otherwise ends every case with break.
switch or the Ladder?
The two tools overlap and exams like asking which fits. The honest division: switch handles equality against a fixed set of integer constants, menu options, day numbers, single characters like 'y' and 'n'; the else-if ladder handles everything else, ranges (marks >= 90), non-integer types, and conditions built from && and ||. Grading is a ladder because it tests ranges; a calculator's + - * / menu is a switch because it tests one character against four constants. When either would do, switch usually reads better, one glance shows every value the code responds to.
Key Takeaways
switch (integerExpression)jumps to the matching constantcaselabel;defaultcatches the rest; duplicate labels do not compile.- Case labels are entry points: without
break, execution falls through into the next case's statements. - Trace exam switches as straight-line code from the entry label, stopping at
breakor the closing brace. - Stacked empty labels sharing one body are idiomatic; fallthrough past statements is a bug unless loudly commented.
- Ranges, doubles, and compound conditions belong to the else-if ladder; fixed integer alternatives belong to
switch.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
switch Statements - Quiz
Test your understanding of the lesson.
Practice Exercises
The Calculator Menu
Read two doubles and an operation character, then use a switch on the character to print the result of adding, subtracting, or multiplying. Any other character reports "unknown operation". Guard the scanf as always.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!