foreach

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 13 15:15:59 PDT 2014


On Friday, 13 June 2014 at 22:07:53 UTC, H. S. Teoh via 
Digitalmars-d wrote:
>> The very idea of a loop without a condition seems very, very 
>> wrong to
>> me.
>
> Why would it be "very, very wrong"? Perpetual cycles are 
> ubiquitous in
> nature

Hum... you guys seem to be forgetting about break statements.

You know when you want to do something, and also do something 
*between* each iteration, but don't want to check your end 
condition twice per loop? It usually ends up looking something 
like this:

T[] arr = ... ;
auto len = arr.length;
write("[");
if ( len != 0 ) {
   size_t i = 0
   for ( ;  ;  ) {
     write(arr[i]);
     if (++i == len) break;
     write(", ");
   }
}
write("]");


More information about the Digitalmars-d mailing list