C Tutorial
C Switch
switch picks a case from an integer or enum. Remember break, or execution falls through.
One value, several labels
A switch compares an integer (or an enum) against a list of case labels. When a label matches, execution jumps there. It is a menu: the program looks at one number and picks a branch.
You can switch on int, char, and enum. You cannot switch on adouble or on a string. For text, use if / else if. For ranges such asscore >= 80, use if as well. Switch wants exact matches.
A complete program
Each case ends with break; so the program leaves the switch after that branch.default runs when no label matches.
Example
#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;
default:
printf("another day\n");
break;
}
return 0;
}Change day in /c/try. Try 1, 3, and 9 to see a matching case anddefault.
case, default, break
case n:is a label.nmust be a constant integer expression.default:is the fallback. Put it last. You can omit it if every value is covered.break;jumps out of the switch. Without it, execution continues into the next case.
The parentheses after switch hold the value you test. The braces after that hold every case. Case labels themselves do not use extra parentheses.
Fall-through
If you forget break, C does not stop at the end of that case. It keeps running the statements in the following cases until it hits a break or the closing brace. That is called fall-through. It is almost always a bug.
Example
#include <stdio.h>
int main(void) {
int day = 1;
switch (day) {
case 1:
printf("Mon\n");
case 2:
printf("Tue\n");
break;
default:
printf("other\n");
break;
}
return 0;
}This program prints both Mon and Tue because case 1 has no break. Add a break after every case unless you grouped empty labels on purpose.
Sharing one body
Stacking labels without statements between them is the one common, intentional fall-through. Several values run the same block. Put a comment on the empty cases so the next reader knows it is deliberate.
Example
#include <stdio.h>
int main(void) {
int day = 6;
switch (day) {
case 6:
case 7:
printf("weekend\n");
break;
default:
printf("weekday\n");
break;
}
return 0;
}switch versus if
| switch | if / else if | |
|---|---|---|
| Best for | Exact integer or enum values | Ranges and combinations |
| Strings and doubles | No | Yes |
| Easy miss | Forgotten break | Wrong order of tests |
Enums belong with switch. You will meet enum later; the shape is the same: one value, named cases, a break on each branch.
Next: while — repeating a block while a condition stays true.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Solid, liquid, gas
Water’s common states at 1 atm are ice, liquid, and steam. A coded sample type is an integer because switch in C wants an integer (or enum). 1, 2, 3 are arbitrary; the names in the comments are not.
Forget break and execution falls into the next case. That is a bug here, not a feature.
Example
#include <stdio.h>
int main(void) {
int state = 2; /* 1 solid, 2 liquid, 3 gas */
switch (state) {
case 1:
printf("solid — ice\n");
break;
case 2:
printf("liquid — water\n");
break;
case 3:
printf("gas — steam\n");
break;
default:
printf("unknown\n");
}
return 0;
}Astronomy
Planet index
Old catalogues numbered planets from the Sun: 3 Earth, 4 Mars, 5 Jupiter. Pluto is not in this switch, which is historically accurate for a 2006-and-after list and also a reminder that default exists.
switch maps a small integer onto a name. If the number comes from a file, always keep default.
Example
#include <stdio.h>
int main(void) {
int n = 4;
switch (n) {
case 3:
printf("Earth\n");
break;
case 4:
printf("Mars\n");
break;
case 5:
printf("Jupiter\n");
break;
default:
printf("not in this list\n");
}
return 0;
}