switch Statement
Choosing by valueWhen one value decides between many fixed choices — a menu number, a grade letter, a day of the week — switch is clearer than a long else-if ladder. In the lab below you watch switch jump to the matching case, see what break does, what happens without it, and which case labels the compiler refuses.
- switchjumps straight to the matching case
- breakleaves the switch
- defaultruns when nothing matches
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
The shape of a switch
switch takes ONE whole-number value (an int, a char …) and jumps straight to the case label with the same value. From there the statements run until a break leaves the switch. If no label matches, it jumps to default — or skips the whole switch if there is no default.
switch (expression) {
case 1:
statements;
break; // leave the switch
case 2:
case 3: // 2 and 3 share this block
statements;
break;
default: // no case matched
statements;
}
| switch | else-if ladder | |
|---|---|---|
| Tests | one value against fixed constants | any conditions |
| Types | whole numbers and characters only | anything (double, ranges, several variables) |
| Ranges like x > 5 | no | yes |
| How it decides | jumps straight to the matching case | checks the conditions from the top |
| Best for | menus, commands, codes, letters | ranges, mixed conditions |
switch and case
- The expression in switch ( ) is worked out once; it must be a whole number (int, char, long …).
- case labels are constants: case 1:, case 'a':, case MAX: (a #define). Note the colon.
- switch JUMPS to the matching label — the cases above it are not run.
- Every label must be different.
- Use a switch when one value chooses among many fixed options.
break and fall-through
- break leaves the switch immediately.
- Without break, execution FALLS THROUGH into the next case, ignoring its label.
- Forgetting break is the most common switch bug (gcc -Wextra: -Wimplicit-fallthrough).
- Stacked labels (case 'a': case 'e': …) share one block — fall-through on purpose.
- Mark intentional fall-through with a comment: /* fall through */.
default and the rules
- default runs when no case matches; it is optional but recommended.
- Without default and without a match, the switch silently does nothing.
- default can be anywhere, but it is usually last (then it needs no break).
- No doubles, no strings, no variables, no ranges as case labels.
- To declare a variable inside a case, put the case's statements in { }.
A tiny calculator
Every line, explained.
#include <stdio.h>
int main(void)
{
double a = 12, b = 4;
char op = '/';
The operator is a char — a small whole number, so it can be switched on.
switch (op) {
Jump to the case for '/'.
case '+': printf("%.2f\n", a + b); break;
case '-': printf("%.2f\n", a - b); break;
case '*': printf("%.2f\n", a * b); break;
case '/':
This is the matching case.
if (b != 0)
A case can contain any statements — even an if.
printf("%.2f\n", a / b);
Prints 3.00
else
printf("Cannot divide by zero\n");
break;
Leave the switch.
default:
Any other character lands here.
printf("Unknown operator %c\n", op);
}
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
A forgotten break
case 1: puts("Tea");
case 2: puts("Coffee"); break;
case 1: puts("Tea"); break;
case 2: puts("Coffee"); break;
Without break, choice 1 prints Tea AND Coffee: execution falls through into case 2.
A condition as a case label
switch (marks) {
case marks >= 90: puts("A");
}
if (marks >= 90)
puts("A");
Labels must be constants. Ranges and conditions need if / else if.
A semicolon instead of a colon
case 1; puts("one"); break;
case 1: puts("one"); break;
A case label ends with a colon. The semicolon gives a compiler error: expected ':' before ';'.
Declaring a variable right after a label
case 1:
int total = 5;
break;
case 1: {
int total = 5;
break;
}
Before C23, a label cannot be followed by a declaration: "a label can only be part of a statement". Braces make a block.
switch on a string
switch (answer) {
case "yes": …
}
switch (answer[0]) {
case 'y': …
}
switch works only with whole numbers. Compare strings with strcmp (in the Strings part), or switch on one character.
Check your understanding
Answer each question — you will see the explanation straight away.
int x = 2; switch (x) { case 1: puts("A"); break; case 2: puts("B"); break; default: puts("C"); } — output?
switch jumps to case 2, prints B, then break leaves the switch.
The same switch WITHOUT any break and x = 2 — output?
It jumps to case 2 and falls through into default.
Which type can NOT be used in switch ( )?
switch needs a whole number; double is not allowed.
When does default run?
default handles the values that no case covers.
Which is a valid case label?
A character constant is a whole-number constant ('q' is 113).
switch Statement quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.