Java Reference
In-Depth Information
/* ... */
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;
}
Applicability
Failure to include break statements can cause unexpected control flow.
The break statementattheendofthefinalcaseina switch statementmaybeomitted.
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 blockwithoutregardtothepresenceorabsenceofthe break statement.Neverthe-
less,thefinalcaseina switch statementshouldendwitha break statementinaccordance
with good programming style [Allen 2000].
Exceptionally, when multiple cases require execution of identical code, break state-
ments may be omitted from all cases except the last one. Similarly, when processing for
one case is a proper prefix of processing for one or more other cases, the break statement
may be omitted from the prefix case. This should be clearly indicated with a comment.
For example:
Click here to view code image
int card = 11;
int value;
// Cases 11,12,13 fall through to the same case
switch (card) {
// Processing for this case requires a prefix
// of the actions for the following three
case 10:
do_something(card);
Search WWH ::




Custom Search