7-6 Programming
Iteration
Trio Motion Technology
The program would set the variable
t
to a value of 1 and then go to the next
line to
PRINT
. After the print, the
NEXT
command would return the program to
the
FOR
command and increment the value of T to make it 2. When the
PRINT
command is used again, the value of T has changed and a new value is printed.
This continues until T has gone from 1 through to 5, then the loop ends and
the program is permitted to continue. The next command after the
NEXT
statement prints "Done" to the screen slowing the program has left the loop.
You can also next loops to create a loop within a loop, as the following
example shows:
FOR a=1 TO 5
PRINT "MAIN A=";a
FOR a=1 TO 10
PRINT "LITTLE B=";a
NEXT a
NEXT a
The
FOR..NEXT
statement loops the main A variable from 1 to 5, but for every
loop of A the
FOR..NEXT
statement inside the first loop must also loop its
variable B from 1 to 10. This is known as a nested loop as the loop in the
middle is nested inside an outer loop..
Such loops are especially useful for working on array data by using the
variables that increment as position indexes for the arrays. As an example, we
could list all our lottery numbers using the following example:
FOR y=1 TO 12
FOR x=1 to 12
MOVEABS(x,y)
NEXT x
NEXT y