Decision and Loop Statements in Microsoft Visual C++
- 8/15/2013
- Making decisions by using the if statement
- Making decisions by using the switch Statement
- Performing loops
- Quick reference
Making decisions by using the switch Statement
Now that you have seen how the if statement works, let’s take a look at the switch statement. Using the switch statement, you can test a single variable and execute one of several branches depending on the variable’s value.
Defining simple switch statements
The example that follows 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("Triangle"); break; case 4: Console::Write("Quadrilateral"); break; case 5: Console::Write("Pentagon"); break; case 6: Console::Write("Hexagon"); break; case 7: Console::Write("Septagon"); break; case 8: Console::Write("Octagon"); break; case 9: Console::Write("Nonagon"); break; case 10: Console::Write("Decagon"); break; default: Console::Write("Polygon"); break; }
The switch keyword is followed by an expression in parentheses. This expression must evaluate to an integer, a character, or an enumeration value. The body of the switch consists of a series of case branches, each of which comprises the keyword case, a value, and a colon.
The value identifying a case branch must be a constant of integer type. This means that integer numbers, enumeration values, and characters are allowed. For example, 5 and a are valid, but abc is not because it is a string literal.
Each case branch can contain any number of statements. At the end of each branch, use a break statement to exit the switch statement.
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.
In this exercise, you will enhance your Calendar Assistant application to display the month as a string such as January or February.
Continue working with the project from the previous exercise.
Modify the DisplayDate function. Rather than display the month as an integer, replace the Console::Write(month) statement with a switch statement that displays the month as a string.
switch (month) { case 1: Console::Write("January"); break; case 2: Console::Write("February"); break; case 3: Console::Write("March"); break; case 4: Console::Write("April"); break; case 5: Console::Write("May"); break; case 6: Console::Write("June"); break; case 7: Console::Write("July"); break; case 8: Console::Write("August"); break; case 9: Console::Write("September"); break; case 10: Console::Write("October"); break; case 11: Console::Write("November"); break; case 12: Console::Write("December"); break; default: Console::Write("Unknown"); break; }
Build the application.
Run the application several times, typing a different month each time. Verify that the application displays the correct month name each time.
Using 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. This can be useful to avoid duplication of code, but be careful not to do it accidentally.
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("Vowel"); break; default: Console::Write("Consonant"); break; }
There is no break statement in the first four case labels. As a result, the 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.
In this exercise, you will enhance your Calendar Assistant application to display the season for the user’s date.
Continue working with the project from the previous exercise.
Modify the DisplayDate function. After displaying the year, month, and day, add the following code after the line Console::Write(day) to display the season:
switch (month) { case 12: case 1: case 2: Console::WriteLine(" [Winter]"); break; case 3: case 4: case 5: Console::WriteLine(" [Spring]"); break; case 6: case 7: case 8: Console::WriteLine(" [Summer]"); break; case 9: case 10: case 11: Console::WriteLine(" [Fall]"); break; }
Build the application.
Run the application several times, typing a different month each time. Verify that the application displays the correct season name each time.