Topic : The GFA-Basic Compendium Author : GFA Systemtechnik GmbH Version : GFABasic.HYP v2.98 (12/31/2023) Subject : Documentation/Programming Nodes : 899 Index Size : 28056 HCP-Version : 3 Compiled on : Atari @charset : atarist @lang : @default : Document not found @help : Help @options : +g -i -s +z @width : 75 @hostname : STRNGSRV @hostname : CAB @hostname : HIGHWIRE @hostname : THING View Ref-FileFOR c=b TO e [STEP s] (instructions) NEXT i FOR c=b DOWNTO e (instructions) NEXT i c: avar b, e, s: aexp The FOR-NEXT command (also called a counting loop) is used for repeated processing a group of instructions between the commands FOR and NEXT. For this purpose the count variable c is incremented (or decremented), beginning with the initial value b, as far as the end value 'e'. The instructions in the loop body are gone through, until NEXT is reached. There the count variable c is increased by the value s given after STEP. If no STEP value is given, then an increment of one is assumed. Next c is checked to see whether it has exceeded the value e. If this is the case, the loop is exited and the program continued with the command after the NEXT. If not, the program loops back again to the first instruction, this process is repeated, until c is larger than e. A consequence of this exit method is that c, after the loop is left, is equal to e+s, i.e. the first value which exceeds the abort criterion. Also, the contents of a FOR-NEXT loop will always be gone through at least once. Note: In place of a STEP value of -1 the command DOWNTO can be used instead of TO. However, with DOWNTO the use of STEP is not possible. In general, for the count variable c one should use integer variables, because thereby the loop can be processed more quickly than with floating-point variables. This is naturally not possible with non-integer STEP increments. In place of the command NEXT followed by the count variable, the command ENDFOR i% can be used, but the Interpreter automatically replaces it with NEXT i%. Examples: FOR c=1 TO 10 PRINT c' NEXT c FOR c=-1 DOWNTO -10 PRINT c' NEXT c --> Displays on the screen, the numbers: 1 2 3 4 5 6 7 8 9 10 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 a$="T*e*s*t*w*o*r*d" FOR j=1 TO LEN(a$) STEP 2 PRINT MID$(a$,j,1); NEXT j --> Displays the word 'Testword' on the screen. Memo: For compiler options see section 'FOR-NEXT Loop Checking'. When counting down the counter will end up -1 lower than the set limit. When counting up the counter will end up +1 higher than the set limit. This can be exploited: FOR i&=0 to 32 PRINT i& EXIT IF i&=16 NEXT IF i&=32+1 PRINT "completed all iterations" ELSE PRINT "terminated prematurely" ENDIF A FOR-NEXT loop going backwards with a byte| counter using STEP will completely fail when compiled. Such a loop will work in the interpreter however. Somehow the loop ends up missing entirely when compiled! FOR i|=200 TO 100 STEP -1 !does not work compiled PRINT i|;","; NEXT i| FOR i|=200 DOWNTO 100 !this works compiled PRINT i|;","; NEXT i| FOR-NEXT loops using floats can possibly issue error code #8 if FRE() goes too low. One would expect error code #91, however it's never actually used anywhere, including the library.