When there is only a single condition to test, you have the option of using either a switch
statement or an if
-else
if
-else
statement. For a larger set of potential values, a switch
can be easier to read, but when the condition being
tested is essentially boolean, then an if
/else
statement should be used instead.
Noncompliant code example
_Bool b = p > 0;
switch (b) { // Noncompliant
...
}
switch (x == 0) { // Noncompliant
...
}
Compliant solution
_Bool b = p > 0;
if (b) {
...
} else {
...
}
if (x == 0) {
...
} else {
...
}