Friday, November 18, 2011

Java (switch Statements)


switch Statements

The if statement in Listing 3.4 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 fully account for all the cases, nested if statements were used. Overuse of nested if statements makes a program difficult to read. Java provides a switch statement to handle multiple conditions efficiently. You could write the following switch statement to replace the nested if statement in Listing 3.4:
switch (status) {
  case 0: compute taxes for single filers;
          break;
  case 1: compute taxes for married file jointly;
          break;
  case 2: compute taxes for married file separately;
          break;
  case 3: compute taxes for head of household;
          break;
  default: System.out.println("Errors: invalid status");
           System.exit(0);

}

The flow chart of the preceding switch statement is shown in Figure 3.8.




Figure 3.8. The switch statement checks all cases and executes the statements in the matched case.


This statement checks to see whether the status matches the value 0, 1, 2, or 3, in that order. If matched, the corresponding tax is computed; if not matched, a message is displayed. Here is the full syntax for the switch statement:
switch (switch-expression) {
  case value1: 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 a value of char, byte, short, or int type and must always be enclosed in parentheses.
  • The value1, . . ., and valueN must have the same data type as the value of the switch-expression. Note that value1, . . ., and valueN are constant expressions, meaning that they cannot contain variables in the expression, such as 1 + x.
    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 keyword break is optional. The break statement immediately ends the switch statement.
  • The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.
  • The case statements are checked in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end.

Caution


Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This phenomenon is referred to as the fall-through behavior. For example, the following code prints character a three times if ch is 'a':
switch (ch) {
  case 'a': System.out.println(ch);
  case 'b': System.out.println(ch);
  case 'c': System.out.println(ch);
}





Tip


To avoid programming errors and improve code maintainability, it is a good idea to put a comment in a case clause if break is purposely omitted.

No comments:

Post a Comment