Range Error

kdevel kdevel at vogtner.de
Mon Apr 12 18:01:02 UTC 2021


On Sunday, 11 April 2021 at 20:41:35 UTC, Bastiaan Veelo 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`.

Can't these kinds of errors be eliminated by using foreach loops?:

```d
     Square[8][8] square;
     foreach (int i, ref v; square)
     {
         foreach(int j, ref w; v)
         {
             if((isEven(i)&&isEven(j))^(!isEven(i)&&!isEven(j)))
                 w = new Square(Color.none, sColor.white);
             else 
if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j)))
                 w = new Square(Color.none,sColor.black);
             else
                 assert(false);

         }
     }
```


More information about the Digitalmars-d-learn mailing list