Previous Topic: Repetitive StatementsNext Topic: WHILE..WEND Statements


FOR..NEXT..STEP Statements

The syntax of this type of statement is as follows:

FOR variable=expression TO expression 
    [STEP expression]
       statement block
NEXT variable

Where, expression must evaluate into an integer.

Note: You can use EXIT FOR in a FOR..NEXT..STEP statement to exit the FOR loop.

Example:

The statement prints each of the numbers 2, 4, 6, and 8 on a separate line and then prints all four numbers on one line.

DIM A as String
DIM J as Integer
A=""
FOR J=2 TO 8 STEP 2
A=A+Str(J)
PRINT Str(J)
NEXT J
PRINT A
end: