Sinclair QL Beginner's Manual page 98

Hide thumbs Also See for QL:
Table of Contents

Advertisement

In addition the REPeat loop must normally have an EXIT amongst the statements or it will never end.
Note also that the EXIT statement causes control to go to the statement which is immediately after the
END of the loop.
A NEXT statement may be placed in a loop. It causes control to go to the statement which is just after
the opening keyword FOR or REPeat. It should be considered as a kind of opposite to the EXIT
statement. By a curious coincidence the two words, NEXT and EXIT, both contain EXT. Think of an
EXTension to loops and:
N means "Now start again"
I means "It's ended"
EXAMPLE 3
The situation is the same as in example 1. The sheriff has a gun loaded with six bullets and he is to
fire at the bandit but two more conditions apply:
1. If he hits the bandit he stops firing and returns to Dodge City
2. If he runs out of bullets before he hits the bandit, he tells his partner to watch the bandit while he
(sheriff) returns to Dodge City
Program 1
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.
Program 2
100 REMark Western REPeat with Epilogue
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 - 1
180
IF bullets <> 0 THEN NEXT Bandit
190
PRINT "Watch Bandit"
200 END REPeat Bandit
210 PRINT "Return to Dodge City"

Advertisement

Table of Contents
loading

Table of Contents