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