Module 3 · Decisions and Loops — topic 2 of 5
The switch Statement
switch selects one of many branches by comparing an integer expression with constant case labels. Learn case, default, break, intentional fall-through and when switch beats an else-if ladder.
Learning objectives
- Write switch statements with case, default and break
- Explain and deliberately use fall-through
- Choose between switch and if-else
Definition
Aswitchstatement transfers control to thecaselabel whose integer constant expression equals the value of the controlling expression, or todefaultif none match; execution continues until abreakor the end of the switch body.
Introduction
Menu-driven programs — “1. Deposit 2. Withdraw 3. Exit” — compare one value against many fixed options. Writing that as an else-if ladder works, but switch expresses the intent more clearly and can compile to a fast jump table.
switch has one surprising rule: after a matching case runs, execution falls through into the next case unless you write break.
Why it matters
Forgotten breaks are a classic exam question and a real source of bugs. Understanding fall-through lets you avoid it — or use it deliberately to group cases.
Command dispatch, state machines and protocol parsers in C are usually written with switch.
Detailed explanation
The controlling expression must have an integer type (int, char, an enum, …). Floating-point values and strings cannot be switched on.
Each case label must be an integer constant expression — a literal, character constant, enumeration constant or macro — and no two labels may be equal.
Control jumps to the matching label and runs statements from there downward. break exits the switch. If there is no break, execution continues into the next label's statements: this is fall-through.
default runs when no case matches. It may appear anywhere but is conventionally last; include it to handle unexpected values.
Syntax
- Case labels end with a colon, not a semicolon.
- Stacking labels without statements shares one branch.
- Declare variables inside a case within braces
{ }to give them their own scope.
Code examples
Day of the week
Wednesday
Fall-through when break is missing
two three done
Execution starts at case 2 and runs every statement below it.
More examples
A calculator using char cases and grouped labels
10 + 4 = 14 10 x 4 = 40 10 / 4 = 2.5 10 / 0: invalid 10 % 4: invalid
Step-by-step explanation
switch (op)works because acharis an integer type;'+'is a character constant.returnleaves the function, so those cases need nobreak.'*','x'and'X'are stacked labels that share the multiplication branch.- For
'/'with a zero divisor theifis false and execution deliberately falls intodefault. '%'matches no case, sodefaultreports it as invalid.%gprints numbers without trailing zeros.
Expected output
Running “A calculator using char cases and grouped labels” prints exactly:
Practical applications
- Menu-driven console applications dispatch on the user's choice.
- Interpreters and virtual machines switch on opcode values.
- Embedded state machines switch on an
enumstate.
Best practices
- Put a
breakat the end of every case, including the last one. - Mark intentional fall-through with a comment such as
/* fall through */. - Always include a
defaultcase to catch unexpected values.
Common mistakes
Forgetting break
Avoid
Prefer
Choosing 1 prints both lines because control falls into case 2.
Using a range or variable as a label
Avoid
Prefer
Case labels must be integer constant expressions; ranges belong in if statements.
Switching on a string
Avoid
Prefer
Strings are arrays, not integers. Compare them with strcmp.
Important notes
- C23 adds the
[[fallthrough]];attribute to mark intentional fall-through; GCC's-Wimplicit-fallthroughwarns about unmarked fall-through. - Jumping past a variable's initialisation into its scope is not allowed for variable-length arrays; wrap case-local variables in braces.
Key takeaways
switchcompares an integer expression with constantcaselabels.- Without
break, execution falls through to the next case. - Use
defaultfor unmatched values andiffor ranges or strings.
Try it yourself
- 1Write a menu program: read 1–4 and print “Add”, “Search”, “Delete” or “Exit”, with an error for anything else.
- 2Read a character and use grouped cases to print whether it is a vowel or a consonant (handle upper and lower case).
Finished reading? Mark it complete to update your dashboard.
Suggested practice
6 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhich type can NOT be used as a switch expression?
- ModerateWhat is printed?
- ModerateWhat is printed?