Sign in Join
Wiki copytemporary

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

Revision 3 to 4. 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-```
5+```text
66value1 relational_operator value2
77```
88
99The following relational operators are used:
1010
11-```
11+```text
1212< less than
1313> greater than
1414= equal to
1515<= less than or equal to
1616>= greater than or equal to
1717<> not equal to
1818```
1919
2020You can assign Boolean expressions to Boolean variables. Here we assign a `true` expression to some_bool:
2121
2222``` pascal
2323 some_bool := 3 < 5;
2424```
2525
2626Complex Boolean expressions are formed by using the Boolean operators:
2727
28-```
28+```text
2929not negation (~)
3030and conjunction (^)
3131or disjunction (v)
3232xor exclusive-or
3333```
3434
3535`NOT` is a unary operator — it is applied to only one value and inverts it:
3636
3737- not true = false
3838- not false = true
3939
4040`AND` yields `TRUE` only if both values are `TRUE`:
4141
4242- TRUE and FALSE = FALSE
4343- TRUE and TRUE = TRUE
4444
4545`OR` yields `TRUE` if at least one value is `TRUE`:
4646
4747- TRUE or TRUE = TRUE
4848- TRUE or FALSE = TRUE
4949- FALSE or TRUE = TRUE
5050- FALSE or FALSE = FALSE
5151
5252`XOR` yields `TRUE` if one expression is `TRUE` and the other is `FALSE`. Thus:
5353
5454- TRUE xor TRUE = FALSE
5555- TRUE xor FALSE = TRUE
5656- FALSE xor TRUE = TRUE
5757- FALSE xor FALSE = FALSE
5858
5959When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses.
6060
6161``` pascal
6262(3>5) or (650<1)
6363```
6464
6565This is because the Boolean operators are higher on the order of operations than the relational operators:
6666
67671. not
68682. * / div mod and
69693. \+ - or
70704. \< \> \<= \>= = \<\>
7171
7272So `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.
7373
7474The Boolean operators (`AND, OR, NOT, XOR`) can be used on Boolean variables just as easily as they are used on Boolean expressions.
7575
7676Whenever possible, don't compare two real values with the equals sign. Small round-off errors may cause two equivalent expressions to differ.
7777
7878---
7979*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