Part 13

Subjects covered...


We saw in part 3 of this chapter how an IF statement takes the form...

	IF condition THEN...

The conditions there were the relations '=', '<', '>', '<=', '>=' and
'<>' which compare two numbers or two strings. You can also combine
several of these, using the logical operations AND, OR and NOT.

One relation AND another relation is true whenever both relations are
true, so that you could have a line like...

	IF a$="yes" AND x>0 THEN PRINT "result" which 'result' gets printed only if 'a$' is equal to 'yes' and
'x' is greater than zero. The BASIC here is so close to English that
it hardly seems worth spelling out the details. As in English, you can
join lots of relations together with AND, and then the whole lot is
true if all the individual relations are.

One relation OR another is true whenever at least one of the two
relations is true. (Remember that it is still if both the relations
are true - this is something that English doesn't always imply.)

The NOT relationship turns things upside down. The NOT relation is
true whenever the relation is false, and false whenever it is true.

Logical expressions may use combinations of AND, OR and NOT, just as
numerical expressions may use combinations of '+', '-', '*' and so on.
You can even put them in brackets if necessary. Logical operations
have priorities in the same way as '+', '-', '*', '/' and ^ do. NOT
has the highest priority, then AND, then OR.

NOT is really a function, with an argument and a result, but its
priority is much lower than that of other functions. Therefore, its
argument does not need brackets unless it contains AND or OR (or
both). 'NOT a=b' means the same as 'NOT (a=b)' (and the same as
'a<>b' of course).

'<>' is the negation of '=' in the sense that it is true only if '='
is false. In other words...

	a<>b is the same as NOT a=b

...and also...

	NOT a<>b is the same as a=b

Convince yourself that '>=' and '<=' are the negations of '<' and '>'
respectively. Thus you can always get rid of NOT from in front of a
relation by changing the relation.


	NOT (a first logical expression AND a second) the same as...

	NOT (the first) OR NOT (the second)


	NOT (a first logical expression OR a second) 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, 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 easily be skipped
by the faint by the faint-hearted!


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

(i) '=', '<', '>', '<=', '>=' and '<>' all give the 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.

(ii) In the statement...

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

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

[The part after 'but' is at best misleading, and arguably wrong. Try
'PRINT 5 AND 1, 5 OR 0' to see what I mean - both of those produce
results (the first value given) which is not a 1. No wonder I was
confused when I first read this all those years ago. :(]

Now try this program...

	10 INPUT a
	20 INPUT b
	30 PRINT (a AND a >= b)+(b AND ab$ THEN LET c$=a$: LET a$=b$: LET b$=c$
	30 PRINT a$;" ";("<" AND a$
[Back] [Contents] [Next]