IF
condition
THEN GOTO
line number
Summary
Statements:
IF
,
STOP
Operations: =, <, >,
<=
,
>=
,
<>
,
AND
,
OR
Function:
NOT
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
NOT
s 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 the
IF
statement means exactly the same as
IF
condition
<>
0
THEN
statement
(iii)
AND
,
OR
&
NOT
are also number valued operations.