Java

Java

Made by DeepSource

For loop appears to check one variable and increment another JAVA-S0214

Bug risk
Major

There is a complicated, subtle or wrong increment in this for loop. Are you sure this for loop is incrementing the correct variable? It appears that another variable is being initialized and checked by the for loop.

This issue is usually caused by a typo.

Examples

Problematic Code

for (int i = 0; i < 20; i++) {
    for (int j = i; j < 20; i++) { // i is updated, not j.
        // ...
    }
}

In most cases, this will result in an infinite loop. Always be mindful of the loop variable being checked or updated, especially in nested loops.

for (int i = 0; i < 20; i++) {
    for (int j = i; j < 20; j++) {
        // ...
    }
}

If this is intended, make sure to document the behavior if what is going on isn't easily obvious.

References