The problem arises when you have a large code.
Pitfall number one: You accidentally or intentionally changing
i somewhere inside your loop. You should obviously never do it, but changing the increment is the first step in this direction.
Pitfall number two: For some reason, you want to change the behavior of your dynamic step. You do it in one place, but forget to do in another and now you have two iterations with different number of steps.
Pitfall number three: Suppose you want a more complicated behavior and need three different steps instead of two. You won’t be able to do it with
for anymore. You will have to either delegate step calculation to additional function which is even worse, or rewrite everything. In my example you can do it easily by replacing
Pitfall number four occurs when you have some parallel operations messing with
i. In my example, you can simply replace
AtomicInteger and make your operations thread safe.
There could be additional motivation, but in general I think that classic
for loop should be deprecated and replaced by functional-style operations wherever it is possible.