09-09-2005, 02: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
This is allowed by exception for deliberate infinite loops.
i is never set to 10!
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
These are compliant if
x is not modified in body.
Note
x is loop counter
flags test is optional
Not compliant – new clarification of 13.5
The text above are only examples of possible expressions.
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++)
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.