C++ Switch
When you compare one value against many fixed options, a long if/else if chain becomes repetitive. The switch statement expresses the same idea more compactly.
Basic syntax
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday\n";
break;
case 2:
cout << "Tuesday\n";
break;
case 3:
cout << "Wednesday\n";
break;
default:
cout << "Some other day\n";
}
}Wednesday
How it works:
- The expression in
switch (...)is evaluated once. - Execution jumps to the
caselabel with a matching value. - It runs until a
break(or the end of the switch). - If no case matches, the
defaultlabel runs (if present).
The switched value must be an integer, a char or an enum — not a string or a double. Case labels must be compile-time constants.
Fall-through
If you forget break, execution "falls through" into the next case. Usually that is a bug, but it can be used on purpose to group cases:
#include <iostream>
using namespace std;
int main() {
char letters[] = {'a', 'x', 'E'};
for (char c : letters) {
switch (c) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
cout << c << " is a vowel\n";
break;
default:
cout << c << " is a consonant\n";
}
}
}a is a vowel
x is a consonant
E is a vowel
Here is what an accidental fall-through looks like:
#include <iostream>
using namespace std;
int main() {
int level = 1;
switch (level) {
case 1:
cout << "Easy\n"; // no break!
case 2:
cout << "Medium\n";
break;
case 3:
cout << "Hard\n";
break;
}
}Easy
Medium
Tip: When fall-through is intentional and a case contains code, mark it with
[[fallthrough]];so readers (and the compiler's-Wimplicit-fallthroughwarning) know it is on purpose.
A small calculator
switch works well with char operators:
#include <iostream>
using namespace std;
int main() {
double a = 12, b = 4;
char ops[] = {'+', '-', '*', '/', '%'};
for (char op : ops) {
cout << a << " " << op << " " << b << " = ";
switch (op) {
case '+': cout << a + b << "\n"; break;
case '-': cout << a - b << "\n"; break;
case '*': cout << a * b << "\n"; break;
case '/': cout << a / b << "\n"; break;
default: cout << "unknown operator\n";
}
}
}12 + 4 = 16
12 - 4 = 8
12 * 4 = 48
12 / 4 = 3
12 % 4 = unknown operator
Declaring variables inside a case
If you need a local variable inside one case, wrap the case body in braces. Otherwise the compiler complains that a jump "crosses initialization":
switch (choice) {
case 1: {
int bonus = 10; // OK: scoped to this block
cout << bonus;
break;
}
case 2:
cout << "two";
break;
}switch vs if
Use switch when… |
Use if when… |
|---|---|
| comparing one integral/enum value to constants | testing ranges (x > 10) |
| there are many discrete options | combining several conditions with &&/|| |
| you want the compiler to warn about missing enum cases | comparing strings or doubles |
Test yourself
What happens when a case has no break?
- Compile error
- The switch ends
- Execution falls through to the next case
- The default case runs
Which type can NOT be used in switch (...)?
- int
- char
- enum
- std::string
When does the default label run?
- Always first
- Always last
- When no case matches
- Only if it is the first label