Why is this an issue?
A for
loop with a counter that moves in the wrong direction is not an infinite loop. Because of wraparound, the loop will eventually
reach its stop condition, but in doing so, it will run many, many more times than anticipated, potentially causing unexpected behavior.
Noncompliant code example
public void DoSomething(string[] strings)
{
for (int i = 0; i < strings.Length; i--) // Noncompliant
{
string s = strings[i]; // IndexOutOfRangeException when i reaches -1
// do stuff
}
}
Compliant solution
public void DoSomething(string[] strings)
{
for (int i = 0; i < strings.Length; i++)
{
string s = strings[i];
// do stuff
}
}