switch statements in C
Multi-way branching with fall-through cases.
int days_in_month(int month) {
switch (month) {
case 2:
return 28;
case 4: case 6: case 9: case 11:
return 30;
default:
return 31;
}
}
How it works
switchjumps to the matchingcaselabel.- Stacked labels share one body.
defaulthandles everything else.
Keywords and builtins used here
casedays_in_monthdefaultintreturnswitch
The run, in numbers
- Lines
- 10
- Characters to type
- 134
- Tokens
- 40
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 28 seconds.
Step 5 of 7 in Operators & flow, step 12 of 35 in Language basics.