The FOR NEXT loop is a simple way to loop through one or more instructions for a specified number of times.
FOR X=1 to 12
In this example, the numbers 1 through 12 will be displayed when the program is run. The variable X is first assigned the value of 1. When the program reaches NEXT X, the value in X is increased to 2 and the current position in the program jumps back to the FOR statement.
In Ozapell Basic, FOR NEXT loops can also decrease in value.
FOR Y=12 to 1
In this example, the numbers 12 through 1 will be displayed when the program is run. Ozapell Basic determines that each NEXT Y will decrease the value in Y based on the values in the FOR statement.
The values used in a FOR statement must be integers (whole numbers) including 0 and negative numbers.
FOR Z=-5 TO 5
When the program is run, the numbers -5 through 5 will be displayed including the 0 in the middle.
Finally, it should be noted that the values provided in the FOR statement can also be variables when the range needed is not known until run-time. This would be useful when looping through a line of text where the length of the text isn’t known to the programmer.
FOR C=0 to L
In this partial example, the variable C will first be assigned the value of 0 and then loop until C reaches the value contained in the variable L.