Sign in Join
Wiki copytemporary

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

Revision 1 to 2. 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
55`value1 `**`relational_operator`**` value2`
66
77The following relational operators are used:
88
99`<  less than`\
1010`>  greater than`\
1111`=  equal to`\
1212`<= less than or equal to`\
1313`>= greater than or equal to`\
1414`<> not equal to`
1515
1616You can assign Boolean expressions to Boolean variables. Here we assign a `true` expression to some_bool:
1717
1818``` pascal
1919 some_bool := 3 < 5;
2020```
2121
2222Complex Boolean expressions are formed by using the Boolean operators:
2323
2424`not    negation (~)`\
2525`and    conjunction (^)`\
2626`or disjunction (v)`\
2727`xor    exclusive-or`
2828
2929`NOT` is a unary operator — it is applied to only one value and inverts it:
3030
31-- <tt>not true = false
32-- not false = true</tt>
31+- not true = false
32+- not false = true
3333
3434`AND` yields `TRUE` only if both values are `TRUE`:
3535
36-- <tt>TRUE and FALSE = FALSE
37-- TRUE and TRUE = TRUE</tt>
36+- TRUE and FALSE = FALSE
37+- TRUE and TRUE = TRUE
3838
3939`OR` yields `TRUE` if at least one value is `TRUE`:
4040
41-- <tt>TRUE or TRUE = TRUE
41+- TRUE or TRUE = TRUE
4242- TRUE or FALSE = TRUE
4343- FALSE or TRUE = TRUE
44-- FALSE or FALSE = FALSE</tt>
44+- FALSE or FALSE = FALSE
4545
4646`XOR` yields `TRUE` if one expression is `TRUE` and the other is `FALSE`. Thus:
4747
48-- <tt>TRUE xor TRUE = FALSE
48+- TRUE xor TRUE = FALSE
4949- TRUE xor FALSE = TRUE
5050- FALSE xor TRUE = TRUE
51-- FALSE xor FALSE = FALSE</tt>
51+- FALSE xor FALSE = FALSE
5252
5353When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses.
5454
5555``` pascal
5656(3>5) or (650<1)
5757```
5858
5959This is because the Boolean operators are higher on the order of operations than the relational operators:
6060
61-1. <tt>not
61+1. not
62622. * / div mod and
63633. \+ - or
64-4. \< \> \<= \>= = \<\></tt>
64+4. \< \> \<= \>= = \<\>
6565
6666So `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.
6767
6868The Boolean operators (`AND, OR, NOT, XOR`) can be used on Boolean variables just as easily as they are used on Boolean expressions.
6969
7070Whenever possible, don't compare two real values with the equals sign. Small round-off errors may cause two equivalent expressions to differ.
7171
7272---
7373*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