switch statements
Use switch statements in C for clean multi-way branching on integer and character values — including fallthrough, break, and default.
- Write a switch statement with case and default labels
- Explain what fallthrough is and how to prevent it with break
- Choose between switch and else if appropriately
When a program needs to branch on many possible values of a single expression, an else if chain becomes unwieldy. The switch statement is C's answer: a cleaner way to dispatch on integer and character values.
Basic syntax
#include <stdio.h>
int main(void) {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Weekend\n");
break;
}
return 0;
}The expression in switch(day) is evaluated once. Control jumps to the matching case label. If no case matches, control jumps to default.
The break statement is mandatory
Here is the critical difference from Python's match or Java's newer switch expressions: C switch falls through by default. Without a break, execution continues into the next case.
int n = 2;
switch (n) {
case 1:
printf("one\n");
case 2:
printf("two\n"); /* prints */
case 3:
printf("three\n"); /* ALSO prints -- fallthrough! */
default:
printf("other\n"); /* ALSO prints -- fallthrough! */
}This behaviour is usually unintentional and a source of bugs. Forgetting break is one of the most common C mistakes. Every case except the last should end with break (or return) unless you explicitly want fallthrough.
Intentional fallthrough
Fallthrough is occasionally useful when multiple cases should do the same thing:
char c = 'a';
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("vowel\n");
break;
default:
printf("consonant or other\n");
break;
}Here, case 'a': falls through to case 'e': ... all the way through to the printf. This is intentional and readable because the empty cases are stacked vertically with no code between them.
The default label
default is optional but recommended. When the switch expression does not match any case, default runs. Without it, no code runs and the program continues silently — which can hide bugs. Always include a default case that either handles unexpected values explicitly or asserts they should never occur.
What switch can and cannot test
switch works with integer types: int, char, short, long, and enum values. It does not work with float, double, or strings. For those, use if/else.
Case labels must be compile-time constant integers:
int x = 5;
switch (n) {
case x: /* ERROR: x is not a constant */
break;
case 5: /* OK: 5 is a constant */
break;
}switch versus else if
Use switch when:
- You are testing a single expression against multiple constant values.
- There are four or more cases.
- The cases are of integer or character type.
Use if/else if when:
- The conditions involve ranges (
score >= 80). - The conditions involve different variables.
- You are testing strings or floating-point values.
- There are only two or three cases.
/* This is a good switch candidate */
switch (command_code) {
case CMD_QUIT: handle_quit(); break;
case CMD_SAVE: handle_save(); break;
case CMD_LOAD: handle_load(); break;
default: handle_unknown(); break;
}
/* This is not a good switch candidate -- use if/else if */
if (temperature > 100.0) {
printf("Boiling\n");
} else if (temperature < 0.0) {
printf("Freezing\n");
}Add a comment when fallthrough is intentional. Compilers like gcc with -Wimplicit-fallthrough will warn about fallthrough unless you add a comment like /* falls through */ or use the C17 attribute [[fallthrough]]. Documenting intent protects future readers from thinking the break is missing.
Where to go next
You have both main branching tools. Next: while and do-while loops — repeating work until a condition changes.
if, else, and else if
Write conditional logic in C using if, else, and else if — including comparison operators, logical operators, and common patterns.
while and do-while loops
Repeat code in C with while and do-while loops — including condition testing, loop body execution, and the difference between the two forms.