IF number<0 THEN PRINT "Negative number":
GO TO 100: ELSE IF number>0 THEN PRINT
"Positive number": GO TO 200: ELSE PRINT
"The number is zero" : zero += 1: GO TO
300

will allow you to jump to different parts of the program dependent on the results of the IF...THEN...ELSE statements; in this case, whether the number is negative, positive or zero (note that if the number is zero, one is added to the variable zero as well).

IF...THEN...ELSE however only works within a single program line and as a consequence it can be bulky and even somewhat unreadable. For that reason a longer form variation exists:

IF condition [ELSE [IF condition]] action END IF

It is immediately apparent that there is no THEN clause. This is what determines if it's the long, medium or short form of the IF structure. Medium and short forms use THEN and are thus single program line structures whereas the absence of THEN makes it the long form one. The long form IF has some specific syntax requirements. First off IF condition needs to be the first statement on its line and so are ELSE and ELSE IF. Secondly, the whole IF structure's command' sequence must be terminated by an END IF (which also needs to be the first statement on its line). Consider this example:

100 IF x>7:PRINT "x>7"
112    IF x>1000
113       PRINT "In fact it's
     huge"
114    ELSE
115       PRINT "But not too
     big"
116    ENDIF
120 ELSE IF x>3:PRINT "x>3 but
     x<=7"
140 ELSE IF x=3
150   PRINT "x=3"
 160 ELSE
170   PRINT "x is too small to
     bother"
180 ENDIF

It vividly demonstrates how a combination of nesting IF...ELSE...ENDIF structures within an external superset of IF...ELSE...ENDIF can allow multiple decisions in an easy straightforward way. The main decision is contained in the initial IF on line 100 and the ensuing ELSE clause on line 160. There we're checking if x is greater than 7 or not. Obviously x can be smaller than 7 but how small is immaterial? This gets asked by the ELSE IF clauses on lines 120 and 140 where it's decided that for values from 3 (line 140) up to and including 7 (line 120) – the value of greater than 7 has been dealt with on line 100 as we saw – it does indeed matter. There is however a set of values that x can have that is greater than 7 and that's what the nested IF...ELSE...ENDIF structure on lines 112 through 116 allows us to test against where we further break it down to values greater than 1000 or less or equal to 1000 (but still greater than 7). The PRINT statements are there to demonstrate there's actions we can take per decision and could easily have been GO TO or INPUT or other sets of keywords actually doing something according to the value of x. Moreover, there are two styles of writing on display here. The compact one (lines 100 and 120) and the more relaxed with lots whitespace one (lines 140 to 180).


ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.