...
In this noncompliant code example, the case wherein the card is 11, does not have a break statement. As a result, the statements for card = 12 are also executed.
...
This compliant solution terminates each case (including the default case) by 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;
}
|
...