Pages 16–20 · Markdown
All the programs we have seen so far have been predictable; they went straight through the instructions, then went back to the beginning again. This is not very useful. In practice the computer would be expected to make decisions and act accordingly. There are three ways NextBASIC helps you make decisions: The first is by using the IF keyword in a short, medium and long format, the second by using the ON case selection keyword and the third is by using the select operator ? (question mark).
The short and medium form of IF are as follows:
IF condition THEN action [ELSE alternative action]
Clear the previous program from memory by using NEW and type in and run the following example:
10 REM Guess the number
20 INPUT "Enter the number to
guess", a: CLS
30 INPUT "Guess the number",
b
40 IF b=a THEN PRINT "That is
correct": STOP
50 IF b<a THEN PRINT "That is
too small, try again"
60 IF b>a THEN PRINT "That is
too big, try again"
70 GO TO 30
You can see that in its simplest form an IF statement is:
IF condition THEN action
where action stands for a sequence of commands, separated by colons in the usual way. The condition is something that is going to be worked out as either true or false; if it comes out as true then the statements in the rest of the line after THEN are executed, but otherwise they are skipped over, and the program executes the next instruction.
The simplest conditions compare two numbers or two strings: they can test whether two numbers are equal or whether one is bigger than the other; and they can test whether two strings are equal, or (roughly) one comes before the other in alphabetical order. They use the relations =, <, >, <=, >= and <>. Additionally we can use NOT, AND, OR, ! (bitwise NOT), | (bitwise OR), & (bitwise AND), ^| (bitwise XOR) all of which produce a true (1) or false (0) result
= means equals. Although it is the same symbol as the = in a LET command, it is used in quite a different sense.
< means is less than so that:
1 < 2
-2 <-1
-3 < 1
are all true, but:
1 < 0
0 <-2
are false.
means is greater than, and is just like < but the other way round. You can remember which is which, because the thin end points to the number that is supposed to be smaller.
<= means is less than or equal to, so that it is like < except that it is true even if the two numbers are equal: thus 2<=2 is true, but 2<2 is false.
= means is greater than or equal to and is similarly like >.
<> means is not equal to, the opposite in meaning to =.
The remaining operators are explained in length in Chapter 6 – Expressions.
Mathematicians usually write <=, >= and <> as ≤, ≥ and ≠. They also write things like 2<3<4 to mean 2<3 and 3<4, but this is not possible in NextBASIC.
Line 40 compares a and b. If they are equal then the program is halted by the STOP command. The report at the bottom of the screen 9 STOP, statement, 30:3 shows that the third statement, or command, in line 30 caused the program to halt, i.e. STOP.
Line 50 determines whether b is less than a, and line 60 whether b is greater than a. If one of these conditions is true then the appropriate comment is printed, and the program works its way to line 70 which tells the computer to go back to line 30 and start all over again. The CLS command in line 20 clears the screen to stop the other person seeing what you put in.
Note: in some versions of BASIC the IF statement can have the form:
IF condition THEN line number
This means the same as:
IF condition THEN GO TO line number/label
By adding the optional ELSE clause, more complex decisions can be made. This instructs the computer to run another set of commands if the IF...THEN test turns out to be false. It is important to note that, unlike some other implementations of BASIC, ELSE must follow a colon within a statement line; for instance:
IF number<0 THEN PRINT "Negative number":
ELSE PRINT "Positive number"
In the example above, if the condition is true (that is, the number is less than zero) then Negative number will be printed. If not, then Positive number will be printed on screen. But what if you for example wanted a third option to tell if the number is zero? You could use the ability to "nest" IF...THEN statements and use the ELSE clause to do so. Lets' rewrite the above:
IF number<0 THEN PRINT "Negative number":
ELSE IF number>0 THEN PRINT "Positive
number": ELSE PRINT "The number is zero
You should see in the above that it is possible to execute a further IF...THEN statement if the condition in the original one was false. NextBASIC will work through the IF...THEN statements until it finds a condition that is true, and will execute that. If no conditions are true, then it will attempt to execute the final ELSE. More than one command can be executed within each part of an IF...THEN...ELSE statement also, so:
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).
Many BASIC dialects contain a special structure called SELECT CASE which is a specialised version of IF...ELSE IF...ENDIF that's much easier to use and read. NextBASIC has a somewhat similar keyword called ON. Its syntax is as follows:
ON n:<stmt0>:<stmt1>:<stmt2>:...:<stmtn>[: ELSE <statements>]
ON uses the resulting integer of rounding the value of expression n to the closest integer and executes the nth statement of the same line that follows it.
When the statement is executed, ON skips the rest of the line and moves to the next line, unless the statement was a non-returning jump such as GO TO, EXIT, RETURN or ENDPROC. If ON runs out of statements (in other words the integer part of n is greater than the number of statements that follows it, then, if the optional ELSE clause is present, all the statements that follow ELSE are executed otherwise the entire line is skipped. For example:
100 ON x:GO TO @xwaszero:GO TO
@xwasone:STOP: GOSUB 200:
ELSE BEEP 1,0: PRINT "x
was > 3":STOP
110 PRINT "execution
returned":STOP
200 PRINT "I'm in the
subroutine now"
210 RETURN
It's easy to understand that if x is 2 then the program will terminate, if it's 0 it will jump to label @xwaszero, if it's 1 it will jump to label @xwasone, if it was 3 it will jump to the subroutine on line 200, return and continue at line 110 and in every other case the computer will beep and inform us that x was greater than 3 before finally halting. While at first sight this may not display much improvement over the nested IF...ELSE...END IF structure especially since it can only check for values greater than 0, in reality however you can prepare the variable to be checked ahead of time and simply test for that and moreover it is much simpler even typing-wise and more concise.
A final way of making decisions but this time within the confines of a single statement that accepts a parameter is provider via the select operator ? (question mark). This has the following form:
n?(expr0,expr1,expr2...)
If this reminds you a bit of the ON keyword syntax above you'd be correct as based on the value of n the nth expression on the list is evaluated. If you run out of expressions in the list then the last expression is always evaluated. The select operator can take either numeric (be it floating point or integer) or string values in its evaluation list, however they all have to be the same. Although it's not immediately apparent how one could use it in te decision-making process, type and RUN the following example which will make it all clear:
10 INPUT "Enter a number "; n
20 PRINT n?(%5, %10, %20)
30 PRINT n?("n is zero", "n is
one", "n is two", "n is
three", "n is four or
more")
40 GO TO n?(100, 60, 120, 130,
10)
50 STOP
60 PRINT "n was one": STOP
100 PRINT "n was zero": STOP
120 PRINT "n was two": STOP
130 PRINT "n was three": STOP
(Do not mind the % symbols in the list of line 20; we will learn more about them in Chapter 6 – Expressions). As you enter values in the program, you will see how your input affects both the PRINT statements but also (and here's the important part) the GO TO statement on line 40 in effect creating a conditional GO TO. Moreover, instead of having to type 5 different GO TO statements like we would have in the case of IF...ELSE...END IF and ON, we only have to type one making it a great time-saver.
Obviously the ability of the select operator to take either string or numeric expressions as parameters makes it useful for many things like for example a conditional LET apart from changing the flow of our program, so other than just line numbers we just used, we can make it calculate and further enhance our decision making as we'll see in Chapter 6.
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.