NOT (the first) AND NOT (the second)
Using this, you can work NOTs through parentheses until eventually they are all applied to relations, and then you can get rid of them. Logically speaking, NOT is unnecessary, although you might still find that using it makes a program clearer.
The following section is quite complicated, and can be skipped by the fainthearted!
Try:
PRINT 1=2,1<>2
which you might expect to give a syntax error. In fact, as far as the computer is concerned, there is no such thing as a logical value: instead it uses ordinary numbers, subject to a few rules.
=, <, >, <=, >= and <> all give numeric results: 1 for true, and 0 for false. Thus the PRINT command above printed 0 for 1=2, which is false, and 1 for 1<>2, which is true.
In: IF condition THEN ... the condition can be actually any numeric expression. If its value is 0, then it counts as false, and any other value (including the value of 1 that a true relation gives) counts as true. Thus the IF statement means exactly the same as:
IF condition <>0 THEN . . .
AND, OR and NOT are also number-valued operations.
| x AND y has the value | { | x if y is true (non-zero) |
| 0 (false), if y is false (zero) |
| x OR y has the value | { | 1 (true) if y is true (non-zero) |
| x, if y is false (zero) |
| NOT x has the value | { | 0 (false), if x is true (non-zero) |
| 1 (true), if x is false (zero) |
(Notice that true means non-zero when we're checking a given value, but it means 1 when we're producing a new one.)
Read through the chapter again in the light of this revelation, making sure that it all works.
In the expressions x AND y, x OR y and NOT x, x and y will usually take the values 0 and 1 for false and true. Work out the ten different combinations (four for AND, four for OR and two for NOT) and check that they do what the chapter leads you to expect them to do.
Try this program:
10 INPUT a
20 INPUT b
30 PRINT (a AND a>=b)+(b AND
a<b)
40 GO TO 10
Each time it prints the larger of the two numbers a and b.
Convince yourself that you can think of:
x AND y as meaning: x if y (else the result is 0)
and of:
x OR y as meaning: x unless y (in which case the result is 1)
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.