[previous] MISRA-C:2004  Rule  14.9:  (Required) [next] An if (expression) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement.
For example:

if ( test1 )
{
    x = 1;              /* Even a single statement must be in braces */
}                       
else if ( test2 )       /* No need for braces in else if */
{                       
    x = 0;              /* Single statement must be in braces */
}                       
else                    
    x = 3; /* This was (incorrectly) not enclosed in braces */
    y = 2; /* This line was added later but, despite the appearance
              (from the indent) it is actually not part of the else, and
               is executed unconditionally */

Note that the layout for compound statements and their enclosing braces should be determined from the style guidelines. The above is just an example.


QAC messages that encompass this guideline:

2212 Body of control statement is not enclosed within braces.
2214 Body of control statement is on the same line and is not enclosed within braces.
3402 Braces are needed to clarify the structure of this 'if'-'if'-'else' statement.


Related rules:

Rule  14.8 The statement forming the body of a switch, while, do ... while or for statement shall be a compound 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