EasyManua.ls Logo

Radio Shack TRS-80 - Page 68

Radio Shack TRS-80
236 pages
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...
This
display
vividly
demonstrates
operation
of the
nested
FOR-NEXT
loop.
"Nesting"
is
used
in the
same
sense
that
drinking
glasses
are
"nested"
when
stored
to
save space.
Certain
types of
portable
chairs,
empty
cardboard
boxes,
etc. can
be
nested.
They
fit one
inside
the
other
for
easy
stacking.
Let's
analyze
the
program
a
line
at a
time:
Line
10
establishes
the first
FOR-NEXT
loop,
called
A, and
directs
that
it be
executed
3
times.
Line
20
prints "A
Loop" so
we
will
know
where
it came
from
in
the
program.
See
how
this
program
line is
indented
several
spaces to
make it
stand
out as
being
nested
in
the
"A"
loop?
Line
30
establishes
the
second
loop,
called B,
and
directs
that
it be
executed
twice.
It is
indented
even
more
so
you
can
instantly
see
that it is
buried
even
deeper
in
the
"A"
loop.
Line
40
prints
two
items:
first the
blank
shown
between
the
two
quote
marks,
then
the
comma
kicks us
into
the
next
print zone
where
"B Loop"
is
printed.
Makes
for
clear
distinction
on
the
screen
between
the A loop
and B
loop, eh?
Line
50
completes
the "B"
loop
and
returns
control to
line
30
for as
many
executions
of
the
"B"
loop
as
line
30
directs.
So
far we
have
printed
one "A"
and
one "B".
Line
60
ends
the first
pass
through the
"A"
loop and
sends
control
back
to line
10,
the
beginning
of
the A
loop.
The A
loop
has
to
be
executed
3
times
before
the
program
run
is
complete,
printing "A"
3
times
and
"B"
6
times
(3
times
2).
Study
the
program
and
the
explanation
until you
completely
understand
it. It's
simple
but
powerful
magic.
mBMBHB
miiMMHBwnn^
i rr
Okay,
to
get
a
better
"feel"
for
this
nested
loop
(or
loop
within
a
loop)
business,
let's
play
with
the
program.
Change
line
10
to
read:
10
FOR
A
=
1
TD
5
...
and
RUN.
Right!
A was
printed
5
times,
meaning
the "A"
loop was
executed
5
times, and
B was
print-
ed
10
times
-
twice
for
each
pass
of
the
"A" loop.
Now
change
line
30
to
read
When you
write
programs,
be sure
to
indent
lines
to
height
nesting
(or
other
lines you
want
to
emphasize).
This
helps
when
reading
programs
-
and
is
a
great
aid when
debugging
(troubleshooting)
program
problems:
30
FOR B
=
1
TO
4
. . and
RUN.
66

Other manuals for Radio Shack TRS-80

Related product manuals