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.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | 3B - Boolean Expressions (author: Tao Yue, state: unchanged) | |
| 2 | 2 | ||
| 3 | 3 | [[Boolean]] expressions are used to compare two values and get a `true-or-false` answer: | |
| 4 | 4 | ||
| 5 | - | ||
| 5 | + | ```text | |
| 6 | 6 | value1 relational_operator value2 | |
| 7 | 7 | ``` | |
| 8 | 8 | ||
| 9 | 9 | The following relational operators are used: | |
| 10 | 10 | ||
| 11 | - | ||
| 11 | + | ```text | |
| 12 | 12 | < less than | |
| 13 | 13 | > greater than | |
| 14 | 14 | = equal to | |
| 15 | 15 | <= less than or equal to | |
| 16 | 16 | >= greater than or equal to | |
| 17 | 17 | <> not equal to | |
| 18 | 18 | ``` | |
| 19 | 19 | ||
| 20 | 20 | You can assign Boolean expressions to Boolean variables. Here we assign a `true` expression to some_bool: | |
| 21 | 21 | ||
| 22 | 22 | ``` pascal | |
| 23 | 23 | some_bool := 3 < 5; | |
| 24 | 24 | ``` | |
| 25 | 25 | ||
| 26 | 26 | Complex Boolean expressions are formed by using the Boolean operators: | |
| 27 | 27 | ||
| 28 | - | ||
| 28 | + | ```text | |
| 29 | 29 | not negation (~) | |
| 30 | 30 | and conjunction (^) | |
| 31 | 31 | or disjunction (v) | |
| 32 | 32 | xor exclusive-or | |
| 33 | 33 | ``` | |
| 34 | 34 | ||
| 35 | 35 | `NOT` is a unary operator — it is applied to only one value and inverts it: | |
| 36 | 36 | ||
| 37 | 37 | - not true = false | |
| 38 | 38 | - not false = true | |
| 39 | 39 | ||
| 40 | 40 | `AND` yields `TRUE` only if both values are `TRUE`: | |
| 41 | 41 | ||
| 42 | 42 | - TRUE and FALSE = FALSE | |
| 43 | 43 | - TRUE and TRUE = TRUE | |
| 44 | 44 | ||
| 45 | 45 | `OR` yields `TRUE` if at least one value is `TRUE`: | |
| 46 | 46 | ||
| 47 | 47 | - TRUE or TRUE = TRUE | |
| 48 | 48 | - TRUE or FALSE = TRUE | |
| 49 | 49 | - FALSE or TRUE = TRUE | |
| 50 | 50 | - FALSE or FALSE = FALSE | |
| 51 | 51 | ||
| 52 | 52 | `XOR` yields `TRUE` if one expression is `TRUE` and the other is `FALSE`. Thus: | |
| 53 | 53 | ||
| 54 | 54 | - TRUE xor TRUE = FALSE | |
| 55 | 55 | - TRUE xor FALSE = TRUE | |
| 56 | 56 | - FALSE xor TRUE = TRUE | |
| 57 | 57 | - FALSE xor FALSE = FALSE | |
| 58 | 58 | ||
| 59 | 59 | When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses. | |
| 60 | 60 | ||
| 61 | 61 | ``` pascal | |
| 62 | 62 | (3>5) or (650<1) | |
| 63 | 63 | ``` | |
| 64 | 64 | ||
| 65 | 65 | This is because the Boolean operators are higher on the order of operations than the relational operators: | |
| 66 | 66 | ||
| 67 | 67 | 1. not | |
| 68 | 68 | 2. * / div mod and | |
| 69 | 69 | 3. \+ - or | |
| 70 | 70 | 4. \< \> \<= \>= = \<\> | |
| 71 | 71 | ||
| 72 | 72 | 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. | |
| 73 | 73 | ||
| 74 | 74 | The Boolean operators (`AND, OR, NOT, XOR`) can be used on Boolean variables just as easily as they are used on Boolean expressions. | |
| 75 | 75 | ||
| 76 | 76 | Whenever possible, don't compare two real values with the equals sign. Small round-off errors may cause two equivalent expressions to differ. | |
| 77 | 77 | ||
| 78 | 78 | --- | |
| 79 | 79 | *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).* |
