Ivan wrote: ↑Fri 02 Oct 2020, 20:11
I thought for-next always increments with one as default.
It does, and that's exactly what your result is showing! Breaking it down to what each statement does:
Code: Select all
FOR t% = 1 TO 0 : REM Set t% = 1, set loop terminating value to 0
PRINT t% : REM Print the value of t%, which is 1
NEXT : REM Add the default STEP to t%, then test if it is > the terminating value
PRINT t% : REM Print value of t%, which is 1 + 1 = 2
But it seems for - next always do one iteration...
Yes, it must, since the test for loop termination is performed in the
NEXT statement. It's the same with a REPEAT ... UNTIL loop, because the test for loop termination is in the
UNTIL statement it must execute at least once. Only in a WHILE ... ENDWHILE loop does the test for termination happen at the
start of the loop, so it may not be executed at all.
There are some BASICs which perform the test for loop termination in the
FOR statement but they tend to run more slowly (if interpreted) because
both the FOR and the NEXT statements must be executed on each loop iteration. In BBC BASIC the FOR statement is executed only
once, which speeds things up.