Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

A switch block comprises several case labels and an optional but highly recommended default label. By convention, statements that follow each case label end with a break statement, responsible for transferring the control to the end of the switch block. When omitted, the statements in the subsequent case label get are executed. Because the break statement is optional, its omission produces no compiler warnings. If this behavior is unintentional, it can cause unexpected control flow.

...

EX1: The last label in a switch statement requires no breakdoes 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.

...

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 
}

...