Sinclair QL Beginner's Manual page 99

Hide thumbs Also See for QL:
Table of Contents

Advertisement

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.
NESTED LOOPS
Consider the following FOR loop which PLOTS a row of points of various randomly 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 80. But you 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:
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
END FOR up

Advertisement

Table of Contents
loading

Table of Contents