Opening the book…
In this section, we will discuss another selection statement often used in a C++ program: The switch statement.
Statement Structure:
switch(a)
{
case 1:
statements;
case 2:
statements;
case 3:
statements;
---------
default:
statements:
};
Logic of the switch statement:
The statement will compares the value of the variable a with the integer that immediately follows the word case. If we have a match, then all the statements under the first word case as well as the statements under the rest of the cases including the statement under the default will be executed. If the value of the variable a does not equal the integer that follows the word case, the switch statement skips all the statements under the first case and compares the value of the variable a with the integer that follows the second word case. Again, if we have a match, then all the statements under the second case and the statement under the rest of cases will be executed including the statements under the default. If we don't have a match, the switch statement will skip the statements under the second case, and proceed to compare the value of a with the integer that follows the third word case, and so on. If the value of a does not match any integer that follows the word case, then the switch statement will skip the statements under all the cases and only execute the statements under the default.
Note:
- There is no limit to the number of the cases that we can use in a switch statement
- The integer used in a switch statement doesn't have to be started at 1 and doesn't need to be consecutive.
- The value of the variable of a doesn't have to be integer, it can be a character. However, If the value of a is a character, then we have to use characters in place of integers in the switch statement.
Following is an example of using switch statement in a C++ program.
Example
Use "switch" statement to write a menu driven program that allow a user to selectively perform one of the following tasks:
- Convert the temperature from Celsius to Fahrenheit
- Display Next Month Appointments
- Find My Grade
Additional Information:
- The formula of converting the temperature from the Celsius to Fahrenheit is
Fahrenheit = (9/5.0) * celsius + 32
When a user selects this option, the program should ask user to enter the temperature in Celsius and then display the same temperature in Fahrenheit.
- If a user opt to display his/her next month appointments, the program should display a list of his/her next month appointments
- If a user opt to find his or her class grade, the program should first ask the user to enter four test scores, then calculate the sum. Divide the sum by 4 and use the following scale to display the user's grade:
A: 90 or above, B: 80 - 89 C: 70 - 79, D: 60 - 69, F: 59 or below
Followings are the program's sample runs.
First run:

The second run:

The third run:

The fourth run:

Now, we begin the coding of this program:
First, we use a set of cout statements to display a menu to the user of this program as following:

Then we declare an integer variable option at the beginning of the main(), and use a set of cout and cin statements to prompt the user to provide an input:

Now, we set up a switch statement as following:

We put the code that performs each task in the spaces after each case. For example, we put the code that performs temperature conversion in the space after the case 1:

If the value of the variable option is 1, then all the code under the case 1 will be executed. By default, after all the statements in case 1 are executed, all the statements under the rest of the cases will also be executed including the statements under the default. Thus, we put a break statement at line 23 to prevent the statements under the rest of the cases from being executed.
A screenshot of the completed program is shown as following:

If a user enter 3, then the program will skip the code under the case 1 and case 2 and executes the code under the case 3. After all the statements under the case 3 are executed, the break statement at line 49 will prevent the code under the case 4 and the statements under the default from being executed.
If a user enter an invalid input (an input other than 1, 2, 3, or 4), then only the statements under the default will be executed.
This page titled 3.9: Switch Statements and Menu Driven Programs was last modified on Wed, 24 Dec 2025 20:37:21 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.
The book could not be loaded just now.