What is boolean in Pascal?

A boolean in Pascal is used to represent the built-in boolean data type. boolean can store one of two values: true or false. The size of the boolean data type is 1 byte.

Declaration

The boolean data type can be declared as:

variableName: boolean = true;

OR:

variableName: boolean = false;

Examples

Example 1

Consider the code snippet below, which shows the use of the boolean data type:

Program fixedRepetetion;
var
foo: boolean = true;
begin
foo := false;
if (foo = true) then
writeln('Boolean is true')
else if (foo = false) then
writeln('Boolean is false');
end.

Explanation

  • Line 3: A boolean type variable foo is initialized with true.
  • Line 6: foo is assigned false.
  • Lines 7-10: Checks whether the value of foo is true or false.

Example 2

Another way to initialize a boolean is to initialize it with a comparison expression. If the result of the comparison is true, boolean is initialized with true. If the result of the comparison is false, boolean is initialized with false.

Program fixedRepetetion;
var
foo: boolean = (5<10);
foo2: boolean = ('a'='b');
begin
writeln('foo: ', foo);
writeln('foo2: ', foo2);
end.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved