<!-- PDF page 71 -->

## Chapter 12 – Conditions

### AND, OR and NOT

We saw in *Chapter 2 how an* **IF** statement takes the form:

**IF** *condition* ...

Apart from expressions that generate **true** (**1**) or **false** (**0**) results, 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 you could have a line like:

```
IF a$="yes" AND x>0 THEN PRINT x
```

in which **x** only gets printed **if a$="yes" and x>0**. The syntax 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 *true* if both the relations are *true*; this is not always implied in English).

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*, can be made with relations and **AND**, **OR** and **NOT**, just as numerical expressions can be made with numbers and +, - and so on; you can even put them in parentheses if necessary. They have priorities in the same way as the usual operations +, -, **\***, / and ↑ do: **OR** has the lowest priority, then **AND**, then **NOT**, then the relations, and the usual operations.

**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 parentheses 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 if, and 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**

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

Also:

**NOT** (*a first logical expression* **AND** *a second*)

is the same as:

**NOT** (*the first*) **OR NOT** (*the second*)

and:

**NOT** (*a first logical expression* **OR** *a second*)

is the same as:

