C++ Tutorial
C++ Switch
switch picks a case from an integer or enum. Remember break, or execution falls through.
One value, several labels
A switch compares an integer (or an enum) against a list of case labels. When a label matches, execution jumps there. It is a menu: the program looks at one number and picks a branch.
You cannot switch on a string in standard C++. For text, use if / else if. For ranges such as score >= 80, use if as well. Switch wants exact matches.
A complete program
Each case ends with break; so the program leaves the switch after that branch.default runs when no label matches.
Example
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
default:
cout << "another day" << endl;
break;
}
return 0;
}Change day in /cpp/try. Try 1, 3, and 9 to see a matching case anddefault.
case, default, break
case n:is a label.nmust be a constant integer expression.default:is the fallback. Put it last. You can omit it if every value is covered.break;jumps out of the switch. Without it, execution continues into the next case.
The parentheses after switch hold the value you test. The braces after that hold every case. Case labels themselves do not use extra parentheses.
Fall-through
If you forget break, C++ does not stop at the end of that case. It keeps running the statements in the following cases until it hits a break or the closing brace. That is called fall-through. It is almost always a bug.
Example
#include <iostream>
using namespace std;
int main() {
int day = 1;
switch (day) {
case 1:
cout << "Mon" << endl;
case 2:
cout << "Tue" << endl;
break;
default:
cout << "other" << endl;
break;
}
return 0;
}This program prints both Mon and Tue because case 1 has no break. Add a break after every case unless you grouped empty labels on purpose.
Sharing one body
Stacking labels without statements between them is the one common, intentional fall-through. Several values run the same block. Put a comment on the empty cases so the next reader knows it is deliberate.
Example
#include <iostream>
using namespace std;
int main() {
int day = 6;
switch (day) {
case 6:
case 7:
cout << "weekend" << endl;
break;
default:
cout << "weekday" << endl;
break;
}
return 0;
}switch versus if
| switch | if / else if | |
|---|---|---|
| Best for | Exact integer or enum values | Ranges and combinations |
| Strings | No | Yes |
| Easy miss | Forgotten break | Wrong order of tests |
Enums belong with switch. You will meet enum later; the shape is the same: one value, named cases, a break on each branch.
What to remember
- Switch on integers and enums, not strings.
- End each case with
breakunless empty labels share a body. - Use
defaultfor unexpected values.
Next: while — repeating a block while a condition stays true.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Solid, liquid, gas
Water’s common states at 1 atm are ice, liquid, and steam. A coded sample type is an integer because classic switch wants an integer (or enum). 1, 2, 3 are arbitrary; the names in the comments are not.
Forget break and execution falls into the next case. That is a bug here, not a feature.
Example
#include <iostream>
using namespace std;
int main() {
int state = 2; // 1 solid, 2 liquid, 3 gas
switch (state) {
case 1:
cout << "solid — ice" << endl;
break;
case 2:
cout << "liquid — water" << endl;
break;
case 3:
cout << "gas — steam" << endl;
break;
default:
cout << "unknown" << endl;
}
return 0;
}Astronomy
Planet index
Old catalogues numbered planets from the Sun: 3 Earth, 4 Mars, 5 Jupiter. Pluto is not in this switch, which is historically accurate for a 2006-and-after list and also a reminder that default exists.
Example
#include <iostream>
using namespace std;
int main() {
int n = 4;
switch (n) {
case 3:
cout << "Earth" << endl;
break;
case 4:
cout << "Mars" << endl;
break;
case 5:
cout << "Jupiter" << endl;
break;
default:
cout << "not in this list" << endl;
}
return 0;
}