MISRA Discussion Forums

Full Version: 13.5 for() loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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.

Gavin McCall

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) …
i is never set to 10!

Code:
for ( ; x < 10 ; x++)
for ( ; (x < 10) && (flags != false; 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++)
for ( x = p ; (x < q) && (flags != false) ; x++)

These are compliant if
x is not modified in body.
Note
x is loop counter
flags test is optional

Code:
for ( ;  ; x ++)
for ( x = 0 ; ; x ++)
for ( x = 3 ; ; )
for (x = 5;    x < 10 ; )
for (x = 5;    (x < 10) && (flags != false) ; )
for ( ; x < 6 ;  x < 20 ; )
for ( ; x < 6 ;  x < 20 && flags != true ; )

Not compliant – new clarification of 13.5

The text above are only examples of possible expressions.