FOR...NEXT (Statement)


Repeatedly execute a block of statements in a FOR...NEXT loop.


FOR <variablename> = <initial value> TO <final value> [STEP <increment>]
:
NEXT [<variablename>]


This statement repeatedly executes a block of statements in a FOR...NEXT loop according to the condition specified in the FOR line.
<initial value> and <final value> specify the initial and final values of the variable specified by <variablename>, respectively.
<increment> specifies the increment from the initial to the final values. If STEP is omitted, the increment is regarded as 1.
In the following cases, the FOR...NEXT is not executed and control is transferred to the position immediately following the NEXT.
  • <increment> is positive and <initial value> is greater than <final value>
  • <increment> is negative and <initial value> is smaller than <final value>
Note that <initial value> is assigned to <variablename>.
You can nest FOR...NEXT statements in one FOR...NEXT, which is referred to as a nested construction. In such a case, each FOR...NEXT must have different variables.
Additionally, a nested FOR...NEXT must be concluded within the upper-level FOR...NEXT.
FOR and NEXT must be a pair.
Jumping into or out of a FOR...NEXT loop with GOTO does not assure the program operation. If the increment is set to 0, the loop becomes infinite.



DEFINT li1, li2, li3, li4, li5, li6, li7, li8, li9
DO WHILE li1 > li2
'Repeat in a pretest loop
IF li1 = 4 THEN EXIT DO
'If li1=4, exit from DO...LOOP
FOR li3 = 0 TO 5
'Repeat the process of FOR...NEXT 5 times
FOR li4 = li5 TO li6
'Repeat the process of FOR...NEXT while adding 1 to the
'value of li5 each time the process is done until li5
'becomes the value of li6
FOR li7 = 1 TO li8 STEP 2
'Repeat the process of FOR...NEXT while adding 2 to the
'value starting with 1 each time the process is done
'until the value becomes li8
IF li2 = 2 THEN EXIT FOR
'If li2=2, exit from FOR...NEXT
DO WHILE li2 < li9
'Repeat in a pretest loop
GOSUB *samp2
li9 = li9 + 1
LOOP
'Call a GOSUB *samp2 statement while li2 < li9
NEXT li7
'Repeat
NEXT
'Repeat
NEXT
'Repeat
li9 = 0
DO
'Repeat in a posttest loop
GOSUB *samp2
li9 = li9 + 1
LOOP UNTIL li9 < 5
'Call a GOSUB *samp2 statement until li9 < 5
LOOP
'Repeat


Top