c / beginner
Snippet
The switch Statement
The switch statement allows you to select one of many code blocks to be executed based on the value of a variable.
snippet.c
1
2
3
4
5
6
7
8
9
int mode = 2;switch (mode) {case 1:break;case 2:break;default:break;}
Breakdown
1
switch (mode)
The variable to be compared against various cases.
2
case 1:
A label that marks a block of code to run if 'mode' equals 1.
3
break;
Terminates the switch block to prevent 'falling through' to the next case.