C++ Switch Statement
Introduction to Switch
The switch
statement is a control structure used to test a variable against a list of constant values (cases).
It is a cleaner and often more efficient alternative to long if-else if
chains when checking a single variable against multiple options.
Basic Syntax
switch (expression) {
case constant1:
// code if expression == constant1
break;
case constant2:
// code if expression == constant2
break;
// ...
default:
// code if no cases match
}
Simple Example
#include <iostream>
int main() {
int day = 4;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";
break;
case 3:
std::cout << "Wednesday";
break;
case 4:
std::cout << "Thursday";
break;
case 5:
std::cout << "Friday";
break;
default:
std::cout << "Weekend day";
}
return 0;
}
Thursday
The break Keyword
The break
statement ends the current case. Without it, execution continues into the next case (fall-through).
#include <iostream>
int main() {
int x = 1;
switch (x) {
case 1:
std::cout << "One";
// no break here -> fall-through into case 2
case 2:
std::cout << "Two";
break;
default:
std::cout << "Other";
}
return 0;
}
OneTwo
Intentional Fall-Through
Sometimes fall-through is used intentionally when multiple cases share the same code. In C++17+, use the [[fallthrough]]
attribute to document intent.
#include <iostream>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
[[fallthrough]]; // explicitly document fall-through
case 'B':
[[fallthrough]];
case 'C':
std::cout << "Pass";
break;
case 'D':
std::cout << "Marginal";
break;
case 'F':
std::cout << "Fail";
break;
default:
std::cout << "Invalid grade";
}
return 0;
}
Pass
The default Case
The default
case executes when no other case matches. It is optional but recommended for robustness.
#include <iostream>
int main() {
int option = 99;
switch (option) {
case 1:
std::cout << "Option 1";
break;
case 2:
std::cout << "Option 2";
break;
default:
std::cout << "Invalid option";
}
return 0;
}
Invalid option
Switch with Enums
Switch works well with enumerated types. With scoped enums (enum class
), qualify enumerators with the enum type.
#include <iostream>
enum class Color { Red, Green, Blue };
int main() {
Color c = Color::Green;
switch (c) {
case Color::Red:
std::cout << "Red";
break;
case Color::Green:
std::cout << "Green";
break;
case Color::Blue:
std::cout << "Blue";
break;
}
return 0;
}
Green
C++17 Initialization in Switch
C++17 allows an init-statement directly inside a switch header. The declared variable is scoped to the switch.
#include <iostream>
int getValue() { return 2; }
int main() {
switch (int value = getValue(); value) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
default:
std::cout << "Other";
}
}
Real-World Example: Menu System
Switch statements are commonly used for menu-driven programs.
#include <iostream>
int main() {
int choice;
std::cout << "1. Add\n2. Subtract\n3. Multiply\nEnter choice: ";
std::cin >> choice;
int a = 10, b = 5;
switch (choice) {
case 1:
std::cout << "Result: " << a + b;
break;
case 2:
std::cout << "Result: " << a - b;
break;
case 3:
std::cout << "Result: " << a * b;
break;
default:
std::cout << "Invalid choice";
}
return 0;
}
1. Add 2. Subtract 3. Multiply Enter choice: 2 Result: 5
Limitations of Switch
- Only checks for equality (no ranges or complex conditions).
- Case values must be constant expressions.
- Cannot switch directly on strings (use if-else, maps, hashing, or map to an enum).
- Limited to integral and enumeration types (not floating point or class types).
// Invalid example:
/*
#include <string>
std::string color = "red";
switch (color) { // Error: not allowed
case "red":
// ...
}
*/
Best Practices
- Use break
unless fall-through is intentional.
- Document intentional fall-through (prefer [[fallthrough]]
in C++17+).
- Keep case blocks concise—extract complex logic into functions.
- Prefer switch over long if-else chains for constant equality checks.
- Use enum class
for stronger type safety and qualified enumerators.
- Ensure all case labels are unique; unreachable or duplicate labels are ill-formed.