false and true: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 1 | + | The [[Constant|constants]] `false` and `true` are used to define the false and true conditions of a [[Boolean|`boolean`]] [[Variable|variable]]. They are [[Manifest constant|manifest constants]] that are defined as part of the [[Standard type|standard data types]] the [[Compiler|compiler]] initially knows about. | |
| 4 | 2 | ||
| 5 | - | ||
| 3 | + | These constant values must be predefined by the compiler as there is no way to define them in terms of anything else. They are defined via [`compiler/psystem.pas`](<https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_2_0/compiler/psystem.pas?view=markup#l114>) as part of the [[System unit|system unit]]. | |
| 6 | 4 | ||
| 7 | - | ||
| 5 | + | As of [[FPC]] 3.0.0 `false` and `true` are **no longer** [[Reserved words|reserved words]]. Thus the following program is valid, compiles and is “usable”: | |
| 6 | + | ``` | |
| 8 | 7 | program falseAndTrue(input, output, stderr); | |
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 8 | + | ||
| 9 | + | const | |
| 10 | + | true = 42; | |
| 11 | + | ||
| 12 | + | begin | |
| 13 | + | writeLn(true); // prints 42 | |
| 14 | + | //writeLn(true and false); // does not compile | |
| 15 | + | writeLn(system.true and false) // prints FALSE | |
| 16 | + | end. | |
| 18 | 17 | ``` | |
| 19 | 18 | ||
| 20 | 19 | ## Internal value | |
| 21 | - | ||
| 22 | - | ||
| 20 | + | ``` | |
| 23 | 21 | program falseDemo(input, output, stderr); | |
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | 22 | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 38 | - | ||
| 23 | + | uses | |
| 24 | + | typInfo; | |
| 39 | 25 | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 26 | + | begin | |
| 27 | + | writeLn(false); // prints FALSE | |
| 28 | + | ||
| 29 | + | // enumerative actions ------------------------------------------ | |
| 30 | + | writeLn(ord(false)); // prints 0 | |
| 31 | + | writeLn(succ(false)); // prints TRUE | |
| 32 | + | // next two statements generate compile errors | |
| 33 | + | // "Error: range check error while evaluating constants (-1 must be between 0 and 1)" | |
| 34 | + | writeLn(pred(false)); | |
| 35 | + | // " Error: range check error while evaluating constants (2 must be between 0 and 1)" | |
| 36 | + | writeLn(succ(succ(false))); | |
| 37 | + | ||
| 38 | + | // data type ---------------------------------------------------- | |
| 39 | + | writeLn(sizeOf(false)); // prints 1 | |
| 40 | + | writeLn(bitSizeOf(false)); // prints 8 | |
| 41 | + | writeLn(PTypeInfo(typeInfo(false))^.kind); // prints tkBool | |
| 42 | + | writeLn(PTypeInfo(typeInfo(false))^.name); // prints Boolean | |
| 43 | + | end. | |
| 46 | 44 | ``` | |
| 47 | 45 | ||
| 48 | - | ||
| 46 | + | When [[Typecast|typecasting]] or interpreting any numeric value as a boolean value, it is important to know, that _any_ non-zero value means `true` whilst only `0` (zero) is `false`. | |
| 49 | 47 | ||
| 50 | - | ||
| 48 | + | Confer [ISO 7185 § “Required simple-types”](<http://pascal-central.com/iso7185.html#6.4.2.2%20Required%20simple-types>). | |
| 51 | 49 | ||
| 52 | 50 | ## Two types of True | |
| 53 | 51 | ||
| 54 | - | ||
| 52 | + | (according to developer PascalDragon) There are two types of True in Pascal: | |
| 55 | 53 | ||
| 56 | - | ||
| 57 | - | ||
| 54 | + | * the True for **Boolean** , **Boolean8** , **Boolean16** , **Boolean32** and **Boolean64** has the value 1, anything else (except 0 for False) is undefined. | |
| 55 | + | * for **ByteBool** , **WordBool** , **LongBool** (the type for the Windows API's **BOOL**) and **QWordBool** any value that is not 0 is True, but if you assign True to such a type it will have the value "not 0" in the appropriate bit width. | |
| 58 | 56 | ||
| 57 | + | **VALUES SUMMARY** | |
| 58 | + | --- | |
| 59 | + | **·** | **Boolean** | **Boolean8** | **Boolean16** | **Boolean32** | **Boolean64** | **ByteBool** | **WordBool** | **LongBool** | **QWordBool** | |
| 60 | + | false | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 61 | + | true | 1 | 1 | 1 | 1 | 1 | 1..FF | 1..FFFF | 1..FFFFFFFF | 1..FFFFFFFFFFFFFFFF | |
| 62 | + | undefined | 2..FF | 2..FF | 2..FFFF | 2..FFFFFFFF | 2..FFFFFFFFFFFFFFFF | | | | | |
| 63 | + | ||
| 59 | 64 | ## See also | |
| 60 | 65 | ||
| 61 | - | ||
| 66 | + | * [[Boolean]] | |
| 62 | 67 | ||
| 63 | 68 | --- | |
| 64 | - | ||
| 65 | - | ||
| 69 | + | *From the Free Pascal wiki, page "false and true", as the Internet Archive kept it on 2026-02-06, last changed on 2025-08-06. 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).* |
