[previous] MISRA-C:2004  Rule  14.10:  (Required) [next] All if ... else if constructs shall be terminated with an else clause.
This rule applies whenever an if statement is followed by one or more else if statements; the final else if shall be followed by an else statement. In the case of a simple if statement then the else statement need not be included.

The requirement for a final else statement is defensive programming. The else statement shall either take appropriate action or contain a suitable comment as to why no action is taken. This is consistent with the requirement to have a final default clause in a switch statement (15.3)

For example this code is a simple if statement:

if ( x < 0 )
{
    log_error( 3 );
     x = 0;
}                               /* else not needed */

whereas the following code demonstrates an if, else if construct

if ( x < 0 )
{
    log_error( 3 );
    x = 0;
}
else if ( y < 0 )
{
    x = 3;
}
else   /* this else clause is required, even if the     */
{      /* programmer expects this will never be reached */
       /* no change in value of x */
}


QAC messages that encompass this guideline:

2004 No concluding 'else' exists in this 'if'-'else'-'if' statement.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents