...
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
In the compliant solution, each case label is ended with a break statement.
Code Block |
---|
|
int card = 11;
/* Case 11,12,13 fall through to the same case */
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;
}
|
...
Code Block |
---|
|
int card=11;
int value;
/* Case 11,12,13 fall through to the same case */
switch (card) {
/* ... */
case 11:
case 12:
case 13:
value=10;
break;
default:
/* Handle Error Condition */
break;
}
|
Risk Assessment
Failure to include break statements leads to unexpected control flow.
...