13.2. switch statement¶
It’s hard to mention enumerated types without mentioning switch
statements, because they often go hand in hand. A switch statement
is an alternative to a chained conditional that is syntactically
prettier and often more efficient. It looks like this:
switch (symbol) {
case '+':
perform_addition ();
break;
case '*':
perform_multiplication ();
break;
default:
std::cout << "I only know how to perform addition and multiplication" << std::endl;
break;
}
This switch statement is equivalent to the following chained
conditional:
if (symbol == '+') {
perform_addition ();
}
else if (symbol == '*') {
perform_multiplication ();
}
else {
std::cout << "I only know how to perform addition and multiplication" << std::endl;
}
The break statements are necessary in each branch in a switch
statement because otherwise the flow of execution “falls through” to the
next case.
Note
Be sure to incorporate a break statment into each branch so
that the flow of execution stops after that branch.
Without the break statements, the symbol + would make the program
perform addition, and then perform multiplication, and then print the
error message. Occasionally this feature is useful, but most of the time
it is a source of errors when people forget the break statements.
Take a look at the active code below that allows you to choose your starter Pokemon.
If you change the value of type, it will change the Pokemon you choose. Notice how
if you don't assign type to a valid type, it outputs the default message. Try taking out
the break statements in each case. What happens if you run the code with type as 'g' afterwards?
1#include <iostream>
2#include <string>
3using std::cout;
4
5int main() {
6 char type = 'w';
7
8 switch (type) {
9 case 'g':
10 cout << "You've chosen Bulbasaur!" << std::endl;
11 break;
12 case 'f':
13 cout << "You've chosen Charmander!" << std::endl;
14 break;
15 case 'w':
16 cout << "You've chosen Squirtle!" << std::endl;
17 break;
18 default:
19 cout << "Invalid type! Please try again." << std::endl;
20 break;
21 }
22}
switch statements work with integers, characters, and enumerated
types. For example, to convert a card_suit to the corresponding string,
we could use something like:
switch (suit) {
case CLUBS: return "Clubs";
case DIAMONDS: return "Diamonds";
case HEARTS: return "Hearts";
case SPADES: return "Spades";
default: return "Not a valid suit";
}
In this case we don’t need break statements because the return
statements cause the flow of execution to return to the caller instead
of falling through to the next case.
In general it is good style to include a default case in every
switch statement, to handle errors or unexpected values.
A(n) statement is necessary for each branch in a switch statement.
Which one of the following types do NOT work with switch statement?
What is the correct output of the code below?
int main() {
int num = 2;
switch (num) {
case 1:
cout << 1;
break;
case 2:
cout << 4;
case 3:
cout << 9;
break;
default:
cout << "Invalid num! Please try again.";
break;
}
}
What is the correct output this time?
int main() {
int num = 1;
switch (num) {
case 1:
cout << 1;
break;
case 2:
cout << 4;
case 3:
cout << 9;
default:
cout << "Invalid num! Please try again.";
}
}
And finally, what about this time?
int main() {
int num = 2;
switch (num) {
case 1:
cout << 1;
break;
case 2:
cout << 4;
case 3:
cout << 9;
default:
cout << "Invalid num! Please try again.";
}
}