switch picks one path by matching an integer expression to a case label. Use break to leave the switch; use default when nothing matches. Prefer else if for ranges or non-integer tests.
Remember
switch (expr) {
case 1:
/* ... */
break;
case 2:
/* ... */
break;
default:
/* no match */
break;
}
expr and case labels → integer types only
omit break → fall through to the next case
Match an integer, write a case for each constant, and end most cases with break.
Put an integer expression in switch (expr).
Add case constant: labels (compile-time integer constants).
End each case with break unless you want fall-through.
Add default: for unexpected values, then compile and run.
Piece
Required?
Role
switch (expr)
Yes
Value to match (integer type)
case labels
Usually
Constant targets to jump to
break
Usually
Stops fall-through after a match
default
Recommended
Runs when no case matches
Pseudocode
Pseudocode
evaluate expr
if expr equals case A:
run A statements
stop (break) or fall into next case
else if expr equals case B:
run B statements
...
else:
run default (if present)
continue after the switch
Reference
Cheat Sheet
Keep these facts handy while writing multi-way branches.
Topic
Detail
Syntax
switch (e) { case c: ... break; default: ... }
Allowed types
int, char, enum, other integers
Not allowed
float, double, strings
case values
Compile-time constants; no duplicates
break
Leaves the switch; omit only for intentional fall-through
default
Optional catch-all; place anywhere (often last)
vs else if
switch for constants; else if for ranges
Try it
Live Preview
Trace a small switch in the browser. Same control flow as the C examples below.
Browser simulation of switch matching (same idea as the C examples).
Result
day = 2
switch (day)
case 1 → skip
case 2 → match → "Tuesday" → break
output: Tuesday
Trace
Worked Walkthrough — Day 3
Follow switch (day) when day = 3 with cases for Monday–Sunday and a default.
Calculation
day = 3
jump to case 3:
print "Wednesday"
break → leave switch
cases 1, 2, 4… never run
default never runs (we matched)
If day were 0: no case matches → default
If case 3 had no break: fall into case 4 (bug!)
Code
C Examples
Compile with gcc file.c -std=c11 -o out. Pair with break and else if when choosing a style.
Example 1 — Map a Day Number to a Name
One integer, several constant labels, and a break after each case.
C
#include <stdio.h>
int main(void) {
int day = 2;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Unknown day\n");
break;
}
return 0;
}
Output
Tuesday
How It Works
Evaluate the expression.day is 2.
Jump to the matching label. Control goes to case 2:.
Run that case. Prints Tuesday.
Break out. Without break, execution would continue into case 3.
Example 2 — Catch Invalid Input with default
When no case matches, default runs. Good for menus and validation.
C
#include <stdio.h>
int main(void) {
int choice = 9;
switch (choice) {
case 1:
printf("New game\n");
break;
case 2:
printf("Load game\n");
break;
case 3:
printf("Quit\n");
break;
default:
printf("Invalid choice: %d\n", choice);
break;
}
return 0;
}
Output
Invalid choice: 9
How It Works
Choice is 9. None of the menu cases match.
default runs. Prints the invalid-choice message.
Still use break. Keeps the pattern consistent if you reorder cases later.
Safer menus. Unexpected input gets a clear response instead of silence.
Example 3 — Intentional Fall-Through
Stack labels when several values should share the same code. Comment the fall-through so readers know it is deliberate.
C
#include <stdio.h>
int main(void) {
int day = 6; /* 6 = Saturday, 7 = Sunday */
switch (day) {
case 6:
case 7:
/* fall through: both weekend days */
printf("Weekend\n");
break;
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Weekday\n");
break;
default:
printf("Invalid day\n");
break;
}
return 0;
}
Output
Weekend
How It Works
Match case 6. There is no body yet—execution falls into case 7.
Shared body. Both Saturday and Sunday print Weekend.
Then break. Stops before weekday cases run.
Document it. A short comment prevents “missing break” confusion in review.
Edge Cases & Pitfalls
Check these before using switch in real programs.
break
Forgot break
Execution falls into the next case. Almost always a bug unless you documented intentional fall-through.
types
Wrong type for switch
No float, double, or string expressions. Use else if / strcmp for those.
range
Trying ranges in case
case 80...100 is not portable ISO C. Use else if for score bands.
default
No default
Unexpected values do nothing. Add default for menus and user input.
Analysis
Performance Notes
Approach
Cost
Best for
switch (dense ints)
Often a jump table
Many adjacent constants
else if chain
Linear checks
Ranges, mixed conditions
Few cases
Either is fine
Pick the clearer form
Compilers often optimize dense switch cases well. Prefer clarity first; micro-optimizing branch style rarely matters for beginners.
Remember
Key Takeaways
Role: multi-way branch on one integer expression.
break: end each case unless fall-through is intentional.
default: catch unexpected values in menus and input.
Limits: integers only—use else if for ranges and strings.
One line:switch means “jump to the matching case—then usually break.”
Frequently Asked Questions
switch compares one integer expression against labeled case values. When a case matches, its statements run until break or the end of the switch. An optional default handles no match.
The switch expression and case labels must be integer types: int, char, short, long, or enum. C does not allow switch on float, double, or strings. Compare strings with if-else or strcmp.
break exits the switch block immediately. Without break, execution falls through into the next case. Always use break unless you deliberately want fall-through.
Fall-through happens when a case has no break and execution continues into the following case(s). It can be a bug or intentional—for example grouping case 6 and case 7 for weekend.
Use switch when comparing one variable to many constant values (menu 1/2/3, day 1–7). Use else if for ranges (score >= 80) or unrelated conditions.
No, but include default to catch unexpected input. It improves robustness and makes debugging easier when a value does not match any case.