EasyManua.ls Logo

Commodore 128D - LOOPS-The FOR-NEXT Command

Commodore 128D
427 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
I
\
Loops—The
FOR-NEXT
Command
Now
change
line
10
to
read
N
=
20,
and
RUN
the
program
again.
Notice
you
can
tell
the
computer
to
execute
more
than
one
state
ment
when
N
is
less
than
5.
You
can
put
any
statement(s)
you
want
after
the
THEN
command.
Remember
that
the
GOTO
15
will
not
be
reached
until
N<5
is
true.
Any
command
that
should
be
followed
whether
or
not
the
specified
condition
is
met
should
appear
on
a
separate
line.
In
the
first
RUN
of
the
program
used
in
the
previous
example,
we
made
the
computer
print
the
variable
N
five
times
by
telling
it
to
increase
or
"increment"
the
variable
N
by
units
of
one,
until
the
value
of
N
equalled
five;
then
we
ended
the
program.
There
is
a
sim
pler
way
to
do
this
in
BASIC.
We
can
use
a
FOR-NEXT
loop,
like
this:
10FORN
=
1
TO
5
20
?N;
"IS
LESS
THAN
OR
EQUAL
TO
5"
30
NEXT
N
40
END
Type
and
RUN
this
program
and
compare
the
result
with
the
result
of
the
IF-THEN
program—they
are
similar.
In
fact,
the
steps
taken
by
the
computer
are
almost
identical
for
the
two
programs.
The
FOR-
NEXT
loop
is
a
very
powerful
programming
tool.
You can
specify
the
number
of
times
the
computer
should
repeat
an
action.
Let's
trace
the
computer's
steps
for
the
program
above.
First,
the
computer
assigns
a
value
of
1
to
the
variable
N.
The
5
in
the
FOR
statement
in
line
10
tells
the
computer
to
execute
all
state
ments
between
the
FOR
statement
and
the
NEXT
statement,
until
N
is
equal
to
5.
In
this
case
there
is
just
one
statement—the
PRINT
statement.
This
is
how
the
computer
interprets
the
inner
workings
of
a
FOR...
NEXT
loop—it
operates
in
much
the
same
way
as
the
IF...
THEN
example
on
the
previous
page.
First,
the
C128
assigns
a
value
of
1
to
the
variable
N.
It
then
executes
all
instructions
between
the
FOR
and
NEXT
keywords.
When
the
NEXT
statement
is
encountered,
it
tells
the
computer
to
increment
the
counter
variable
N
(in
this
case
by
1),
compare
N
to
5
and
continue
with
another
cycle
through
the
FOR
...
NEXT
loop
if
N
>
5
is
false.
The
increment
defaults
to
1
if
no
other
increment
is
specified
in
the
FOR
statement.
After
five
passes
through
the
loop,
and
once
N
>
5
is
true,
the
computer
processes
the
statement
which
immediately
follows
the
NEXT
statement
and
resumes
with
the
rest
of
the
program. Since
the
computer
does
not
compare
the
value
of
N
to
the
start
value
of
the loop
variable
until
the
NEXT
statement
is
encountered,
every
loop
is
executed
at
least
once.
53
USING
C128
MODE—Advanced
BASIC
Programming

Table of Contents

Other manuals for Commodore 128D

Related product manuals