0 day streak
c / beginner
Snippet

The switch Statement

The switch statement allows you to execute different parts of code based on the value of an integer variable.

snippet.c
c
1
2
3
4
5
6
7
8
9
int grade = 2;
switch (grade) {
case 1:
printf("Excellent");
break;
case 2:
printf("Good");
break;
}
Breakdown
1
switch (grade) {
Evaluates the expression inside parentheses once.
2
case 1:
Checks if the value of grade matches the constant 1.
3
break;
Terminates the switch block to prevent code from running into the next case.