13.5 for() loops - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.13 Control Statement Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=40) +---- Thread: 13.5 for() loops (/showthread.php?tid=25) |
13.5 for() loops - matth1j - 25-02-2005 Hi Rule 13.5 (required) states: \"The 3 expressions of a for statement shall be concerned only with loop control\", and \"First expression: initialising the loop counter. Second expression: shall include testing the loop counter, and optionally other loop control variables. Third expression: increment or decrement of the loop counter.\" The implication would appear to be that for() loops should only be used where there is a loop counter. Is that correct, or could you (for example) use a for() loop without a loop counter, in which case there wouldn't be a first expression? Is the idea to restrict C's for() usage to that of Pascal's ie. for var := first [ to | downto ] last do ... ? I'm Ok with this, but I feel that either I'm missing something or the rule isn't explicit enough about what for() can and can't be used for. Regards John NEC Technologies (UK) MISRA reply to question - MISRA Reply - 12-05-2005 Rule 13.5 also states \"when present the three expressions of a for statement shall be used only for these purposes.\" It is therefore possible to have a loop counter that is initialised outside the loop. It is the intention that all for loops should have a loop counter. What is the definition of a loop control variable? - Gavin McCall - 09-09-2005 Question: What is the definition of a loop control variable as referred to in Rules 13.5 and 13.6 ? It is difficult for programmers to comply with these rules because the definition of a \"loop control variable\" is vague. Answer: MISRA C Steering Team 7/9/2005 Our primary concern is to reduce the risk of programming unbounded loops. Loop Control Variable is defined as any variable occurring in the first, second or third expressions. Loop Counter is defined as a Loop Control Variable which is, • Initialised in the first expression or Initialised prior to first expression; • In the second expression, the operand of a relational operator (= ); Note (1) • In the third expression, always incremented or decremented by a constant, or an expression which evaluates to the same value for the duration of the loop; • Not modified in the body of the loop. Only the Loop Counter can be modified in the first and third expressions. No Loop Control Variable shall be modified in the second expression. Loop Control Variables which are not Loop Counters can only be changed in the body of the loop if they are effectively Boolean. (See Rule 13.6) Note 1: The equality operators (==, !=) should not be used because termination of the loop may not occur. Examples Code: for ( ; ; ) This is allowed by exception for deliberate infinite loops. Code: for ( i = 1; i != 10; i+=2) … Code: for ( ; x < 10 ; x++) These are compliant if x is initialised prior to loop x is not modified in body. Note x is loop counter flags test is optional Code: for ( x = p ; x < q ; x++) These are compliant if x is not modified in body. Note x is loop counter flags test is optional Code: for ( ; ; x ++) Not compliant – new clarification of 13.5 The text above are only examples of possible expressions. |