FOR...NEXT
 
Syntax


FOR counter = startvalue TO endvalue [STEP stepvalue]
    [statement block]
NEXT [counter]

FOR counter = startvalue DOWNTO endvalue [STEP stepvalue]
    [statement block]
NEXT [counter]

Description


A FOR [...] NEXT loop initializes counter to startvalue, then executes the statement block's, incrementing counter by stepvalue until it reaches endvalue. If stepvalue is not explicitly given it will set to 1.

If the DOWNTO is used, then the counter is decremented by the stepvalue or 1 if none is specified.

An EXIT is the proper way to leave a FOR...NEXT, you can also use a GOTO or RETURN statement.

Example


PRINT "counting from 3 to 0, with a step of -1"
FOR i = 3 DOWNTO 0 STEP 1
        PRINT "i is "; i
NEXT i

Differences from other BASICs

See also