Selections in C++: switch Statements

A switch statement executes statements based on the value of a variable or an expression.

The if statement in Listing 3.3, ComputeTax.cpp, makes selections based on a single true or false condition. There are four cases for computing taxes, which depend on the value of status. To account fully for all the cases, nested if statements were used. Overusing nested if statements makes a program difficult to read. C++ provides a switch statement to sim­plify coding for multiple cases. You may write the following switch statement to replace the nested if statement in Listing 3.3:

switch (status)

{

case 0: compute tax for single filers;

break;

case 1: compute tax for married jointly or qualifying widow(er);

break;

case 2: compute tax for married filing separately;

break;

case 3: compute tax for head of household;

break;

default: cout << “Error: invalid status” << endl;

}

The flowchart of the preceding switch statement is shown in Figure 3.5.

This statement checks to see whether the status matches the value 0, 1, 2, or 3, in that order. If there is a match, the corresponding tax is computed; if not, a message is displayed. Here is the full syntax for the switch statement:

switch (switch-expression)

{

case valuel: statement(s)1;

break;

case value2: statement(s)2;

break;

….

case valueN: statement(s)N;

break;

default:     statement(s)-for-default;

}

The switch statement observes the following rules:

  • The switch-expression must yield an integral value and always be enclosed in parentheses.
  • The valuel, . . . , and valueN are integral constant expressions, meaning that they cannot contain variables, such as 1 + x. These values are integers and cannot be floating-point values.
  • When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached.
  • The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.
  • The keyword break is optional. The break statement immediately ends the switch statement.

Now let us write a program to determine the Chinese Zodiac sign for a given year. The Chinese Zodiac is based on a twelve-year cycle, each year being represented by an animal: rat, ox, tiger, rabbit, dragon, snake, horse, sheep, monkey, rooster, dog, and pig, in this cycle, as shown in Figure 3.6 (next page).

Note that year % 12 determines the Zodiac sign. 1900 is the year of the rat since 1900 % 12 is 4. Listing 3.8 gives a program that prompts the user to enter a year and displays the animal for the year.

Listing 3.8 ChineseZodiac.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6     cout << “Enter a year: “;

7     int year;

8     cin >> year;

9

10    switch (year % 12)

11    {

12          case 0: cout << “monkey” << endl; break;

13          case 1: cout << “rooster” << endl; break;

14          case 2: cout << “dog” << endl; break;

15          case 3: cout << “pig” << endl; break;

16          case 4: cout << “rat” << endl; break;

17          case 5: cout << “ox” << endl; break;

18          case 6: cout << “tiger” << endl; break;

19          case 7: cout << “rabbit” << endl; break;

20          case 8: cout << “dragon” << endl; break;

21          case 9: cout << “snake” << endl; break;

22          case 10: cout << “horse” << endl; break;

23          case 11: cout << “sheep” << endl; break;

24      }

25

26       return 0;

27 }

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *