Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


3.2.2 Logic and comparison

Logic lies at the very heart of a computer. They rarely guess what to do next; instead they rely on hard facts and precise reasoning. Consider the password protection on most games. The computer must decide whether you entered the correct number or word before it lets you play the game. When you play the game it's constantly making decisions: did your laser hit the alien?, have you got any lives left?, etc. Logic controls the operation of a program.

In E, the constants TRUE and FALSE represent the truth values true and false (respectively), and the operators AND and OR are the standard logic operators. The comparison operators are `=' (equal to), `>' (greater than), `<' (less than), `>=' (greater than or equal to), `<=' (less than or equal to) and `<>' (not equal to). All the following expressions are true:

  TRUE
  TRUE AND TRUE
  TRUE OR FALSE
  1=1
  2>1
  3<>0

And these are all false:

  FALSE
  TRUE AND FALSE
  FALSE OR FALSE
  0=2
  2<1
  (2<1) AND (-1=0)

The last example must use parentheses. We'll see why in the next section (it's to do with precedence, again).

The truth values TRUE and FALSE are actually numbers. This is how the logic system works in E. TRUE is the number -1 and FALSE is zero. The logic operators AND and OR expect such numbers as their parameters. In fact, the AND and OR operators are really bit-wise operators (see 10.4.3 Bitwise AND and OR), so most of the time any non-zero number is taken to be TRUE. It can sometimes be convenient to rely on this knowledge, although most of the time it is preferable (and more readable) to use a slightly more explicit form. Also, these facts can cause a few subtle problems as we shall see in the next section.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.