A switch
block comprises several case
labels and an optional but highly recommended default
label. Statements that follow each case label must end with a break
statement, which is responsible for transferring the control to the end of the switch
block. When omitted, the statements in the subsequent case
label are executed. Because the break
statement is optional, omitting it produces no compiler warnings. When this behavior is unintentional, it can cause unexpected control flow.
Noncompliant Code Example
In this noncompliant code example, the case wherein the card
is 11 lacks a break
statement. As a result, execution continues with the statements for card = 12
.
Code Block | ||
---|---|---|
| ||
int card = 11; switch (card) { /* ... */ case 11: System.out.println("Jack"); case 12: System.out.println("Queen"); break; case 13: System.out.println("King"); break; default: System.out.println("Invalid Card"); break; } |
Compliant Solution
This compliant solution terminates each case (including the default
case) with a break
statement.
Code Block | ||
---|---|---|
| ||
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 |
---|
*MSC09-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 \[java:[Rogue 2000|AA. Bibliography#Rogue 00]\]). |
...
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 |
---|---|---|---|---|---|
MSC09-J | medium | unlikely | low | P6 | L2 |
Related Guidelines
MSC17-C. Finish every set of statements associated with a case label with a break statement | ||||
MSC18-CPP. Finish every set of statements associated with a case label with a break statement | ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2de66ef68866da5f-c02b53b6-4a03454f-9a10b2cb-b5ebaef717cdfad5d3c53d22"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | "Switch Statements and Static Analysis [java:CLL]" | ]]></ac:plain-text-body></ac:structured-macro> |
CWE-484, "Omitted Break Statement in Switch" |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ec44c26f6d2d9569-11e95324-44854500-a4c68c5b-c7a6751114f76837894ff6d7"><ac:plain-text-body><![CDATA[ | [java:[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] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="032800cf064dfbca-2f4782a2-4fc041e9-8610ade9-30afba82b4baa71d19267279"><ac:plain-text-body><![CDATA[ | [java:[Rogue 2000 | AA. Bibliography#Rogue 00]] | [The Elements of Java Style | http://www.ambysoft.com/books/elementsJavaStyle.html], Rule 78. | ]]></ac:plain-text-body></ac:structured-macro> |
...