Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/Boolean Expressions: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
113B - Boolean Expressions (author: Tao Yue, state: unchanged)
22
33[[Boolean]] expressions are used to compare two values and get a `true-or-false` answer:
44
5-`value1 `**`relational_operator`**` value2`
5+```
6+value1 relational_operator value2
7+```
68
79The following relational operators are used:
810
9-`<  less than`\
10-`>  greater than`\
11-`=  equal to`\
12-`<= less than or equal to`\
13-`>= greater than or equal to`\
14-`<> not equal to`
11+```
12+< less than
13+> greater than
14+= equal to
15+<= less than or equal to
16+>= greater than or equal to
17+<> not equal to
18+```
1519
1620You can assign Boolean expressions to Boolean variables. Here we assign a `true` expression to some_bool:
1721
1822``` pascal
1923 some_bool := 3 < 5;
2024```
2125
2226Complex Boolean expressions are formed by using the Boolean operators:
2327
24-`not    negation (~)`\
25-`and    conjunction (^)`\
26-`or disjunction (v)`\
27-`xor    exclusive-or`
28+```
29+not negation (~)
30+and conjunction (^)
31+or disjunction (v)
32+xor exclusive-or
33+```
2834
2935`NOT` is a unary operator — it is applied to only one value and inverts it:
3036
3137- not true = false
3238- not false = true
3339
3440`AND` yields `TRUE` only if both values are `TRUE`:
3541
3642- TRUE and FALSE = FALSE
3743- TRUE and TRUE = TRUE
3844
3945`OR` yields `TRUE` if at least one value is `TRUE`:
4046
4147- TRUE or TRUE = TRUE
4248- TRUE or FALSE = TRUE
4349- FALSE or TRUE = TRUE
4450- FALSE or FALSE = FALSE
4551
4652`XOR` yields `TRUE` if one expression is `TRUE` and the other is `FALSE`. Thus:
4753
4854- TRUE xor TRUE = FALSE
4955- TRUE xor FALSE = TRUE
5056- FALSE xor TRUE = TRUE
5157- FALSE xor FALSE = FALSE
5258
5359When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses.
5460
5561``` pascal
5662(3>5) or (650<1)
5763```
5864
5965This is because the Boolean operators are higher on the order of operations than the relational operators:
6066
61671. not
62682. * / div mod and
63693. \+ - or
64704. \< \> \<= \>= = \<\>
6571
6672So `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.
6773
6874The Boolean operators (`AND, OR, NOT, XOR`) can be used on Boolean variables just as easily as they are used on Boolean expressions.
6975
7076Whenever possible, don't compare two real values with the equals sign. Small round-off errors may cause two equivalent expressions to differ.
7177
7278---
7379*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](/wiki-contact).*

Back to history