Suppose you create a screen menu that asks the user to select one of five choices—for example
- Cheap
- Moderate
- Expensive
- Extravagant
- Excessive
You can extend an if else if else sequence to handle five alternatives
- But the C++ switch statement more easily handles selecting a choice from an extended list
- Here’s the general form for a switch statement (A-1):
Example of (A-1)
switch (integer-expression)
{
case label1 : statement(s)
case label2 : statement(s)
...
default : statement(s)
}
A C++ switch statement acts as a routing device
- That tells the computer which line of code to execute next
On reaching a switch statement
- A program jumps to the line labeled with the value corresponding to the value of integer-expression
- For example, if integer-expression has the value 4, the program goes to the line that has a case 4: label
The value integer-expression
- As the name suggests, must be an expression that reduces to an integer value
Also, each label
- Must be an integer constant expression
Most often, labels
- Are simple int or char constants, such as 1 or ‘q’, or enumerators
If integer-expression doesn’t match any of the labels
- The program jumps to the line labeled default
- The default label is optional
- If you omit it and there is no match, the program jumps to the next statement following the switch
- See (Image-1)
- If you omit it and there is no match, the program jumps to the next statement following the switch
- The default label is optional
(Image-1) The structure of switch statements

The switch statement
- Is different from similar statements in languages such as Pascal in a very important way
Each C++ case label functions
- Only as a line label, not as a boundary between choices
- That is, after a program jumps to a particular line in a switch
- It then sequentially executes all the statements following that line in the switch unless you explicitly direct it otherwise
- That is, after a program jumps to a particular line in a switch
Execution does not automatically stop at the next case
- To make execution stop at the end of a particular group of statements
- You must use the break statement
- This causes execution to jump to the statement following the switch
- You must use the break statement
(A-2) shows how to use switch and break together to implement a simple menu for executives
- The program uses a showmenu() function to display a set of choices
- A switch statement then selects an action based on the user’s response
Compatibility Note
- Some C++ implementations treat the \a escape sequence (used in case 1 in (A-2)) as silent
Example of (A-2)
// switch.cpp -- using the switch statement #include <iostream> using namespace std; void showmenu(); // function prototypes void report(); void comfort(); int main() { showmenu(); int choice; cin >> choice; while (choice != 5) { switch(choice) { case 1 : cout << "\a\n"; break; case 2 : report(); break; case 3 : cout << "The boss was in all day.\n"; break; case 4 : comfort(); break; default : cout << "That's not a choice.\n"; } showmenu(); cin >> choice; } cout << "Bye!\n"; return 0; } void showmenu() { cout << "Please enter 1, 2, 3, 4, or 5:\n" "1) alarm 2) report\n" "3) alibi 4) comfort\n" "5) quit\n"; } void report() { cout << "It's been an excellent week for business.\n" "Sales are up 150%. Expenses are down 35%.\n"; } void comfort() { cout << "Your employees think you are the finest CEO\n" "in the industry. The board of directors think\n" "you are the finest CEO in the industry.\n"; }
Here is a sample run of the executive menu program in (A-2):

The while loop terminates
- When the user enters 5
Entering 1 through 4
- Activates the corresponding choice from the switch list
Entering 10
- Triggers the default statements
As noted earlier, this program needs the break statements
- To confine execution to a particular portion of a switch statement
- To see that this is so, you can remove the break statements from (A-2) and see how it works afterward
- You’ll find, for example, that entering 2 causes the program to execute all the statements associated with case labels 2, 3, 4, and the default
- C++ works this way because that sort of behavior can be useful
- You’ll find, for example, that entering 2 causes the program to execute all the statements associated with case labels 2, 3, 4, and the default
- To see that this is so, you can remove the break statements from (A-2) and see how it works afterward
For one thing, it makes it simple to use multiple labels
- For example, suppose you rewrote (A-2) using characters instead of integers as menu choices and switch labels
- In that case, you could use both an uppercase and a lowercase label for the same statements (A-3):
Example of (A-3)
// switch.cpp -- using the switch statement #include <iostream> using namespace std; void showmenu(); // function prototypes void report(); void comfort(); int main() { showmenu(); char choice; cin >> choice; while (choice != 'Q' && choice != 'q') { switch(choice) { case 'a': case 'A': cout << "\a\n"; break; case 'r': case 'R': report(); break; case 'l': case 'L': cout << "The boss was in all day.\n"; break; case 'c': case 'C': comfort(); break; default : cout << "That's not a choice.\n"; } showmenu(); cin >> choice; } cout << "Bye!\n"; return 0; } void showmenu() { cout << "Please enter a, r, l, c, or q:\n" "a) alarm r) report\n" "l) alibi c) comfort\n" "q) quit\n"; } void report() { cout << "It's been an excellent week for business.\n" "Sales are up 150%. Expenses are down 35%.\n"; } void comfort() { cout << "Your employees think you are the finest CEO\n" "in the industry. The board of directors think\n" "you are the finest CEO in the industry.\n"; }
(A-3) because there is no break immediately following case ‘a’, program execution passes on to the next line
- Which is the statement following case ‘A’

Discussion
No comments for “The switch Statement”