# Delphi Programming/Boolean expression

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:

```
1 program Go_to_the_playground;
2
3 {$APPTYPE CONSOLE}
4
5 uses
6 SysUtils;
7 var
8 age:integer;
9
10 begin
11 WriteLn ('How old are you?');
12 ReadLn (age);
13 Write ('Allowed to play: ');
14 WriteLn ((3 < age) and (age > 12)); // Display "Allowed to play: True" or "Allowed to play: False"
15 end.
```

## Logical operatorsEdit

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

The NOT logical operator is written `not`

in Delphi. So to get the opposite of a value in Delphi, we write:

```
1 var
2 x, y: boolean;
3 begin
4 x := true;
5 y := not x; // y = false
6 end.
```

### Operator "and"Edit

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:

```
1 var
2 x, y, z: boolean;
3 begin
4 x := true;
5 y := false;
6 z := y and x; // z = false
7 end.
```

### Operator "or"Edit

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:

```
1 var
2 x, y, z: boolean;
3 begin
4 x := true;
5 y := false;
6 z := y or x; // z = true
7 end.
```

### Operator "xor" (symmetric difference, or exclusive)Edit

The XOR logical operator is written `xor`

in Delphi. So to make an xor on values in Delphi, we write:

```
1 var
2 x, y, z: boolean;
3 begin
4 x := true;
5 y := false;
6 z := y xor x; // z = true
7 end.
```

## Comparison operatorsEdit

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

### Equality "="Edit

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

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

```
1 var
2 x, y: Integer;
3 isDifferent: boolean;
4 begin
5 x := 5;
6 y := 10;
7 isDifferent := (x <> y); // isDifferent = true
8 end.
```

### Comparator "<" (strictly less than) :Edit

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

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

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

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 combinationEdit

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

```
1 var
2 x, y, z: boolean;
3 areAllEqual: boolean;
4 begin
5 x := false;
6 y := false;
7 z := true;
8 areAllEqual := ((x = y) and (x = z) and (y = z)); // areAllEqual = false
9 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:

```
1 var
2 x, y, z: boolean;
3 areAllEqual: boolean;
4 begin
5 x := false;
6 y := false;
7 z := true;
8 areAllEqual := ((x = y) and ((x = (z and y)) = z)); // areAllEqual = true
9 end.
```

So beware of the order.