 Ada '83 Rationale, Sec 3.6: Expressions

## CHAPTER 3: Classical Programming

### 3.6 Expressions

An expression is a formula that defines the computation of a value by the application of operators. Six classes of operators exist in Ada. They are listed below in increasing order of precedence:

 ```logical and or xor relational = /= < > <= >= binary adding + - & unary adding + - multiplying * / mod rem highest precedence ** abs not ```

When defining levels of precedence it is very important to follow common usage, and we found six levels to be the minimum number compatible with accepted practice.

Different levels are clearly required for relational, binary adding, unary adding, multiplying, and exponentiation operators. These levels have a very deep intuitive foundation, going back to algebra learned at school.

Logical operators must be on a different (and lower) level if expressions such as

 ```TODAY = MON or TODAY = SAT LOWER_BOUND <= X and X <= UPPER_BOUND ```

are to be written without parentheses. In the case of a succession of operators with the same precedence, Ada adopts the traditional rule of binding from left to right. However, the syntax recognizes that intuition of logical operators is not as deeply rooted as in the case of arithmetic operators. For example, consider the following expression (not allowed in Ada) written in two different ways:

 ```COLD and SUNNY or HUMID COLD and SUNNY or HUMID ```

Many programmers are likely to rely on the spacing for their interpretation of such expressions, whereas nobody would be fooled by misleading spacing in the case of arithmetic operators

 ```X*Y + Z -- yes X * Y+Z -- no ```

For this reason, the syntax requires explicit parentheses in the case of a succession of different logical operators. For example:

 ```A or (B xor C) -- parentheses required (A or B) xor C -- parentheses required (not always equal to previous expression) A or (B and C) -- parentheses required A or B or C -- no need for parentheses A and B and C -- no need for parentheses ```

Finally, the precedence of abs is chosen higher than that of multiplying operators so that

 ```abs A * B -- and abs (A) * B ```

both mean (abs A) * B. For similar reasons, the precedence of not is higher than that of logical and relational operators so that

```    not SUNNY or WARM
```

is equivalent to

```   (not SUNNY) or WARM
```

In practice most expressions are simple. The inconvenience of parentheses should not be exaggerated, as programmers tend to introduce additional parentheses anyway for readability purposes (to reassure themselves when in doubt) whether they are required or not.

NEXTPREVIOUSUPTOCINDEX