Home > Articles

This chapter is from the book

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 cpe.jpg 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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020