A for loop with a counter that moves in the wrong direction, away from the stop condition, is not an infinite loop. Because of wraparound,
the loop will eventually reach its stop condition, but in doing so, it will probably run more times than anticipated, potentially causing unexpected
behavior.
If your stop condition indicates a maximum value, the iterator should increase towards it. Conversely, if your stop condition indicates a minimum value, the iterator should decrease towards it.
for (int i = 0; i < maximum; i--) // Noncompliant: runs until it underflows to int.MaxValue
{
// ...
}
for (int i = maximum; i >= maximum; i++) // Noncompliant: runs until it overflows to int.MinValue
{
// ...
}
for (int i = 0; i < maximum; i++) // Compliant: Increment towards the maximum value
{
}
for (int i = maximum; i >= 0; i--) // Compliant: Decrement towards the minimum value
{
// ...
}