C switch Statement
In this page:
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no case matches
}
What is a switch Statement?
A switch statement evaluates one expression once and compares it against a list of case labels, jumping directly to the matching case -- a cleaner alternative to a long else-if ladder when you're comparing one variable against many fixed values.
Example: What is a switch Statement?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare day and set it to 3
int day = 3;
// Choose a branch based on the value of day
switch (day) {
case 3:
printf("Wednesday");
// Leave the switch/loop
break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Day of the Week
Mapping the integers 1 through 7 to day names is a classic switch use case: each case label handles one specific number, making the mapping far more readable than an equivalent chain of if-else comparisons.
Example: Day of the Week
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare day and set it to 2
int day = 2;
// Choose a branch based on the value of day
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Grade Switch
switch can match character constants too, so a grade switch might map the case A to print "Excellent", letting you branch on single-character input just as easily as on integers.
Example: Grade Switch
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare grade and set it to 'A'
char grade = 'A';
// Choose a branch based on the value of grade
switch (grade) {
case 'A':
printf("Excellent");
// Leave the switch/loop
break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The break Keyword
Without an explicit break at the end of a case block, execution "falls through" and continues running the code in the next case as well -- a frequent source of bugs for anyone coming from a language where fall-through isn't the default.
Example: The break Keyword
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 1
int x = 1;
// Choose a branch based on the value of x
switch (x) {
case 1:
printf("One ");
case 2:
printf("Falls through to Two");
// Leave the switch/loop
break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The default Case
The default case runs when the switch expression doesn't match any of the listed case values, functioning as a catch-all -- useful for handling unexpected input or flagging invalid values explicitly.
Example: The default Case
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 9
int x = 9;
// Choose a branch based on the value of x
switch (x) {
case 1:
printf("One");
// Leave the switch/loop
break;
// default runs when no case matches
default:
printf("Unknown value");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting
break, so execution falls through into the next case. - Using a string or a variable as a
caselabel, when labels must be integer constants. - Forgetting the
defaultcase for unexpected values.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: