Learn Pascal
3B - Boolean Expressions

Boolean expressions are used to compare two values.

The simplest form of Boolean expression looks like this:
     value1 relational_operator value2

The following relational operators are used:
<less than
>greater than
=equal to
<=less than or equal to
>=greater than or equal to
<>not equal to

You can assign Boolean expressions to Boolean variables:
     some_bool := 3 < 5;
Of course, the value of some_bool becomes TRUE.

Complex Boolean expressions are formed by using the Boolean operators:
notnegation (~)
andconjunction (^)
ordisjunction (v)
xorexclusive-OR

NOT is a unary operator - it is applied to only one value and inverts it:
     not true = false
     not false = true

AND yields TRUE only if both expressions are TRUE.
     TRUE and FALSE = FALSE      TRUE and TRUE = TRUE

OR yields TRUE if either expression is TRUE, or if both are. The following are TRUE:

     TRUE or TRUE
     TRUE or FALSE
     FALSE or TRUE

XOR yields TRUE if one expression is TRUE and the other is FALSE. Thus,

     TRUE or TRUE = FALSE
     TRUE or FALSE = TRUE
     FALSE or TRUE = TRUE
     FALSE or FALSE = FALSE

When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses.
     (3>5) or (650<1)
This is because the Boolean operators are higher on the order of operations than the relational operators:

     not
     * / div mod and
     + - or
     < > <= >= = <>

This way,
     3 > 5 or 650 < 1
becomes evaluated as
     3 > (5 or 650) < 1
which makes no sense, because the Boolean operator or only works on Boolean values, not on integers.

The Boolean operators (AND, OR, NOT, XOR) can be used on Boolean variables just as easily as they are used on Boolean expressions.

Whenever possible, don't compare two real values with the equals sign. Small round-off errors may cause two equivalent expressions to differ.


Previous lesson

Next lesson

Contents

Index

e-mail me


taoyue@mit.edu
Copyright © 1997-2001 Tao Yue. All rights reserved.