Skip to content

FOR/NEXT

Loop Controlled by Counter

This directive provides the ability to repeat a segment of program a specified number of times.

FOR numeric-variable = numeric-value1 TO numeric-value2
{STEP numeric-value3]
NEXT numeric-variable
numeric-variable
is any valid numeric variable name.
numeric-value1,2,3
is any number.

This directive starts numeric-variables at numeric-value1 and increment numeric-variable by numeric-value3 (or 1 if not specified) each time the NEXT is executed until numeric-value2 is equalled or exceeded.

This directive places a return address pointer on the stack of scheduled places to go in order to show the NEXT portion where to return. This pointer is removed when the loop is terminated normally by NEXT (numeric-variable reaches or exceeds value2) or an EXITTO directive abnormally terminates the loop.

The NEXT that is paired with this FOR directive must have the same numeric-variable name.

There may exist more than one NEXT for the same FOR/NEXT loop.

01000 FOR NUM_VAL = 10 TO 5000
01010 FOR BACK_LOOP = NUM_VAL TO 2 STEP -1
01020 IF FPT(NUM_VAL/BACK_LOOP)=0 AND NUM_VAL<>BACK_LOOP
THEN EXITTO 01050
01030 NEXT BACK_LOOP
01040 PRINT NUM_VAL
01050 NEXT NUM_VAL

prints all prime numbers from 10 to 5000.

WHILE/WEND directive

From the Thoroughbred Basic Language Reference, Volume II: F–Q, version 8.9.1. Open this page in the PDF