...
This noncompliant code example declares variables and contains executable statements before the first case label within the switch statement.
| Code Block | ||||
|---|---|---|---|---|
| ||||
int func(int expr) {
switch(expr){
int i = 4;
f(i);
case 0:
i = 17;
/* falls through into default code */
default:
printf(“%d\nâ€, i);
}
return 0;
}
|
...
In this compliant solution, the statements before the first case label occur before the switch statement, improving the predictability and readability of the code.
| Code Block | ||||
|---|---|---|---|---|
| ||||
int func(int expr) {
int i = 4; /* Move the code outside the switch block */
f(i); /* Now the statements will get executed */
switch(expr) {
case 0:
i = 17;
/*falls through into default code */
default:
printf(“%d\nâ€, i);
}
return 0;
}
|
...