for...next

The FOR . . . NEXT looping construct provides a way to execute code a specific amount of times. See the section below on the proper way to exit a loop before the counter variable reaches the limit.

Optional variable with "next"

Just BASIC makes the use of a variable after "next" optional, but if one is designated, it must match the variable use with "for".

Exiting a loop prematurely

GOSUB, FUNCTION and SUB may be used within a FOR/NEXT loop because they only temporarily redirect program flow or call on other parts of the program. Program execution resumes within the FOR/NEXT loop in these instances.  Program execution does not return to the FOR/NEXT loop if GOTO is used within the loop.  GOTO should not be used to exit a FOR/NEXTloop.  "EXIT FOR" will correctly exit the loop before it would have terminated normally.

EXIT FOR

If it is necessary to exit a loop before the counter variable has reached its final value, use the EXIT FOR statement.  This allows the program to exit the loop properly and to preserve the current value of the counter variable. 