Chapter 2 – Decisions

Making decisions

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).

Using IF… to make decisions

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:


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.