Difference between revisions of "false and true"
m (grammar) |
(→Internal value: update to current 3.2.2 compiler behavior) |
||
(11 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{false and true}} |
− | The [[Constant|constants]] <syntaxhighlight lang="pascal" | + | The [[Constant|constants]] <syntaxhighlight lang="pascal" inline>false</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>true</syntaxhighlight> are used to define the false and true conditions of a [[Boolean|<syntaxhighlight lang="pascal" inline>boolean</syntaxhighlight>]] [[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. | They are [[Manifest constant|manifest constants]] that are defined as part of the [[Standard type|standard data types]] the [[Compiler|compiler]] initially knows about. | ||
These constant values must be predefined by the compiler as there is no way to define them in terms of anything else. | 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 [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_2_0/compiler/psystem.pas?view=markup#l114 <tt>compiler/psystem.pas</tt>] as part of the [[System unit|system unit]]. | ||
+ | |||
+ | As of [[FPC]] 3.0.0 <syntaxhighlight lang="pascal" inline>false</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>true</syntaxhighlight> are '''no longer''' [[Reserved words|reserved words]]. | ||
+ | Thus the following program is valid, compiles and is “usable”: | ||
+ | |||
+ | <syntaxhighlight lang="pascal"> | ||
+ | program falseAndTrue(input, output, stderr); | ||
+ | |||
+ | const | ||
+ | true = 42; | ||
+ | |||
+ | begin | ||
+ | writeLn(true); // prints 42 | ||
+ | //writeLn(true and false); // does not compile | ||
+ | writeLn(system.true and false) // prints FALSE | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Internal value == | ||
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
Line 18: | Line 37: | ||
writeLn(ord(false)); // prints 0 | writeLn(ord(false)); // prints 0 | ||
writeLn(succ(false)); // prints TRUE | writeLn(succ(false)); // prints TRUE | ||
− | // next two statements generate | + | // next two statements generate compile errors |
− | writeLn(pred(false)); | + | // "Error: range check error while evaluating constants (-1 must be between 0 and 1)" |
− | writeLn(succ(succ(false))); | + | writeLn(pred(false)); |
+ | // " Error: range check error while evaluating constants (2 must be between 0 and 1)" | ||
+ | writeLn(succ(succ(false))); | ||
// data type ---------------------------------------------------- | // data type ---------------------------------------------------- | ||
Line 29: | Line 50: | ||
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | When [[Typecast|typecasting]] or interpreting any numeric value as a boolean value, it is important to know, that ''any'' non-zero value means <syntaxhighlight lang="pascal" inline>true</syntaxhighlight> whilst only <syntaxhighlight lang="pascal" inline>0</syntaxhighlight> (zero) is <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>. | ||
+ | |||
+ | Confer [http://pascal-central.com/iso7185.html#6.4.2.2%20Required%20simple-types ISO 7185 § “Required simple-types”]. | ||
+ | |||
+ | ==Two types of True== | ||
+ | |||
+ | (according to developer PascalDragon) | ||
+ | There are two types of True in Pascal: | ||
+ | |||
+ | * the True for '''Boolean''', '''Boolean8''', '''Boolean16''', '''Boolean32''' and '''Boolean64''' has the value 1, anything else (except 0 for False) is undefined. | ||
+ | * 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. | ||
+ | |||
+ | == See also == | ||
+ | |||
+ | * [[Boolean]] | ||
+ | |||
+ | [[Category: Code]] |
Latest revision as of 01:33, 18 October 2023
│
English (en) │
The constants false
and true
are used to define the false and true conditions of a boolean
variable.
They are manifest constants that are defined as part of the standard data types the compiler initially knows about.
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 as part of the system unit.
As of FPC 3.0.0 false
and true
are no longer reserved words.
Thus the following program is valid, compiles and is “usable”:
program falseAndTrue(input, output, stderr);
const
true = 42;
begin
writeLn(true); // prints 42
//writeLn(true and false); // does not compile
writeLn(system.true and false) // prints FALSE
end.
Internal value
program falseDemo(input, output, stderr);
uses
typInfo;
begin
writeLn(false); // prints FALSE
// enumerative actions ------------------------------------------
writeLn(ord(false)); // prints 0
writeLn(succ(false)); // prints TRUE
// next two statements generate compile errors
// "Error: range check error while evaluating constants (-1 must be between 0 and 1)"
writeLn(pred(false));
// " Error: range check error while evaluating constants (2 must be between 0 and 1)"
writeLn(succ(succ(false)));
// data type ----------------------------------------------------
writeLn(sizeOf(false)); // prints 1
writeLn(bitSizeOf(false)); // prints 8
writeLn(PTypeInfo(typeInfo(false))^.kind); // prints tkBool
writeLn(PTypeInfo(typeInfo(false))^.name); // prints Boolean
end.
When 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
.
Confer ISO 7185 § “Required simple-types”.
Two types of True
(according to developer PascalDragon) There are two types of True in Pascal:
- the True for Boolean, Boolean8, Boolean16, Boolean32 and Boolean64 has the value 1, anything else (except 0 for False) is undefined.
- 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.