Program
Structure
In
SuperBASIC the
rule
applies
to
all
structures.
You
can solve
all
problems
uSing
them •
properly
We
therefore treat the FOR loop
as
an
entity and design a new program:
FOR up
= 30
TO
80
FOR across
~
20
TO
60
INK RND(2
To
7)
POINT across, up
END FOR across
iii.
END FOR up
When
we
translate this into a program we are entitled not only
to
expect
It
to
work but
to
know what
It
will
do.
It
will
plot a rectangle made up of
rows
of
pixels.
100
REMark Rows
of
pixeLs
110
PAPER
0 : C
LS
1Z0
FOR
up
=
30
TO
80
130
FOR
across
=
ZO
TO
60
140
INK
RND
(Z
TO
7)
150
POINT
across,Up
160
END
FOR
ac
ross
170
END
FOR
up
Different structures may be nested. Suppose
we
replace
the
Inner FOR loop of the above
program
by
a REPeal loop.
We
will terminate the REPeat loop when the zero colour
code appears for a selection
In
the range 0
to
7.
100
REMark
REPeat
in
FOR
110
PAPER
0:
CLS
1Z0
FOR
up =
30
TO
80
130
LET
across
=
19
140
REPeat
dot
s
150
LET
co
tour
=
RND
(7)
160
INK
coLour
170
LET
across:::;
across
+ 1
180
POINT
across,
up
190
IF
coLour
= 0
then
EXIT
dots
ZOO
END
REPeat
dots
Z10
END
FOR
up
Much
of
the wisdom about program control and structure can be expressed
In
two
rules:
1.
Construct your program
uSing
only the legitimate structures
for
loops and decision-
making.
2.
Each structure should be properly related
in
sequence or wholly within another.
BINARY
DECISIONS
The three types of binary decision can be illustrated easily
in
terms
of
what
to
do
when
it
rains.
100
REMark
Short
form
IF
110
LET
rain
=
RND(O
TO
1)
120
IF
rain
THEN
PRINT
"Open
brotty"
ii.
100
REMark Long
form
IF
•••
END
IF
110
LET
rain
=
RND<O
TO
1)
1Z0
IF
rain
THEN
130
PRINT
"Wear
coat"
140
PRINT
"Open
brotly"
150
PRINT "WaLk
fast"
160
END
IF
100
REMark
Long
form
IF
...
ELSE
•••
END
IF
110
LET
rain
=
RND(O
TO
1)
1Z0
IF
rain
THEN
130
PRINT
"Take
a
bus"
140
ELSE
150 PRINT
"Walk"
160
END
IF
•
•
•
82
12/84