3.
Build the application.
4.
Run the application several times, typing a different month each time. Verify that the applica-
tion 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.
Do'stlaringiz bilan baham: |