Making Decisions with the switch Statement


Making Decisions with the switch Statement

Now that you have seen how the if statement works, let’s take a look at the switch statement. The switch statement enables you to test a single variable and to execute one of several branches depending on its value.

Defining Simple switch Statements

The following illustration shows how the switch statement works.

click to expand

The following example shows the syntax for the switch statement. The switch statement tests the numberOfSides in a shape and displays a message to describe that shape:

int numberOfSides; // Number of sides in a shape ... switch (numberOfSides) { case 3: Console::Write(S"Triangle"); break; case 4: Console::Write(S"Quadrilateral"); break; case 5: Console::Write(S"Pentagon"); break; case 6: Console::Write(S"Hexagon"); break; case 7: Console::Write(S"Septagon"); break; case 8: Console::Write(S"Octagon"); break; case 9: Console::Write(S"Nonagon"); break; case 10: Console::Write(S"Decagon"); break; default: Console::Write(S"Polygon"); break; }

The switch keyword is followed by an expression in parentheses. (The expression must evaluate to an integer-based value, a character, or an enumeration value.) Next a series of case branches are defined within braces.

Note

Each case label specifies a single constant number. You can’t specify multiple values, and you can’t define a range of values.

Each case branch can contain any number of statements. At the end of each branch, use a break statement to exit the switch statement.

Note

There is normally no need to use braces within each case branch. The break statement marks the end of each case branch. However, you’ll need to use braces if you need to declare a variable within the branch code.

You can define an optional default branch in the switch statement. The default branch will be executed if the expression doesn’t match any of the case labels.

Tip

It’s good practice to define a default branch even if you don’t have any specific processing to perform. Including the default branch shows that you haven’t just forgotten it. Also, the default branch can help you trap unexpected values and display a suitable warning to the user.

In this exercise, you will enhance your Calendar Assistant application to display the month as a string such as January, February, and so on.

  1. Continue working with the project from the previous exercise.

  2. Modify the DisplayDate function. Rather than display the month as an integer, use a switch statement to display the month as a string:

    switch (month) { case 1: Console::Write(S"January"); break; case 2: Console::Write(S"February"); break; case 3: Console::Write(S"March"); break; case 4: Console::Write(S"April"); break; case 5: Console::Write(S"May"); break; case 6: Console::Write(S"June"); break; case 7: Console::Write(S"July"); break; case 8: Console::Write(S"August"); break; case 9: Console::Write(S"September"); break; case 10: Console::Write(S"October"); break; case 11: Console::Write(S"November"); break; case 12: Console::Write(S"December"); break; default: Console::Write(S"Unknown"); break; }
  3. Build the program.

  4. Run the program several times, and enter a different month each time. Verify that the program displays the correct month name each time.

Defining Fall-Through in a switch Statement

If you omit the break statement at the end of a case branch, flow of control continues on to the next statement. This process is called fall-through.

The following example illustrates why fall-through might be useful. This example tests a lowercase letter to see if it is a vowel or a consonant:

char lowercaseLetter; // Single lowercase letter, for example ’a’ ... switch (lowercaseLetter) { case ’a’: case ’e’: case ’i’: case ’o’: case ’u’: Console::Write(S"Vowel"); break; default: Console::Write(S"Consonant"); break; }

There is no break statement in the first four case labels. Flow of control passes on to the next executable statement to display the message Vowel. The default branch deals with all the other letters and displays the message Consonant.

Using Fall-Through in a switch Statement

In this exercise, you will enhance your Calendar Assistant application to display the season for the user’s date.

  1. Continue working with the project from the previous exercise.

  2. Modify the DisplayDate function. After displaying the year, month, and day, add the following code to display the season:

    switch (month) { case 12: case 1:     case 2: Console::WriteLine(S" [Winter]"); break; case 3:     case 4:     case 5: Console::WriteLine(S" [Spring]"); break; case 6: case 7: case 8: Console::WriteLine(S" [Summer]"); break; case 9:     case 10: case 11: Console::WriteLine(S" [Fall]"); break; }
  3. Build the program.

  4. Run the program several times, and enter a different month each time. Verify that the program displays the correct season name each time.




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net