BASIC Programming/Beginning BASIC/Control Structures/FOR...NEXT

The FOR...NEXT loop is a form of flow control that places focus on a single counter.

The basic syntax is simple - a variable is given a starting value and ending value, and it increments on each pass of the loop:

FOR i = 1 TO 10
NEXT

A more advanced loop includes the STEP parameter, which specified the value at which a variable is incremented. It can be negative to have the loop decrease the variable instead of increasing it, and may even be fractional. An example of a reverse counting loop is as follows:

FOR i = 10 TO 1 STEP -1
NEXT

The FOR loop terminates when the variable passes the final value in the loop. This is checked by determining if it is greater than the second parameter (or less than if STEP is negative.)

If desired, you can place variables within the start and end parameters instead of constant numbers.