switch
statements are useful when there are many different cases depending on the value of the same expression.
For just one or two cases however, the code will be more readable with if
statements.
switch (variable) {
case 0:
doSomething();
break;
default:
doSomethingElse();
break;
}
if (variable == 0) {
doSomething();
} else {
doSomethingElse();
}
- MISRA C:2004, 15.5 - Every switch statement shall have at least one case clause.
- MISRA C++:2008, 6-4-8 - Every switch statement shall have at least one case-clause.
- MISRA C:2012, 16.6 - Every switch statement shall have at least two switch-clauses