MISRA Discussion Forums

Full Version: For Loop: must update expression use only invariants?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
MISRA C:2012 Rule 14.2 puts the following restriction on the "third clause" (called "expression" in C++:2008):

Shall not use objects that are modified in the for loop body.

For example, the following violates this rule:
[code]
for (int i=0; i
Your example violates rule 6-5-4

i is a loop counter (initialised before the condition, tested with a relational operator in the condition and modified in the expression).

6-5-4 requires that the loop counter shall only be modified by one of ++ -- += n or -= n where n is constant for the duration of the loop