* When the loop counters are updated in the body of the `for` loop
* When the termination condition depends on a method call
* When the termination condition depends on an object property since such properties could change during the execution of the loop.
== How to fix it
=== Code examples
==== Noncompliant code example
Make the termination condition invariant by using a constant or a local variable instead of an expression that could change during the execution of the loop.
[source,java,diff-id=1,diff-type=noncompliant]
----
for (int i = 0; i < foo(); i++) { // Noncompliant, "foo()" is not an invariant
// ...
}
----
==== Compliant solution
[source,java,diff-id=1,diff-type=noncompliant]
----
int end = foo();
for (int i = 0; i < end; i++) { // Compliant, "end" does not change during loop execution
// ...
}
----
==== Noncompliant code example
If this is impossible and the counter variable must be updated in the loop's body, use a `while` or `do` `while` loop instead of a `for` loop.