Synopsis: | N-ary selection constructs programmed using "if ... else if ..." shall have an "else" clause. |
Language: | C |
Severity Level: | 6 |
Category: | Statement and Blocks |
Description: |
Justification In the interest of safety and reliability an "else" clause should always be included to be sure all possible cases are covered. Example 1 if (temp < 0) { /* any statement */ } else if (temp > 0) { /* any statement */ } /* WRONG */ Example 2 if (temp < 0) { /* any statement */ } else if (temp > 0) { /* any statement */ } else /* temp == 0 */ /* RIGHT */ { /* Log an error */ } Note It is best practice to use the last "else" clause to log an error even (or especially) if one should never get there. |