Difference between revisions of "false and true"

From Lazarus wiki
Jump to navigationJump to search
(past tense statement concerning reserved words)
m (Fixed template loop)
Line 1: Line 1:
{{Translate}}
+
{{LanguageBar}}
  
 
The [[Constant|constants]] <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight> are used to define the false and true conditions of a [[Boolean|<syntaxhighlight lang="pascal" enclose="none">boolean</syntaxhighlight>]] [[Variable|variable]].
 
The [[Constant|constants]] <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight> are used to define the false and true conditions of a [[Boolean|<syntaxhighlight lang="pascal" enclose="none">boolean</syntaxhighlight>]] [[Variable|variable]].
Line 8: Line 8:
 
In [[FPC]] 3.0.0 they were <!-- documented as --> [[Reserved word|reserved words]], but they are not anymore.
 
In [[FPC]] 3.0.0 they were <!-- documented as --> [[Reserved word|reserved words]], but they are not anymore.
 
Thus the following program is valid, compiles and is “usable”:
 
Thus the following program is valid, compiles and is “usable”:
 +
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
program falseAndTrue(input, output, stderr);
 
program falseAndTrue(input, output, stderr);
Line 22: Line 23:
  
 
== internal value ==
 
== internal value ==
 +
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
program falseDemo(input, output, stderr);
 
program falseDemo(input, output, stderr);

Revision as of 07:13, 27 December 2019

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.

In FPC 3.0.0 they were reserved words, but they are not anymore. 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 out-of-range compile-time warnings
	writeLn(pred(false));                      // prints TRUE
	writeLn(succ(succ(false)));                // prints TRUE
	
	// 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.