Switch Statements
in C++
Master the switch statement for clean multi-way branching, understand fall-through behavior, and learn when to choose switch over if-else chains.
What Is a Switch Statement?
A switch statement evaluates a single expression once and compares its value against a list of constant labels. When a match is found, the program executes the code associated with that label.
Why Use Switch Over If-Else?
When you have multiple discrete values to test for the same variable, a switch statement is often cleaner and can be more efficient. Compilers can optimize switch into a jump table, making lookups constant-time.
Anatomy of a Switch Statement
The Syntax in Detail
Understanding Fall-Through
Fall-through is the default behavior in C++: if you omit the break after a case, execution continues into the next case's code, regardless of whether that case's label matches.
Intentional Fall-Through
Sometimes fall-through is deliberately useful: when multiple cases should execute the same code, you can stack case labels together.
Switch vs If-Else
| switch | if-else | |
|---|---|---|
| Expression type | Integral only (int, char, enum) | Any boolean expression |
| Comparison | Equality check only | Any operator (<, >, !=, ranges) |
| Multiple values | Stack case labels | Use || operators |
| Readability (many cases) | Excellent | Degrades quickly |
| Performance hint | Jump table possible | Sequential checks |
| Range checks | Not supported | Natural fit |
How to Write a Switch Statement
Example 1: Simple Grade Checker
Example 2: Fall-Through in Action
Example 3: Enums with Switch
Variables Inside Cases
Declaring variables inside a case without braces can cause cross-initialization errors. Each case shares the same scope unless you add explicit braces.
case 1: int x = 5; break; case 2: ...Jump to case 2 skips initialization of x
case 1: { int x = 5; break; }Braces create a local scope for x
C++17: [[fallthrough]] Attribute
Modern compilers often warn when a case lacks a break, as this can be an unintentional bug. The [[fallthrough]] attribute explicitly marks intentional fall-through, silencing these warnings and improving code clarity.
Common Mistakes
Summary Cheat Sheet
| Concept | Key Point |
|---|---|
| Expression type | int, char, enum (integral only) |
| Case labels | Must be compile-time constants |
| break | Exits the switch; prevents fall-through |
| Fall-through | Without break, execution continues to next case |
| default | Catches all unmatched values; recommended |
| [[fallthrough]] | C++17 attribute for intentional fall-through |
| Variable scope | Use { braces } in cases with declarations |
Question 1 of 5
What happens if you omit the break statement at the end of a case in a switch block?
Question 2 of 5
Consider this code:
int x = 2; switch (x) { case 1: cout << "A"; case 2: cout << "B"; case 3: cout << "C"; default: cout << "D"; }
What is the output of this program?
Question 3 of 5
Which of the following cannot be used as a switch expression in C++?
Question 4 of 5
What is the purpose of the [[fallthrough]] attribute introduced in C++17?
Question 5 of 5
Consider this code:
switch (n) { case 1: int val = 10; cout << val; break; case 2: cout << val; break; }
What is the problem with this switch statement?