3 min read
Switch Statements
Prerequisites: Conditionals
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("Unknown\n");
}
Common mistakes
- Forgetting
break— execution falls through into the nextcaseinstead of stopping, one of C's most-cited sharp edges (occasionally used deliberately, but almost always a bug when accidental).