Lesson 7 of 31
Switch
Switch
The switch statement selects one of many code blocks to execute based on the value of an expression.
Basic Syntax
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("Other\n");
break;
}
Bridge stations: each crew member mans a different console. Helm, navigation, tactical -- switch to the right station for the job.
The break Statement
Each case must end with break, otherwise execution falls through to the next case:
int x = 1;
switch (x) {
case 1:
printf("one\n");
// no break -- falls through!
case 2:
printf("two\n");
break;
}
// prints: one
// two
Fall-through on Purpose
Sometimes fall-through is intentional, to handle multiple cases the same way:
switch (day) {
case 6:
case 7:
printf("Weekend\n");
break;
default:
printf("Weekday\n");
break;
}
default
The default case runs when no other case matches. It is optional but recommended.
Switch vs if-else
Use switch when comparing a single value against multiple constants. Use if-else for ranges or complex conditions.
Your Task
Write a function const char *day_type(int day) that returns "weekend" if day is 6 or 7, and "weekday" otherwise (1-5). Print the result for days 1, 6, and 7.
TCC compiler loading...
Loading...
Click "Run" to execute your code.