AV1530 Don’t change a loop variable inside a for loop block



LonelyPixel wrote Jun 25, 2015 at 5:01 PM

I'd like to propose an exception to this:

If you're iterating a list and make changes to the list, like removing or inserting an item, it may be necessary to adjust the index variable accordingly to continue the loop at the correct position. This is in fact a major advantage of using a for loop over foreach which doesn't allow you to alter the collection while iterating it.