13.5 for() loops

6.13 Control Statement Expressions

Moderators: misra-c, david ward

Locked
matth1j
Posts: 1
Joined: Fri Feb 25, 2005 1:45 pm
Location: Reading, UK

13.5 for() loops

Post by matth1j » Fri Feb 25, 2005 5:02 pm

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
Posts: 52
Joined: Mon Dec 06, 2004 12:24 pm

MISRA reply to question

Post by MISRA Reply » Thu May 12, 2005 11:24 am

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
Posts: 72
Joined: Mon Sep 13, 2004 9:24 am

What is the definition of a loop control variable?

Post by Gavin McCall » Fri Sep 09, 2005 3:06 pm

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: Select all

for ( ; ; )
This is allowed by exception for deliberate infinite loops.

Code: Select all

for ( i = 1; i != 10; i+=2) …
i is never set to 10!

Code: Select all

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: Select all

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: Select all

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.[/b]
Gavin McCall
Personal view only.

Locked

Return to “6.13 Control Statement Expressions”