ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 10.2 - If...

Exercises

1. <> and = are opposites in the sense that NOT A=B is the same as A<>B

&

NOT A<>B is the same as A=B

Persuade yourself that >= is opposite to <, and <= is opposite to > so that you can always get rid of NOT from in front of a relation by changing the relation to its opposite.

Also,

NOT (a first logical expression AND a second)

is the same as

NOT (the first) OR NOT (the second),

&

NOT (a first logical expression OR a second)

is the same as

NOT (the first) AND NOT (the second).

Using this you can work NOTs through brackets until eventually they are all applied to relations, & then you can get rid of them. Thus, logically speaking, NOT is unnecessary. You might still find that using it makes a program clearer.

2. BASIC can sometimes work along different lines from English. Consider, for instance, the English clause 'if A doesn't equal B or C'. How would you write this in BASIC? [The answer is not

['IF A<>B OR C' nor 'IF A<>B OR A<>C']

Don't worry if you don't understand exercises 3, 4 & 5, the points covered in them are rather refined.

3. (For experts.)

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.

    (i) =, <, >, <=, >=, and <> are all number valued binary operations, with priority 5. The result is 1 (for true) if the relation holds, & 0 (for false) if it does not.

    (ii) In

IF condition THEN statement

the condition can actually be any numeric expression. If its value is 0, then it counts as false, & any other value counts as true. This IF statement means exactly the same as

IF condition <>0 THEN statement

    (iii) AND, OR & NOT are also number valued operations.

X AND Y has the value
          { X if Y is non-zero (counting as true) }
          { 0 if Y is zero (counting as false) }

X OR Y has the value
          { 1 if Y is non-zero }
          { X if Y is zero }

NOT X has the value
          { 0 if X is non-zero }
          { 1 if X is zero }

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 & NOT X, X and Y will each usually take the value 0 or 1, for false or true. Work out the ten different combinations & check that they do what you expect AND, OR & NOT to do.

4. Try this program:

10 INPUT A

20 INPUT B

30 PRINT (A AND A>=B)+(B AND A< B)

40 GOTO 10

Each time it prints the larger of the two numbers A & B - why?

Convince yourself that you can think of

X AND Y

as meaning

'X if Y (else the result is 0)'

& of

X OR Y

as meaning

'X unless Y (in which case the result is 1)'

An expression using AND & OR like this is called a conditional expression. An example using OR could be

LET RETAIL PRICE=PRICE LESS VAT*(1.15 OR V$="ZERO RATED")

Notice how AND tends to go with addition (because its default value is 0), & OR tends to go with multiplication (because its default value is 1).

5. You can also make string valued conditional expressions, but only using AND.

X$ AND Y$ has the value
          { X$ if Y is non-zero }
          { if Y is zero }

so it means 'X$ if Y (else the empty string)'.

Try this program, which inputs two strings & puts them in alphabetical order.

10 INPUT A$

20 INPUT B$

30 IF A$<=B$ THEN GOTO 70

40 LET C$=A$

50 LET A$=B$

60 LET B$=C$

70 PRINT A$;" ";("<" AND A$ < B$)+("=" AND A$=B$);" ";B$

80 GOTO 10

6. Try this program:

10 PRINT "X"

20 STOP

30 PRINT "Y"

When you run it, it will display "X" & stop with report 9/20. Now type

CONT

You might expect this to behave like 'GOTO 20', so that the computer would just stop again without displaying "Y"; but this would not be very useful, so things are arranged so that for reports with code (STOP statement executed), the line number is increased by 1 for a CONT statement. This in our example, 'CONT' behaves like 'GOTO 21' (which, since there are no lines between 20 & 30, behaves like 'GOTO 30').

7. Many versions of BASIC (but not the ZX81 BASIC) have an ON statement, which takes the form

ON numeric expression GOTO line number, line number,...,line number In this the numeric expression is evaluated;

suppose its value is n then the effect is that of.

GOTO the nth line number

For instance

ON A GOTO 100, 200, 300, 400, 500

Here, if A has the value 2, then 'GOTO 200' is executed. In ZX81 BASIC this can be replaced by

GOTO 100*A

In case the line numbers don't go up neatly by hundreds like this, work out how you could use

GOTO a conditional expression

instead.

Sinclair ZX Spectrum

  Previous Page Back Next Page