Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/Boolean Expressions

Sent here from Boolean Expressions.

English · Français · Български · 中文(中国) · 日本語

3B - Boolean Expressions (author: Tao Yue, state: unchanged)

Boolean expressions are used to compare two values and get a true-or-false answer:

text
value1 relational_operator value2

The following relational operators are used:

text
<   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. Here we assign a true expression to some_bool:

pascal
 some_bool := 3 < 5;

Complex Boolean expressions are formed by using the Boolean operators:

text
not negation (~)
and conjunction (^)
or  disjunction (v)
xor exclusive-or

NOT is a unary operator — it is applied to only one value and inverts it:

AND yields TRUE only if both values are TRUE:

OR yields TRUE if at least one value is TRUE:

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

When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses.

pascal
(3>5) or (650<1)

This is because the Boolean operators are higher on the order of operations than the relational operators:

  1. not
    • / div mod and
  2. + - or
  3. < > <= >= = <>

So 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.


From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/Boolean Expressions", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 4, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.