...
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
*MSC09-EX0*: The {{ Wiki Markup 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|AA. References#Rogue 00]\]).
MSC09-EX1: When multiple cases require execution of identical code, then break
statements may be omitted from all cases except the last one. For example:
...
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 | |
"CLL Switch Statements and Static Analysis" | |
CWE-484, "Omitted Break Statement in Switch" |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fdf23875-d682-49e8-b9fe-7ad7ae47892c"><ac:plain-text-body><![CDATA[ | [[JLS 2005AA. References#JLS 05]] | 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="e2e3cfb6-fbad-4670-83d8-a07bd38e4428"><ac:plain-text-body><![CDATA[ | |
[ [Rogue 2000AA. References#Rogue 00]] | [The Elements of Java Stylehttp://www.ambysoft.com/books/elementsJavaStyle.html], Rule 78. ]]></ac:plain-text-body></ac:structured-macro> |
...
49. Miscellaneous (MSC) MSC57-J. Use inequality operators to terminate loops whose counter changes by more than one