Delphi Programming/Boolean expression

From Wikibooks, open books for an open world
Jump to navigation Jump to search

A boolean expression returns a boolean value that can be used directly or stored in a boolean variable. An expression is created on a operator (and, or, xor, <, >...) which needs a given number of operands which can be a hard coded value, a variable or even another expression. A boolean expression can use boolean or numerical values:

program Go_to_the_playground;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  age:integer;

begin
  WriteLn ('How old are you?');
  ReadLn (age);
  Write ('Allowed to play: ');
  WriteLn ((3 < age) and (age > 12)); // Display "Allowed to play: True" or "Allowed to play: False"
end.

Logical operators[edit | edit source]

The logical operators allow to handle and compare the boolean or binary data. If you are not familiar with this concept, learn more about the boolean algebra.

Operator "not"[edit | edit source]

The NOT logical operator is written not in Delphi. So to get the opposite of a value in Delphi, we write:

var
  x, y: boolean;
begin
  x := true;
  y := not x; // y = false
end.

Operator "and"[edit | edit source]

The AND logical operator is written and in Delphi. It's like & in C. So to make a bitwise and on values in Delphi, we write:

var
  x, y, z: boolean;
begin
  x := true;
  y := false;
  z := y and x; // z = false
end.

Operator "or"[edit | edit source]

The OR logical operator is written or in Delphi. It's like | in C. So to make an or on values in Delphi, we write:

var
  x, y, z: boolean;
begin
  x := true;
  y := false;
  z := y or x; // z = true
end.

Operator "xor" (symmetric difference, or exclusive)[edit | edit source]

The XOR logical operator is written xor in Delphi. So to make an xor on values in Delphi, we write:

var
  x, y, z: boolean;
begin
  x := true;
  y := false;
  z := y xor x; // z = true
end.

Comparison operators[edit | edit source]

They are mostly used to do logical tests on numerical values.

Equality "="[edit | edit source]

The Delphi equality comparator '= allows to verify that the value on the left is strictly equal to the value on the right.

It is the same as do an inversion on the XOR logical operator (i.e. NOT XOR). Here is the truth table of the = comparator for the (x = y) operation:

= (NOT XOR) y = true y = false
x = true true false
x = false false true

Difference "<>"[edit | edit source]

The Delphi differential operator <> allows to verify that the value on the left is strictly different to the value on the right:

var
  x, y: Integer;
  isDifferent: boolean;
begin
  x := 5;
  y := 10;
  isDifferent := (x <> y); // isDifferent = true
end.

Comparator "<" (strictly less than) :[edit | edit source]

The comparator "<" (strictly less than) allows to verify that the value on the left is strictly less than to the value on the right. This operator returns true only if the numerical value is strictly less than to the numerical value on the right.

Comparator "<=" (less than) :[edit | edit source]

The comparator "<=" (less than) allows to verify that the value on the left is less than to the value on the right. This operator returns true only if the numerical value is less than to the numerical value on the right.

Comparator ">=" (greater than) :[edit | edit source]

The comparator ">=" (greater than) allows to verify that the value on the left is greater than the value on the right. This operator returns true only if the numerical value is greater than the numerical value on the right.

Comparator ">" (strictly greater than) :[edit | edit source]

The comparator ">" (strictly greater than) allows to verify that the value on the left is strictly greater than the value on the right. This operator returns true only if the numerical value is strictly greater than the numerical value on the right.

Expression combination[edit | edit source]

You can create a boolean expression on boolean expressions instead of variables:

var
  x, y, z: boolean;
  areAllEqual: boolean;
begin
  x := false;
  y := false;
  z := true;
  areAllEqual := ((x = y) and (x = z) and (y = z)); // areAllEqual = false
end.

A boolean expression on boolean expressions will use the returned values of the nested expressions. The nested expressions are in the brackets. They are evaluated first. In other word, if you change the brackets, the expressions will be evaluated differently:

var
  x, y, z: boolean;
  areAllEqual: boolean;
begin
  x := false;
  y := false;
  z := true;
  areAllEqual := ((x = y) and ((x = (z and y)) = z)); // areAllEqual = true
end.

So beware of the order.