[previous] MISRA-C:2004  Rule  14.8:  (Required) [next] The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement.
The statement that forms the body of a switch statement or a while, do ... while or for loop, shall be a compound statement (enclosed within braces), even if that compound statement contains a single statement.

For example:

for ( i = 0; i < N_ELEMENTS; ++i )
{
    buffer[i] = 0;       /* Even a single statement must be in braces */
}

while ( new_data_available )
    process_data();       /* Incorrectly not enclosed in braces */
    service_watchdog();   /* Added later but, despite the appearance
                             (from the indent) it is actually not
                             part of the body of the while statement,
                             and is executed only after the loop has
                             terminated */

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.


Related rules:

Rule  14.9 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.



(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