Sinclair QL Beginner's Manual page 55

Hide thumbs Also See for QL:
Table of Contents

Advertisement

150 END IF
(ii)
100 REMark Long form IF...ELSE...END IF
110 LET sunny = RND(0 TO 1)
120 IF sunny THEN
130 PRINT 'Wear sunglasses'
140 PRINT 'Go for walk'
150 ELSE
160 PRINT 'Wear coat'
170 PRINT 'Go to cinema'
180 END IF
The separator THEN, is optional in long forms or it can be replaced by a colon in short forms. The
long decision structures have the same status as loops. You can nest them or put other structures
into them. When a single variable appears where you expect a condition the value zero will be taken
as false and other values as true.
SUBROUTINES AND PROCEDURES
Most BASICs have a GOSUB statement which may be used to activate particular blocks of code
called subroutines. The GOSUB statement is unsatisfactory in a number of ways and SuperBASIC
offers properly named procedures with some very useful features.
Consider the following programs both of which draw a green 'square' of side length 50 pixel screen
units at a position 200 across 100 down on a red background.
(a) Using GOSUB
100 LET colour = 4 : background = 2
110 LET across = 20
120 LET down = 100
130 LET side = 50
140 GOSUB 170
150 PRINT 'END'
160 STOP
170 REMark Subroutine to draw square
180 PAPER background : CLS
190 BLOCK side, side, across, down, colour
200 RETurn
(b) Using a procedure with parameters
100 square 4, 50, 20, 100, 2
110 PRINT 'END'
120 DEFine PROCedure square(colour,side,across,down,background)
130 PAPER background : CLS
140 BLOCK side, side, across, down, colour
150 END DEFine
In the firs t program the values of colour, across, down, side are fixed by LET statements before the
GOSUB statement activates lines 180 and 190 Control is then sent back by the RETURN statement.
In the second program the values are given in the first line as parameters in the procedure call,
square, which activates the procedure and at the same time provides the values it needs.
In its simplest form a procedure has no parameters. It merely separates a particular piece of code,
though even in this simpler use the procedure has the advantage over GOSUB because it is properly
named and properly isolated into a self contained unit.

Advertisement

Table of Contents
loading

Table of Contents