Section 6.2 - Boolean

Ada also predefines a simple type called Boolean, which can only have two values, True and False. All of the comparison operations (=, >=, /=, etc.) have result values of type Boolean. All conditions (such as what goes after an if and while) must be of type Boolean.

There are a few special infix operations that take two Booleans and result in a Boolean: "and", "or", and "xor" (exclusive-or), with their usual meanings. The value of `True and False' is False, while the value of `True or False' is True. `Exclusive or' is true if either of two conditions, but not both, is true.

There is also the prefix operation ``not''. If a boolean variable A has the value True, `not A' has the value False.

Although you can probably guess what the values of any combination of values is, here they are officially:

--- When ---+------------------- Then -----------------
A     B     | (A and B)   (A or B)   (A xor B)  (not A)
True  True  |   True        True       False     False
True  False |   False       True       True      False
False True  |   False       True       True      True
False False |   False       False      False     True

Normally Ada will evaluate these expressions in whatever order is most efficient for the machine. If it's important to evaluate them in a certain order and to stop evaluating them when the answer is known, there are versions of `and' and `or' that are called `short-circuit operations'. These operations will execute strictly left-to-right and will not execute anything if they don't have to. C's && and || operations work this way. The short-circuit version of `and' is `and then'; the short-circuit version of `or' is `or else'. For example, if you want to do something if K isn't zero and 1.0/K is more than B, but you realize that the latter test must be done after the former:

 if K /= 0 and then 1.0/Float(K) > B then ...

Quiz:

Which of the following is True?

  1. (True and False)
  2. not (True or False)
  3. True and then True

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 6 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s6s2.htm".