- 4.1 Introduction
- 4.2 Essentials of Counter-Controlled Iteration
- 4.3 for Iteration Statement
- 4.4 Examples Using the for Statement
- 4.5 Application: Summing Even Integers
- 4.6 Application: Compound-Interest Calculations
- 4.7 do... while Iteration Statement
- 4.8 switch Multiple-Selection Statement
- 4.9 C++17 Selection Statements with Initializers
- 4.10 break and continue Statements
- 4.11 Logical Operators
- 4.12 Confusing the Equality (==) and Assignment (=) Operators
- 4.13 Objects-Natural Case Study: Using the miniz-cpp Library to Write and Read ZIP files
- 4.14 C++20 Text Formatting with Field Widths and Precisions
- 4.15 Wrap-Up
4.8 switch Multiple-Selection Statement
C++ provides the switch multiple-selection statement to choose among many different actions based on the possible values of a variable or expression. Each action is associated with the value of an integral constant expression—any combination of character and integer constants that evaluates to a constant integer value.
Using a switch Statement to Count A, B, C, D and F Grades
Figure 4.6 calculates the class average of a set of numeric grades entered by the user. The switch statement determines each grade’s letter equivalent (A, B, C, D or F) and increments the appropriate grade counter. The program also displays a summary of the number of students who received each grade.
1 // fig04_06.cpp 2 // Using a switch statement to count letter grades. 3 #include <iostream> 4 #include <iomanip> 5 using namespace std; 6 7 int main() { 8 int total{0}; // sum of grades 9 int gradeCounter{0}; // number of grades entered 10 int aCount{0}; // count of A grades 11 int bCount{0}; // count of B grades 12 int cCount{0}; // count of C grades 13 int dCount{0}; // count of D grades 14 int fCount{0}; // count of F grades 15 16 cout << "Enter the integer grades in the range 0-100.\n" 17 << "Type the end-of-file indicator to terminate input:\n" 18 << " On UNIX/Linux/macOS type <Ctrl> d then press Enter\n" 19 << " On Windows type <Ctrl> z then press Enter\n"; 20 21 int grade; 22 23 // loop until user enters the end-of-file indicator 24 while (cin >> grade) { 25 total += grade; // add grade to total 26 ++gradeCounter; // increment number of grades 27 28 // increment appropriate letter-grade counter 29 switch (grade / 10) { 30 case 9: // grade was between 90 31 case 10: // and 100, inclusive 32 ++aCount; 33 break; // exits switch 34 35 case 8: // grade was between 80 and 89 36 ++bCount; 37 break; // exits switch 38 39 case 7: // grade was between 70 and 79 40 ++cCount; 41 break; // exits switch 42 43 case 6: // grade was between 60 and 69 44 ++dCount; 45 break; // exits switch 46 47 default: // grade was less than 60 48 ++fCount; 49 break; // optional; exits switch anyway 50 } // end switch 51 } // end while 52 53 // set floating-point number format 54 cout << fixed << setprecision(2); 55 56 // display grade report 57 cout << "\nGrade Report:\n"; 58 59 // if user entered at least one grade... 60 if (gradeCounter != 0) { 61 // calculate average of all grades entered 62 double average{static_cast<double>(total) / gradeCounter}; 63 64 // output summary of results 65 cout << "Total of the " << gradeCounter << " grades entered is " 66 << total << "\nClass average is " << average 67 << "\nNumber of students who received each grade:" 68 << "\nA: " << aCount << "\nB: " << bCount << "\nC: " << cCount 69 << "\nD: " << dCount << "\nF: " << fCount << "\n"; 70 } 71 else { // no grades were entered, so output appropriate message 72 cout << "No grades were entered" << "\n"; 73 } 74 }
Enter the integer grades in the range 0-100. Type the end-of-file indicator to terminate input: On UNIX/Linux/macOS type <Ctrl> d then press Enter On Windows type <Ctrl> z then press Enter 99 92 45 57 63 71 76 85 90 100 ^Z
Grade Report: Total of the 10 grades entered is 778 Class average is 77.80 Number of students who received each grade: A: 4 B: 1 C: 2 D: 1 F: 2
Fig. 4.6 | Using a switch statement to count letter grades.
Figure 4.6 declares local variables total (line 8) and gradeCounter (line 9) to keep track of the sum of the grades entered by the user and the number of grades entered. Lines 10–14 declare and initialize to 0 counter variables for each grade category. Lines 24–51 input an arbitrary number of integer grades using sentinel-controlled iteration, update variables total and gradeCounter, and increment an appropriate letter-grade counter for each grade entered. Lines 54–73 output a report containing the total of all grades entered, the average grade and the number of students who received each letter grade.
Reading Grades from the User
Lines 16–19 prompt the user to enter integer grades or type the end-of-file indicator to terminate the input. The end-of-file indicator is a system-dependent keystroke combination used to indicate that there’s no more data to input. In Chapter 8, you’ll see how the end-of-file indicator is used when a program reads its input from a file.
The keystroke combinations for entering end-of-file are system dependent. On UNIX/Linux/macOS systems, type the sequence
<Ctrl> d
on a line by itself. This notation means to press both the Ctrl key and the d key simultaneously. On Windows systems, type
<Ctrl> z
On some systems, you must also press Enter. Also, Windows typically displays ^Z on the screen when you type the end-of-file indicator, as shown in the output of Fig. 4.6.
The while statement (lines 24–51) obtains the user input. Line 24
while (cin >> grade) {
performs the input in the while statement’s condition. In this case, the loop-continuation condition evaluates to true if cin successfully reads an int value. If the user enters the end-of-file indicator, the condition evaluates to false.
If the condition is true, line 25 adds grade to total, and line 26 increments grade-Counter. These are used to compute the average. Next, lines 29–50 use a switch statement to increment the appropriate letter-grade counter based on the numeric grade entered.
Processing the Grades
The switch statement (lines 29–50) determines which counter to increment. We assume that the user enters a valid grade in the range 0–100. A grade in the range 90–100 represents A, 80–89 represents B, 70–79 represents C, 60–69 represents D and 0–59 represents F. The switch statement’s block contains a sequence of case labels and an optional default case, which can appear anywhere in the switch, but normally appears last. These are used in this example to determine which counter to increment based on the grade.
When the flow of control reaches the switch, the program evaluates the controlling expression in the parentheses (grade / 10) following keyword switch. The program compares this expression’s value with each case label. The expression must have a signed or unsigned integral type—bool, char, char8_t, char16_t, char32_t, wchar_t, int, long or long long.
The controlling expression in line 29 performs integer division, which truncates the fractional part of the result. When we divide a value from 0 to 100 by 10, the result is always a value from 0 to 10. We use several of these values in our case labels. If the user enters the integer 85, the controlling expression evaluates to 8. The switch compares 8 with each case label. If a match occurs (case 8: at line 35), that case’s statements execute. For 8, line 36 increments bCount, because a grade in the 80s is a B. The break statement (line 37) exits the switch. In this program, we reach the end of the while loop, so control returns to the loop-continuation condition in line 24 to determine whether the loop should continue executing.
The cases in our switch explicitly test for the values 10, 9, 8, 7 and 6. Note the cases at lines 30–31 that test for the values 9 and 10 (both of which represent the grade A). Listing cases consecutively in this manner with no statements between them enables the cases to perform the same set of statements—when the controlling expression evaluates to 9 or 10, the statements in lines 32–33 execute. The switch statement does not provide a mechanism for testing ranges of values, so every value you need to test must be listed in a separate case label. Each case can have multiple statements. The switch statement differs from other control statements in that it does not require braces around multiple statements in a case, unless you need to declare a variable in a case.
case without a break Statement—C++17 [[fallthrough]] Attribute
Without break statements, each time a match occurs in the switch, the statements for that case and subsequent cases execute until a break statement or the end of the switch is reached. This is referred to as “falling through” to the statements in subsequent cases.4
Err 17 Forgetting a break statement when one is needed is a logic error. To call your attention to this possible problem, many compilers issue a warning when a case label is followed by one or more statements and does not contain a break statement. For such instances in which “falling through” is the desired behavior, C++17 introduced the [[fallthrough]] attribute. You can tell the compiler that “falling through” to the next case is the correct behavior by placing the statement
[[fallthrough]];
where the break statement would normally appear.
The default Case
If no match occurs between the controlling expression’s value and any of the case labels, the default case (lines 47–49) executes. We use the default case in this example to process all controlling-expression values that are less than 6—that is, all failing grades. If no match occurs and the switch does not contain a default case, program control simply continues with the first statement after the switch. In a switch, it’s good practice to test for all possible values of the controlling expression.
Displaying the Grade Report
Lines 54–73 output a report based on the grades entered. Line 60 determines whether the user entered at least one grade—this helps us avoid dividing by zero, which for integer division causes the program to fail and for floating-point division produces the value nan—for “not a number.” If so, line 62 calculates the average of the grades. Lines 65–69 then output the total of all the grades, the class average and the number of students who received each letter grade. If no grades were entered, line 72 outputs an appropriate message. The output in Fig. 4.6 shows a sample grade report based on 10 grades.
switch Statement UML Activity Diagram
The following is the UML activity diagram for the general switch statement:
Most switch statements use a break in each case to terminate the switch after the case is processed. The diagram emphasizes this by including break statements and showing that the break at the end of a case causes control to exit the switch statement immediately.
The break statement is not required for the switch’s last case (or the optional default case, when it appears last), because execution continues with the next statement after the switch. Provide a default case in every switch statement to focus you on processing exceptional conditions.
Notes on cases
Each case in a switch statement must contain a constant integral expression—that is, any expression that evaluates to a constant integer value. You also can use enum constants (introduced in Section 5.9) and character literals—specific characters in single quotes, such as 'A', '7' or '$', which represent the integer values of characters. (Appendix B shows the integer values of the characters in the ASCII character set, which is a subset of the Unicode character set.)
In Chapter 10, OOP: Inheritance and Runtime Polymorphism, we present a more elegant way to implement switch logic. We use a technique called polymorphism to create programs that are often clearer, easier to maintain and easier to extend than programs using switch logic.