Chapter
10
I
BASIC Keywords
FOR/NEXT
Statement
FOR
variable
=
initial value
TO
final value
[STEP
increment]
NEXT
[variable]
Establishes a program loop that allows a series of program state-
ments to be executed
a
specified number
of
times.
Variabb
must be either integer or single precision. Each
FOR/
NEXT
loop
must have
a
unique variable.
Increment
is the number BASIC
adds to the
initial value
each
time the loop
is
executed. If you omit
increment,
BASIC incre-
ments by
1.
If
increment
is
a negative value, BASIC decreases
the
initial value
each time through the loop. In this case, the
fi-
nal value
must be less than the
initial value.
BASIC executes the program lines following the FOR statement
until it encounters
a
NEXT. At this point, it increases
initial
value
by the STEP
increment.
If
initial value
is less than or equal
to
final value,
BASIC branches back to the line after FOR and
repeats the process. When
initial value
is greater than
final
value,
the loop
is
completed, and BASIC continues with the state-
ment after NEXT.
Note:
BASIC skips the body of the loop if
initial value
is greater than
final value
when
increment
is positive
or
if
final value
is greater than
initial value
when
in-
crement
is negative.
164