Sinclair QL Beginner's Manual page 54

Hide thumbs Also See for QL:
Table of Contents

Advertisement

FOR loop. The program below produces scores of two dice in each row until a seven occurs, instead
of crosses.
100 REMark Dice rows
110 FOR row = 1 TO 4
120 PRINT 'Row number '! row
130 REPeat throws
140
LET die1 = RND(1 TO 6)
150
LET die2 = RND(1 TO 6)
160
LET score = die1 + die2
170
PRINT ! score !
180
IF score = 7 THEN EXIT throws
190 END REPeat throws
200 PRINT \'End of row '! row
210 END FOR row
sample output:
Row number 1
8 11 6 3 7
End of row number 1
Row number 2
4 6 2 9 4 5 12 7
End of row number 2
Row number 3
7
End of row number 3
Row number 4
6 2 4 9 9 7
End of row number 4
The third feature of loops in SuperBASIC allows more flexibility in providing the range of values in a
FOR loop. The following program illustrates this by printing all the divisible numbers from 1 to 20. (A
divisible number is divisible evenly by a number other than itself or 1.)
100 REMark Divisible numbers
110 FOR num = 4,6,8, TO 10,12,14 TO 16,18, 20
120
PRINT ! num !
130 END FOR num
More will be said about handling repetition in a later chapter but the features described above will
handle all but a few uncommon or advanced situations.
DECISION MAKING
You will have noticed the simple type of decision:
IF die = 6 THEN EXIT throws
This is available in most BASICs but SuperBASIC offers extensions of this structure and a completely
new one for handling situations with more than two alternative courses of action.
However, you may find the following long forms of IF..THEN useful. They should explain themselves.
(i)
100 REMark Long form IF. ..END IF
110 LET sunny = RND(0 TO 1)
120 IF sunny THEN
130 PRINT 'Wear sunglasses'
140 PRINT 'Go for walk'

Advertisement

Table of Contents
loading

Table of Contents