Range Error

Steven Schveighoffer schveiguy at gmail.com
Mon Apr 12 01:22:12 UTC 2021


On 4/11/21 4:41 PM, Bastiaan Veelo wrote:
> On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:
>> What am I doing wrong here? Is it the 'for' loop?
> 
> Yes, there is a `7` where there should be an `i` on this line:
> ```d
>    for(int i=7;7>=0;i--)
> ```
> This will go on forever, so you get a range error as soon as `i < 0`.

Also this code is wrong:

```d
    for(int i=2;i>=0;i--)
    {
        for(int j=7;i>=0;j--)
```

For that j loop, i will always be 2, so it will not terminate until the 
range error happens.

Should probably be:

for(int j=7; j >= 0; j--)

I would also suggest for more readable code, use foreach + iota or 
foreach_reverse:

```d
foreach_reverse(i; 0 .. 3) // or foreach(i; iota(2, -1, -1))
{
      foreach_reverse(j; 0 .. 8)
```

-Steve


More information about the Digitalmars-d-learn mailing list