Chapter
10
I
BASIC Keywords
Sample Program
BASIC always sets the final value for the loop variable before
setting the initial value. For example:
820 I=5
830 FOR
I
=
1
TO
I
+
5
840 PRINT
I;
850 NEXT
executes the loop
10
times, which prints:
12345678910
Nested
Loops
FORiNEXT loops may be nested; that is,
a
FOR/NEXT loop may
be placed within the context of another FOR/NEXT loop.
The NEXT statement for the inside loop must appear before the
NEXT for the outside loop.
If
nested loops have the same end
point, a single NEXT statement may be used for all of them.
Sample Program
880 FOR
I
=
1
TO
3
890 PRINT "OUTER LOOP"
900 FOR
J
=
1 TO 2
91 0 PRINT
"
INNER LOOP"
920 NEXT
J
930 NEXT
I
This program performs
3
outer loops and
2
inner loops within
each of the outer loops. BASIC prints the following:
OUTER LOOP
INNER LOOP
INNER LOOP
INNER LOOP
INNER
LOOP
INNER LOOP
INNER LOOP
OUTER LOOP
OUTER LOOP
165