Improve the handling of impossible FOR loops
skx opened this issue · comments
Steve Kemp commented
The following example code was emailed to me:
10 FOR I = 1 TO -20
20 PRINT I, "\n"
30 NEXT I
Output? Constant numbers.
Expected output? zero numbers. There shouldn't even be a single iteration.
A similar situation is:
10 FOR I = 1 TO 10 STEP 7
20 PRINT I, "\n"
30 NEXT I
On my system that never terminates. On a real spectrum it produces:
1
8
Steve Kemp commented
10 FOR I = 1 TO 10 STEP 2
20 PRINT I, "\n"
30 NEXT I
Prints 1, 3, 5, 7, 9
.
I guess we test if the loop is "up" or "down" and test for >= END on each iteration.
Steve Kemp commented
Of course ">=" doesn't matter if START==END we must run one iteration. Hrm.