C++ switch Statement
In this page:
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no case matches
}
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 std::cout and std::cin
#include <iostream>
// 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 1:
std::cout << "Monday" << std::endl;
// Leave the switch/loop
break;
case 2:
std::cout << "Tuesday" << std::endl;
// Leave the switch/loop
break;
case 3:
std::cout << "Wednesday" << std::endl;
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 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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare day and set it to 1
int day = 1;
// Choose a branch based on the value of day
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
case 2:
std::cout << "Falls through to Tuesday" << std::endl;
// Leave the switch/loop
break;
case 3:
std::cout << "Wednesday" << std::endl;
// 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
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare day and set it to 9
int day = 9;
// Choose a branch based on the value of day
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
// Leave the switch/loop
break;
// default runs when no case matches
default:
std::cout << "Invalid day" << std::endl;
}
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare day and set it to 6
int day = 6;
// Choose a branch based on the value of day
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
std::cout << "Weekday" << std::endl;
// Leave the switch/loop
break;
case 6:
case 7:
std::cout << "Weekend" << std::endl;
// 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
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare category and set it to 1
int category = 1;
// Declare size and set it to 2
int size = 2;
// Choose a branch based on the value of category
switch (category) {
case 1:
switch (size) {
case 1:
std::cout << "Small shirt" << std::endl;
// Leave the switch/loop
break;
case 2:
std::cout << "Medium shirt" << std::endl;
// Leave the switch/loop
break;
}
break;
}
// 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 non-constant or string in a
case, when case labels must be integral constants. - Declaring a variable inside a case without braces, which can cause a 'jump to case label' error.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: