Foreach loop behaviour and manipulation

bearophile bearophileHUGS at lycos.com
Thu Nov 28 16:41:13 PST 2013


H. S. Teoh:

> Modifying the loop variable of a foreach is, in general, a 
> risky move.

Right. In my opinion D programmers should learn that this is the 
preferred idiom of using foreach ranged loops:

foreach (immutable i; 0 .. n) {}

If a D programmers really needs to modify the index inside the 
loop body, then it's better to use a for() loop, that makes it 
much more explicit and clear what's happening.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list