At school most of us are taught that multiplications must be done before additions in a sum.
In E it's different--there is no operator precedence, and the normal order in which the operations are performed is left-to-right, just like the expression is written.
This means that expressions like 1+3*3
do not give the results a mathematician might expect.
In fact, 1+3*3
represents the number 12 in E.
This is because the addition, 1+3
, is done before the multiplication, since it occurs before the multiplication.
If the multiplication were written before the addition it would be done first (like we would normally expect).
Therefore, 3*3+1
represents the number 10 in E and in school mathematics.
To overcome this difference we can use parentheses to group the expression.
If we'd written 1+(3*3)
the result would be 10.
This is because we've forced E to do the multiplication first.
Although this may seem troublesome to begin with, it's actually a lot better than learning a lot of rules for deciding which operator is done first (in C this can be a real pain, and you usually end up writing the brackets in just to be sure!).
The logic examples above contained the expression:
(2<1) AND (-1=0)
This expression was false. If we'd left the parentheses out, it would have been:
2<1 AND -1=0
This is actually interpreted the same as:
((2<1) AND -1) = 0
Now the number -1 shouldn't really be used to represent a truth value with AND
, but we do know that TRUE
is the number -1, so E will make sense of this and the E compiler won't complain.
We will soon see how AND
and OR
really work (see 10.4.3 Bitwise AND
and OR
), but for now we'll just work out what E would calculate for this expression:
2<1
can be replaced by FALSE
.
(FALSE AND -1) = 0
TRUE
is -1 so we can replace -1 by TRUE
.
(FALSE AND TRUE) = 0
FALSE AND TRUE
is FALSE
.
(FALSE) = 0
FALSE
is really the number zero, so we can replace it with zero.
0 = 0
TRUE
.
TRUE
So E calculates the expression to be true. But the original expression (with parentheses) was false. Bracketing is therefore very important! It is also very easy to do correctly.
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.