switch Statements

   

switch Statements

The next type of control flow is the switch statement. The switch statement is the first control flow statement that does not require a Boolean evaluation. A switch statement consists of a control expression and a block of statements delineated by case labels. The control expression is evaluated and the resulting value determines which case will be executed. Each case is labeled with a unique constant expression that is used in making this determination. Control is passed to the case whose expression matches the value of the control expression. If there is no match, control passes to the default label. If there is no default label, control passes to the first statement after the switch block.

An example of a switch statement is as follows :

 switch (expression){   case C1:          statement1;     break;   case C2:     statement2;     break;   default:          statementD; } 

The control expression that drives a switch statement and the label values for each case must evaluate to a byte, short, char, or int. It is an error to have duplicate case labels, so the compiler must be able to verify that the labels are unique at compile time. For this reason, only constants or constant expressions are allowed in the labels. This is also the reason for the restriction on the data types that are allowed.

The break statement serves an important purpose in a switch. When the block of statements associated with a case has finished executing, execution will continue with the statements for the next case unless a break is encountered . This is useful if you need to perform the same work for multiple values of the switch expression. The order of the case statements is not restricted, so you can arrange them to best support reuse of the same statements. However, if you do not want this behavior, proper use of the break statement is a must. Take a look at the following example:

 switch (1){   case 1:     System.out.println ("one");   case 2:     System.out.println ("two");   case default:     System.out.println("Default"); } 

In this example, the resulting output would be

 one two Default 

This happens because execution falls through each case after the match of case 1 occurs. It is likely, however, that this code was not intended to print all three results. The break statement can be used to only produce the one desired output. To do this, the code should be changed to the following:

 switch (1){   case 1:     System.out.println ("one");     break;   case 2:     System.out.println ("two");     break;   case default:     System.out.println("Default");     break; } 

Note

Notice that blocks are not required for the statements associated with each case because a label identifies the start of each one and a break statement marks the end. Braces can still be used if they aid in readability, but they are purely optional.


It is not necessary for a case to specify any statements at all if it's only intended to execute the statements defined for another case that follows. The next example demonstrates how statements are reused for multiple case labels:

 switch (strike) {   case 0:   case 1:     System.out.println("Got a 0 or 1, want to do the same thing");     break;   case 2:     System.out.println("Got a 2, do something different");     break;   default:     System.out.println("Value out of range"); } 

You should adopt the practice of always defining a default label for your switch statements. Even if you do not need to perform any processing for values that do not match the other cases, a default label with an appropriate comment makes this intent clear to anyone reading your code. If you never expect to see values that do not match a case, a default label is essential so that you can throw an exception or at least write out a warning message if it happens. Although the default label is typically specified last in a switch statement, it can appear anywhere in the sequence of cases.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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