C++ switch Statement
In this page:
Basic switch Statement
A switch statement compares one variable's value against several possible constant cases and jumps directly to the matching one, which reads more cleanly than a long else-if ladder when you're checking a single value against many discrete options like a menu choice or a day number.
Example: Basic switch Statement
#include <iostream>
int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The break Statement
break exits the switch block immediately once a matching case's code has run; omitting it causes execution to 'fall through' into the next case's code as well, which is occasionally intentional but is a very common source of bugs when forgotten. Always add break unless fall-through is a deliberate part of your design.
Example: The break Statement
#include <iostream>
int main() {
int day = 1;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
case 2:
std::cout << "Falls through to Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using the default Case
The default case catches any value that didn't match one of the explicit case labels, functioning as the switch statement's equivalent of a final else in an if-else ladder. Including a default case is good practice even when you don't expect unmatched values, since it makes unexpected input visible instead of silently ignored.
Example: Using the default Case
#include <iostream>
int main() {
int day = 9;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
default:
std::cout << "Invalid day" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Grouping switch Cases
Stacking multiple case labels directly above one shared block, like case 1: case 2: case 3: followed by one block of code, lets several distinct values trigger identical behavior without duplicating that code for each one. This relies on the same fall-through behavior that break normally prevents, used here deliberately.
Example: Grouping switch Cases
#include <iostream>
int main() {
int day = 6;
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
std::cout << "Weekday" << std::endl;
break;
case 6:
case 7:
std::cout << "Weekend" << std::endl;
break;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested switch Statements
Placing a switch statement inside another switch's case is valid for handling layered categorical decisions, such as first branching on a product category and then on a specific product within it. Nested switches can get hard to follow quickly, so it's worth considering whether separate functions would keep the logic clearer.
Example: Nested switch Statements
#include <iostream>
int main() {
int category = 1;
int size = 2;
switch (category) {
case 1:
switch (size) {
case 1:
std::cout << "Small shirt" << std::endl;
break;
case 2:
std::cout << "Medium shirt" << std::endl;
break;
}
break;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: