ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 10.1 - If...
All the program we've seen so far have been pretty predictable - they went straight through doing all the instructions, & then maybe went back to the beginning again. This is not all that useful. In practice the computer would be expected to make decisions & act accordingly; it does this using the IF statement.

Clear the computer (using NEW), & type in & run this terribly amusing little program:

10 PRINT "SHALL I TELL YOU A JOKE?"

20 INPUT A$

30 IF A$="GET LOST" THEN GOTO 200

40 PRINT "HOW MANY LEGS HAS A HORSE GOT?"

50 INPUT LEGS

60 IF LEGS=6 THEN GOTO 100

70 PRINT "NO, 6, FORE LEGS IN FRONT","AND TWO BEHIND."

80 STOP

100 PRINT "YES",,"SHALL I TELL YOU IT AGAIN?"

110 GOTO 20

200 PRINT "ALL RIGHT, THEN, I WONT."

Before we discuss the IF statement, you should first look at the STOP statement in line 80: a STOP statement stops the execution of the program, giving report 9.

Now as you can see, an IF statement takes the form

IF condition THEN statement

The statements here are GOTO statements, but they could be anything at all, even more IF statements. The condition is something that is going to be worked out as either true or false; if it comes out as true then the statement after THEN is executed, but otherwise it is skipped over.

The most useful conditions compare two numbers or two strings: they can test whether two numbers are equal, or whether one is bigger than the other; & can test whether two strings are equal, or whether one comes before the other in alphabetical order. They use the relations =, <, >, <=, >= and <>.

=, which we have used twice in the program (once for numbers & once for strings) means 'equals'. It is not the same as the = in a LET statement.

< means 'is less than', so that

1<2

-2<-1

&      -3<1

are all true, but

1<0

&      0<-2

are false.

To see how this works, let us write a program to input numbers & display the biggest so far.

10 PRINT "NUMBER","BIGGEST SO FAR"

20 INPUT A

30 LET BIGGEST=A

40 PRINT A,BIGGEST

50 INPUT A

60 IF BIGGEST < A THEN LET BIGGEST=A

70 GOTO 40

The crucial part is line 60, which updates BIGGEST if its old value was smaller than the new input number A.

> (shifted M) means 'is greater than', & 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.

<= (shifted R - do not type it as < followed by =) means 'is less than or equal to', so that it is like < except that it holds even if the two numbers are equal: this 2<=2 holds, but 2<2 does not.

>= (shifted Y) means 'is greater than or equal to' & is similarly like >.

<> (shifted T) means 'is not equal to' the opposite in meaning to =.

All six of these relational operations have priority 5.

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

These relations can be combined using the logical operations AND, OR & NOT.

one relation AND another relation

is true whenever both relations are true.

one relation OR another

is true whenever one of the two relations is true (or both are).

NOT relation

is true whenever the relation is false and is false whenever the relation is true.

Logical expressions can be made with relations & AND, OR & NOT just as numerical expressions can be made with numbers & +, - and so on; you can even put in brackets if necessary. NOT has priority 4, AND 3 & OR 2.

Since (unlike other functions) NOT has fairly low priority, its argument does not need brackets unless it contains AND or OR; so NOT A = B means NOT (A = B) (which is the same as A <> B).

To illustrate this, clear the computer & try this program.

10 INPUT F$

20 INPUT AGE

30 IF F$="X" AND AGE<18 OR F$="AA" AND AGE<14 THEN PRINT "DONT";

40 PRINT "LET IN."

50 GOTO 10

Here F$ is supposed to be the category if a film - X for 18 years old & over, AA for 14 & over, & A or U for anyone, & the program works out whether a person of a given age is to be allowed to see the film.

Lastly, we can compare not only numbers, but also strings. We have seen '=' used in 'F$="X"', & you can even use the other five, < & so on.

So what does 'less than' mean for strings? One thing it does not mean is 'shorter than', so don't make that mistake. We make the definition that one string is less than another if it comes first in alphabetical order: thus

"SMITH"       < "SMYTHE"

"SMYTHE"    > "SMITH"

"BLOGGS"    < "BLOGGS-BLACKBERRY"

"BILLION"    < "MILLION"

"TCHAIKOVSKY"    < "WAGNER"

"DOLLAR"    < "POUND"

all hold. <= means 'is less than or equal to', & so on, just as for numbers.

Note: In some versions of BASIC - but not on the ZX81 -, the IF statement can have the form

IF condition THEN line number

This means the same as

IF condition THEN GOTO line number

Summary

Statements: IF, STOP

Operations: =, <, >, <=, >=, <>, AND, OR

Function: NOT

Sinclair ZX Spectrum

  Previous Page Back Next Page