Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed to Applicability and updated the references to Java 7

...

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

Applicability

Failure to include break statements can cause unexpected control flow.

MSC53-EX0: The break statement at the end of the final case in a switch statement may be omitted. By convention, this is the default label. 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. Nevertheless, the final case in a switch statement should end with a break statement in accordance with good programming style (see [Rogue 2000]).

MSC53-EX1: When multiple cases require execution of identical code, then break statements may be omitted from all cases except the last one. For example:

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:        // break not required
  case 12:        // break not required
  case 13: 
    value = 10; 
    break;        // break required
  default: 
    // Handle Error Condition 
}

MSC09-EX2: When a case ends with a return or throw statement, the break statement may be omitted.

Risk Assessment

Failure to include break statements can cause unexpected control flow.

...

Rule

...

Severity

...

Likelihood

...

Remediation Cost

...

Priority

...

Level

...

MSC53-JG

...

medium

...

unlikely

...

low

...

P6

...

Related Guidelines

...

CERT C Secure Coding Standard

...

MSC17-C. Finish every set of statements associated with a case label with a break statement

...

CERT C++ Secure Coding Standard

...

ISO/IEC TR 24772:2010

"CLL Switch Statements and Static Analysis"

MITRE CWE

CWE-484, "Omitted Break Statement in Switch"

Bibliography

 

...

.

...