[futurebasic] Re: FOR loop

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 1997 : Group Archive : Group : All Groups

From: Rick Brown <rbrown@...>
Date: Tue, 02 Dec 1997 23:04:21 -0600
> FOR i = X to Y STEP S
>    execute this!
> NEXT i
> 
> is really executed as ...
> 
> i = X
> DO
>    execute this!
>    IF S >= 0 THEN INC(i) ELSE DEC(i)
> UNTIL i > Y

I think that DO loop should instead look like:

 DO
     execut this!
     IF S >= 0 THEN i = i + S ELSE i = i - S  '(in case ABS(S) <> 1)
 UNTIL SGN(i - Y) = SGN(S)  '(in case S < 0)

Another peculiarity of FB's FOR logic is that the initial, final and
STEP values (X, Y and S in this example) are reevaluated each time
through the loop.  In every other version of Basic I've used, they are
only evaluated once (i.e., when the loop is first entered).

- Rick