Program 2
•
•
•
Program
Structure
in
this
case,
the content between NEXT and END FOR
IS
a kind
of
epilogue which
IS
only executed
if
the FOR loop runs
ItS
full
course.
If
there
is
a premature EXIT the
epilogue
is
not executed.
The
same effect can be achieved with a REPeat loop though
it
is
not
necessarily the
best
way
to
do
it
However
it
IS
worth looking
at
(perhaps
at
a second reading)
if
you
want
to
understand structures which
are
Simple enough
to
use
In
simple
ways
and
powerful enough
to
cope
with
awkward situations when they
arise.
100
REMark
Western
REPeat
with
Epi
Logue
110
LET
buLLets
=6
120
REPeat
Bandit
130
PRINT
"Take
aim"
140
PRINT
"Fire
shot"
150
LET
hit
=RND(9)
160
IF
hit
= 7
THEN
EXIT
Bandit
170
LET
buLLets
=
bulLets-l
180
IF
buLLets
<>
0
THEN
NEXT
Bandit
190
PRINT
"Watch
Bandit"
200
END
REPeat
Bandit
210
PRINT
"Return
to
Dodge
City"
The
program works properly
as
long
as
the
sheriff has
at
least
one bullet
at
the
start.
It
fails
if
line 20 reads:
110
LET
buLLets
= 0
You
might think that the sheriff would be a
fool
to
start
an
enterprise
of
this
kind
if
he
had no bullets
at
all,
and
you
would
be
right
We
are
now discussing how
to
preserve
good structure
In
the most complex type
of
situation.
We
have
at
least
kept the problem
context
simple;
we
know what
we
are
trying
to
do.
Complex structural problems usually
arise
in
contexts more difficult than Wild
West
simulations.
But
if
you
really want a solution
to
the problem which caters
for
a possible
hit,
running out
of
bullets and
an
epilogue,
and also the zero case then add the following line
to
the above program:
125
IF
bulLets
= 0
THEN
PRINT
"Watch
Bandit"
:
EXIT
bandit
We
can conceive
of
no more complex type
of
problem than
thiS
with
a
Single
loop.
SuperBASIC can easily handle
it
if
you want
it
to.
Consider the following FOR loop which
PLOTS
a
row
of
points
of
various randomly
NESTED
LOOPS
chosen colours (not black).
100
REMark
Row
of
pixels
110
PAPER
0 :
CLS
120
LET
up
=
50
130
FOR
across
= 20
TO
60
140
INK
RND
(2
TO
7)
150
POINT
across,
up
160
END
FOR
across
This program plots a
row
of
points thus:
If
you want
to
get
say
51
rows
of
points
you
must plot a row
for
values up from
30
to
8D.
But
ycu must
always
observe
the
rule
that
a structure can go completely
within
another
or
it
can go properly around
it
It
can also follow
in
sequence, but
it
cannot 'mesh'
with
another structure. Books about programming often show how FOR loops can be related
with a diagram
like
12/84
Right
(nested)
Right
(sequence)
Wrong
(Meshed)
81