MISRA.FOR.STMT.CHANGEFor loop counter is modified within the loop loop statement. MISRA-C Rule 13.6 (required): Numeric variables being used within a for loop for iteration counting shall not be modified in the body of the loop.This rule is also covered by MISRA.FOR.COND.CHANGE. Loop counters shall not be modified in the body of the loop. However other loop control variables representing logical values may be modified in the loop, for example a flag to indicate that something has been completed, which is then tested in the for statement. Exampleflag = 1; for ( i = 0; (i < 5) && (flag == 1); i++ ) { /* ... */ flag = 0; /* Compliant - allows early termination of loop */ i = i + 3; /* Not compliant - altering the loop counter */ } MISRA-C++ Rule 6-5-3 (required): The loop-counter shall not be modified within condition or statement.This rule is also covered by MISRA.FOR.COND.CHANGE. RationaleModification of the loop-counter other than in expression leads to a badly-formed for loop. Examplebool modify ( int32_t * pX ) { *pX++; return ( *pX < 10 ); } for ( x = 0; modify ( &x ); ) // Non-compliant { } for ( x = 0; x < 10; ) { x = x * 2; // Non-compliant } |