Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Grammar fixes in the exceptions; added a non-normative recommendation in EX1 saying that good style suggests that the exception should be ignored. So it's permissible, but we don't recommend it.

...

Code Block
bgColor#CCCCFF
int card = 11;

switch (card) {
  /* ... */
  case 11: 
    System.out.println("Jack");
    break;
  case 12: 
    System.out.println("Queen"); 
    break;
  case 13: 
    System.out.println("King"); 
    break;
  default: 
    System.out.println("Invalid Card"); 
    break;
}

Exceptions

Wiki Markup
*MSC14-EX1*:
The last label in a switch statement does not require a break statement. The break statement serves to skip to the end of the switch block, so control transfers to statements following the switch block irrespective of its presence. Conventionally, the last label is the default label.
 The {{break}} statement at the end of the final case in a {{switch}} statement may be omitted. The {{break}} statement serves to transfer control to the end of the {{switch}} block; fall-through behavior also causes control to arrive at the end of the {{switch}} block. Consequently, control transfers to the statements following the {{switch}} block without regard to the presence or absence of the {{break}} statement. Conventionally, the last label is the {{default}} label. Nevertheless, the final case in a {{switch}} statement should end with a {{break}} statement, in accordance with good programming style (see \[[Rogue 2000|AA. Bibliography#Rogue 00]\]).

MSC14-EX2: A break statement may be omitted when multiple cases require execution of identical code. Such instances must be explicitly documented. For example:MSC14-EX2: When it is required to execute the same code for multiple cases, it is permissible to omit the break statement. However, these instances must be explicitly documented.

Code Block
bgColor#CCCCFF
int card = 11;
int value;

// Cases 11,12,13 fall through to the same case 
switch (card) {
  // MSC13-J:EX2: these three cases are treated identically 
  case 11: 
  case 12: 
  case 13: 
    value = 10; 
    break;
  default: 
    // Handle Error Condition 
}

MSC14-EX3: A case needs no break statement if its last statement is When a case ends with a return or throw statement, the break statement may be omitted.

Risk Assessment

Failure to include break statements may cause unexpected control flow.

...

Wiki Markup
\[[JLS 2005|AA. Bibliography#JLS 05]\] [Section 14.11 The switch Statement|http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.11]
\[[Rogue 2000|AA. Bibliography#Rogue 00]\] [The Elements of Java Style|http://www.ambysoft.com/books/elementsJavaStyle.html], Rule 78.

...

MSC13-J. Do not modify the underlying collection when an iteration is in progress      49. Miscellaneous (MSC)      MSC15-J. Use numerical comparison operators to terminate a loop whose counter changes by more than one