A switch
block comprises several case
labels and an optional but highly recommended default
label. By convention, statements 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.
...
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
.
...
Wiki Markup |
---|
*MSC14-EX1*: The {{break}} statement at the end of the final case in a {{switch}} statement may be omitted;. byBy convention, this is the {{default}} label. The {{break}} statement serves to transfer control to the end of the {{switch}} block;. fallFall-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. Bibliography#Rogue 00]\]). |
...