Versions Compared

Key

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

...

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

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.

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 a return or throw.

...

Failure to include break statements may cause unexpected control flow.

Recommendation Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

MSC14-J

medium

unlikely

low

P6

L2

...