MISRA Discussion Forums
For Loop: must update expression use only invariants? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.6 Statements (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=135)
+---- Thread: For Loop: must update expression use only invariants? (/showthread.php?tid=1390)



For Loop: must update expression use only invariants? - swestin - 29-11-2017

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


Re: For Loop: must update expression use only invariants? - misra cpp - 17-01-2018

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