PB.FLVA
Do not assign to loop control variables in the body of a "for" loop
Description
This rule flags assignment to a loop control variable in the body of a "for" loop.
A "for" loop control variable should only be modified in the initialization and condition expressions of the "for" loop statement. Modifying them inside the body of the "for" loop makes the loop condition difficult to understand and points to a possible logical flaw in the code.
Example
package PB;
public class FLVA
{
void method1() {
for (int i = 0; i < 100; i++) { // Violation
i += 3;
}
for (int i = 0; i < 100; i++) { // Violation
i++;
}
}
}
Repair
Rewrite the code so that the "for" loop control variable doesn't need to be assigned within the loop body.
|