About foreach loops

David Nadlinger see at klickverbot.at
Wed Jun 15 07:48:49 PDT 2011


On 6/15/11 4:35 PM, Caligo wrote:
> The problem with trying to "fix" foreach is that it would create
> problems of its own.  The following code would not behave correctly:
>
>    foreach(i; 0..10){
>      if(i&  1)
>        i += 1;
>      writeln(i, " is even.");
>    }

But in my opinion it should not be legal in the first place anyway, as 
we already have »continue« to do this in a very clear way:

---
foreach (i; 0 .. 10) {
   if (i & 1) continue;
   writeln(i, " is even.");
}
---

David


More information about the Digitalmars-d mailing list