MiniBasic logo

The sixth program

It is possible to manipulate arrays of values just using IF ... THEN and GOTO, but it soon becomes very clumsy. For this reason MiniBasic includes FOR NEXT loops.

Say we want to print out an array

10 REM Prints the days of the month
20 DIM months$(12) = "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
30 FOR I = 1 TO 12
40 PRINT "Month", I, months$(I)
50 NEXT I
60 PRINT "Done"

FOR loops can be nested,

10 FOR I = 1 TO 8
20 FOR J = 1 TO 8
30 PRINT I, J
40 NEXT I
50 NEXT J

It is also possible to provide a STEP value other than one.

FOR I = 100 TO 0 STEP -2

If you specify a null loop, such as FOR I = 10 TO 0, control will pass over the loop body and go to the next matching NEXT statement. The control variable must always be in the NEXT statement.

FOR ... TO loops can take complex expressions, such as FOR I = x TO x * x, in these cases the initial value, the end value, and the step value are calculated once and then never modified.

FOR ... NEXT loops can be nested up to 32 deep. If you attempt to jump out of a loop then you are likely to trigger errors

10 REM Bad use of a for loop
20 LET X = 0
30 FOR I = 1 TO 10
40 INPUT Y
50 IF Y < 0 THEN 10
60 NEXT I

However you may alter the counting variable within the loop. This can be used to force premature loop termination.