false and true: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: false and true
| Old | New | Line | |
|---|---|---|---|
| 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. | |
| 2 | + | ||
| 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]]. | |
| 4 | + | ||
| 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 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | program falseAndTrue(input, output, stderr); | |
| 9 | + | ||
| 10 | + | const | |
| 11 | + | true = 42; | |
| 12 | + | ||
| 13 | + | begin | |
| 14 | + | writeLn(true); // prints 42 | |
| 15 | + | //writeLn(true and false); // does not compile | |
| 16 | + | writeLn(system.true and false) // prints FALSE | |
| 17 | + | end. | |
| 18 | + | ``` | |
| 19 | + | ||
| 20 | + | ## Internal value | |
| 21 | + | ||
| 22 | + | ``` pascal | |
| 23 | + | program falseDemo(input, output, stderr); | |
| 24 | + | ||
| 25 | + | uses | |
| 26 | + | typInfo; | |
| 27 | + | ||
| 28 | + | begin | |
| 29 | + | writeLn(false); // prints FALSE | |
| 30 | + | ||
| 31 | + | // enumerative actions ------------------------------------------ | |
| 32 | + | writeLn(ord(false)); // prints 0 | |
| 33 | + | writeLn(succ(false)); // prints TRUE | |
| 34 | + | // next two statements generate compile errors | |
| 35 | + | // "Error: range check error while evaluating constants (-1 must be between 0 and 1)" | |
| 36 | + | writeLn(pred(false)); | |
| 37 | + | // " Error: range check error while evaluating constants (2 must be between 0 and 1)" | |
| 38 | + | writeLn(succ(succ(false))); | |
| 39 | + | ||
| 40 | + | // data type ---------------------------------------------------- | |
| 41 | + | writeLn(sizeOf(false)); // prints 1 | |
| 42 | + | writeLn(bitSizeOf(false)); // prints 8 | |
| 43 | + | writeLn(PTypeInfo(typeInfo(false))^.kind); // prints tkBool | |
| 44 | + | writeLn(PTypeInfo(typeInfo(false))^.name); // prints Boolean | |
| 45 | + | end. | |
| 46 | + | ``` | |
| 47 | + | ||
| 48 | + | 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 | + | ||
| 50 | + | Confer [ISO 7185 § “Required simple-types”](http://pascal-central.com/iso7185.html#6.4.2.2%20Required%20simple-types). | |
| 51 | + | ||
| 52 | + | ## Two types of True | |
| 53 | + | ||
| 54 | + | (according to developer PascalDragon) There are two types of True in Pascal: | |
| 55 | + | ||
| 56 | + | - the True for **Boolean**, **Boolean8**, **Boolean16**, **Boolean32** and **Boolean64** has the value 1, anything else (except 0 for False) is undefined. | |
| 57 | + | - 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 | + | ||
| 59 | + | ## See also | |
| 60 | + | ||
| 61 | + | - [[Boolean]] | |
| 62 | + | ||
| 63 | + | --- | |
| 64 | + | *From the Free Pascal wiki, page "false and true", as it stood on 2023-10-18 after a change by FPC user, 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).* | |
| 65 | + | Category: Code |
