Difference between revisions of "Boolean"

From Lazarus wiki
Jump to navigationJump to search
(WriteStr mention)
Line 2: Line 2:
  
 
== Overview ==
 
== Overview ==
'''Boolean''' is a logical datatype. Data of type boolean has one of only two values, either [[True|true]] or [[False|false]]. A Boolean variable is 1 byte in size.
+
'''Boolean''' is a logical [[Data_type|data type]]. Data of type boolean has one of only two values, either [[True|<code>true</code>]] or [[False|<code>false</code>]]. A boolean [[Variable|variable]] is 1 byte in size.
  
The '''true''' value can be assigned directly to a boolean variable or from the result of a comparison or test that was successful ("true"). Similarly the '''false''' value can be assigned directly or from the result of a comparison or test that was not successful ("false"). The Write() and Writeln() procedures will print a string that corresponds to the value of a Boolean variable (either "TRUE" or "FALSE"). A Boolean variable can be used as the expression in an if statement. The WriteStr() procedure can be used to store a string literal representing a Boolean variable's value in a string variable.
+
The <code>true</code> value can be assigned directly to a boolean variable or from the result of a comparison or test that was successful (<code>true</code>). Similarly the <code>false</code> value can be assigned directly or from the result of a comparison or test that was not successful (<code>false</code>).  
 +
The [[Write]]() and Writeln() [[Procedure|procedures]] will print a [[String|string]] that corresponds to the value of a boolean variable (either "TRUE" or "FALSE"). A Boolean variable can be used as the expression in an [[If|if statement]]. The WriteStr() procedure can be used to store a string literal representing a boolean variable's value in a string variable.
  
 
<syntaxhighlight>
 
<syntaxhighlight>
 +
program booleantest;
 
var
 
var
 
     tooLarge  : Boolean = false;
 
     tooLarge  : Boolean = false;
     boolString : ShortString;  
+
     boolString : ShortString;
 
begin
 
begin
    Writeln(tooLarge);
+
  Writeln(tooLarge);
    tooLarge := (0 = 0);
+
  tooLarge := (0 = 0);
    Writeln(tooLarge);
+
  Writeln(tooLarge);
    tooLarge := (3 > 5);
+
  tooLarge := (3 > 5);
    Writeln(tooLarge);
+
  Writeln(tooLarge);
    tooLarge := true;
+
  tooLarge := true;
    Writeln(tooLarge);
+
  Writeln(tooLarge);
    if tooLarge then
+
  if tooLarge then
      Writeln('tooLarge is true')
+
    Writeln('tooLarge is true')
    else
+
  else
      Writeln('tooLarge is false');
+
    Writeln('tooLarge is false');
    WriteStr(boolString,tooLarge);
+
  WriteStr(boolString,tooLarge);
    Writeln(boolString);
+
  Writeln(boolString);
end
+
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
Ouputs:<br/>
 
Ouputs:<br/>
'''FALSE'''<br/>
+
'''FALSE'''
'''TRUE'''<br/>
+
'''TRUE'''
'''FALSE'''<br/>
+
'''FALSE'''
'''TRUE'''<br/>
+
'''TRUE'''
'''tooLarge is true'''<br/>
+
'''tooLarge is true'''
'''TRUE'''<br/>
+
'''TRUE'''
  
 
== See also ==
 
== See also ==

Revision as of 07:06, 16 January 2019

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

Overview

Boolean is a logical data type. Data of type boolean has one of only two values, either true or false. A boolean variable is 1 byte in size.

The true value can be assigned directly to a boolean variable or from the result of a comparison or test that was successful (true). Similarly the false value can be assigned directly or from the result of a comparison or test that was not successful (false). The Write() and Writeln() procedures will print a string that corresponds to the value of a boolean variable (either "TRUE" or "FALSE"). A Boolean variable can be used as the expression in an if statement. The WriteStr() procedure can be used to store a string literal representing a boolean variable's value in a string variable.

program booleantest;
var
    tooLarge   : Boolean = false;
    boolString : ShortString;
begin
  Writeln(tooLarge);
  tooLarge := (0 = 0);
  Writeln(tooLarge);
  tooLarge := (3 > 5);
  Writeln(tooLarge);
  tooLarge := true;
  Writeln(tooLarge);
  if tooLarge then
    Writeln('tooLarge is true')
  else
    Writeln('tooLarge is false');
  WriteStr(boolString,tooLarge);
  Writeln(boolString);
end.

Ouputs:

FALSE
TRUE
FALSE
TRUE
tooLarge is true
TRUE

See also


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring