A for
loop has 3 basic elements:
In this case, the inner for
loop is modifying a variable that belongs to the outer/enclosing for
loop. This can result in an undefined behavior such as infinite loop. It is therefore recommended that you modify the right variable to ensure that your loop terminates as required.
for (var i = 0; i < ub; i++)
{
// `i` is incremented instead of `j`
for (var j = 0; j < ub; i++)
{
}
}
for (var i = 0; i < ub; i++)
{
// `j` is now correctly incremented instead of `i`.
for (var j = 0; j < ub; j++)
{
}
}