Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.FOR.LOOP_CONTROL.CHANGE.EXPR

Loop control variable is modified in expression section of a for loop.

MISRA-C++ Rule 6-5-5 (required): A loop-control-variable other than the loop-counter shall not be modified within condition or expression.

This rule is also covered by MISRA.FOR.LOOP_CONTROL.CHANGE.COND.

Rationale

loop-control-variables are either the loop-counter, or flags used for early loop termination. The code is easier to understand if these are not modified within condition or expression.

Note that it is possible for a loop-control-variable with volatile qualification to change value (or have it changed) outside statement due to the volatile nature of the object. Such modification does not break this rule.

Example

for ( x = 0; ( x < 10 ) && !bool_a; ++x )
{
   if ( ... )
   {
      bool_a = true;                               // Compliant
   }
}
bool test_a ( bool * pB )
{
   *pB = ... ? true : false;
   return *pB;
}
for ( x = 0;
   ( x < 10 ) && test_a ( &bool_a );
   ++x ) // Non-compliant
volatile bool status;
for ( x = 0; ( x < 10 ) && status; ++x)           // Compliant
for ( x = 0; x < 10; bool_a = test( ++x ) )       // Non-compliant