foreach loop

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 19 09:30:17 PDT 2015


On Monday, 19 October 2015 at 15:56:00 UTC, Namal wrote:
> Is it possible to use foreach backwards?
>
> foreach(int i;20..1)
>  writeln(i);
>
> compiles but I get nothing.

foreach_reverse(i; 1 .. 20)
     writeln(i);

Or:

import std.range : iota, retro;
foreach(i; iota(1, 20).retro)
     writeln(i);

But if you really want the numbers 1 to 20, you should use 21 as 
the end value in both cases, as it's exclusive.


More information about the Digitalmars-d-learn mailing list