What switch Is For
A switch compares one value against a list of fixed options and runs the branch that matches. When you catch yourself writing a long if/else if chain that keeps testing the same variable against different constants, a switch expresses that intent more clearly - and often compiles to a faster jump table.
It only does equality checks against compile-time constants on an integer-like value. It cannot test ranges, compare strings, or combine conditions. For any of those, stay with if/else.
The Basic switch
You give switch a value, then list case labels for the values you care about. Each branch ends with break:
The value 3 matches case 3:, so "Wednesday" prints and break jumps out of the switch. The default branch is the catch-all - it runs when no case matches. It is optional, but include it unless you are certain every value is handled.
Note the case labels are bare constants followed by a colon, not conditions. You write case 3:, never case day == 3:.
break and Fall-Through
This is the single most important switch gotcha in C++. After a case matches, execution does not stop at the next case - it keeps running straight through until it hits a break or the closing brace. Watch what happens when the break statements are missing:
You might expect just "one". Instead all four lines print: matching case 1: enters the switch there and then falls through every label below it. Add a break; after each branch and you get the single line you wanted. Forgetting a break is the classic source of "why is my switch running too much code?" bugs.
Intentional Fall-Through to Group Cases
Fall-through is not always a mistake - it is the idiomatic way to make several cases share one body. Leave the cases empty (no statements, no break) and they all flow into the next block:
'A', 'B', and 'C' all land on the same "Pass" line because the first two cases are empty and fall through into the third. This is clean and intended. When you do mean to fall through after running some code, document it with a comment - or in C++17 and later, the [[fallthrough]]; attribute, which tells the compiler "yes, I meant this" and silences fall-through warnings.
Switching on enums
switch pairs naturally with enums, since an enum is exactly "one of a fixed set of values". Compilers can also warn you if you forget to handle one of the enumerators:
With a scoped enum class you must qualify each label (Direction::East). Because every enumerator is covered, no default is needed - and many compilers will warn if you later add a fifth direction and forget to add its case. That compiler help is a big reason to prefer switch over if/else chains for enums.
A Gotcha: Declarations Inside a case
You cannot declare a variable with an initializer in one case and have it visible across the other cases without scoping it. This is a common compile error:
switch (x) {
case 1:
int n = 10; // error: jump to case 2 bypasses this initialization
cout << n;
break;
case 2:
cout << "two";
break;
}
The compiler rejects it because falling into case 2: would skip past the initialization of n while n is still in scope. The fix is to wrap the case body in its own braces, giving the variable a block of its own:
Whenever a case needs its own local variable, give it braces. It also keeps the variable from leaking into the cases below it.
Next: for-loop
switch and if let your program choose which code to run once. But plenty of work means doing the same thing many times - counting, iterating over a list, repeating until a condition changes. The for loop is the workhorse for that, and it is the next page.
Frequently Asked Questions
When should I use switch instead of if-else in C++?
Use switch when you are comparing one integer-like value against many fixed, constant options - like a menu choice, a day number, or an enum. It reads more clearly than a long if/else if ladder and lets the compiler optimize the dispatch. Stick with if/else when your conditions involve ranges (x > 10), floating-point values, strings, or multiple variables - switch cannot do any of those.
Why do I need break in a C++ switch statement?
Once a case matches, C++ keeps executing the following cases until it hits a break or the end of the switch. This is called fall-through. break stops it. Forgetting break is a classic bug - you match case 1: and accidentally also run the code for case 2, case 3, and the default.
What types can you switch on in C++?
Only integral or enumeration types: int, char, short, long, bool, scoped/unscoped enum, and anything that converts to one of those. You cannot switch on a double, float, or std::string - use if/else for those. Each case label must be a compile-time constant.