When you put a programming algorithm together, you need something called a boolean expression. A boolean expression can take the form of a logical operation or a comparison. You will get plenty of use out of it when we write conditions and loops in the posts that follow.
This time we'll try learning about logical and comparison operations in Pascal. Before that, though, you need to know about the boolean data type first.
The Boolean Data Type in Pascal
Logical operations are tied closely to logical conditions. A logical condition has its own data type, namely Boolean. The boolean data type can only hold 2 values, true and false.
Operations on the boolean data type are called boolean expressions, and they come in 2 kinds: comparison operations and logical operations.
So what exactly are comparison and logical operations? First, type in the following code.
program OperasiLogika;
uses crt;
var
a, b: boolean;
begin
clrscr;
a := true;
b := false;
writeln('a: ', a);
writeln('b: ', b);
readln;
end.
If you run it, the output will look like this.
a: true
b: false
It's clear that a is true and b is false. That's roughly how you write a boolean variable. Don't delete the code yet, we'll modify it again in a moment. For now, let's move on to comparison operations in Pascal.
Comparison Operations in Pascal
A comparison operation compares something on the left side with something on the right side and returns its logical condition. You have known comparison operations since you were a kid.
There are 6 comparison operators: less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to. For the symbols and what they mean, see the table below.
| Operation | Operator | Description |
|---|---|---|
| Less than | < |
true if the left side is less than the right side |
| Greater than | > |
true if the left side is greater than the right side |
| Less than or equal to | >= |
like less than, but returns true if left and right are the same |
| Greater than or equal to | <= |
like greater than, but returns true if left and right are the same |
| Equal to | = |
true if the left and right sides are the same |
| Not equal to | <> |
true if the left and right sides are not the same |
A comparison operator works the same way as an arithmetic operator. You can use it when you assign a value to a variable. The only difference is that the value it gives back comes as true or false.
To see more, let's modify the code we wrote earlier a little. Try replacing the contents of the variables a and b.
program OperasiLogika;
uses crt;
var
a, b: boolean;
begin
clrscr;
a := 9 > 10; // false because 9 is not greater than 10
b := 8 = 8; // true because 8 equals 8
writeln('a: ', a);
writeln('b: ', b);
readln;
end.
If you run it, the output will look like this.
a: false
b: true
Those two variables are filled using comparison conditions. Variable a is false because 9 > 10 is wrong, while variable b is true because 8 = 8 is right.
As an experiment, try swapping the numbers and the comparison operators around.
Logical Operations in Pascal
A logical operation takes 2 logical conditions (true or false) and draws a conclusion from them.
There are 4 operators in a logical operation, namely AND, OR, XOR, and NOT. You may have met them in logic mathematics as conjunction, disjunction, implication, and negation.
For a clearer picture of the operators in Pascal, see the table below.
| Operator | Equivalent | Description |
|---|---|---|
AND |
conjunction | True if the left and right sides are both true |
OR |
disjunction | False if both sides are false |
XOR |
negation of biconditional | False if both sides are the same |
NOT |
negation | Flips the value (true becomes false) |
You write a logical operation the same way as a comparison, only both sides already have to be boolean. To see it, modify the code again. Add 2 more boolean variables named c and d, then fill them in as in the example below.
program OperasiLogika;
uses crt;
var
a, b: boolean;
c, d: boolean; // add a new variable
begin
clrscr;
a := 9 > 10; // false
b := 8 = 8; // true
c := a AND b; // true AND false = false
d := a OR b; // true OR false = true
writeln('a: ', a);
writeln('b: ', b);
writeln('c: ', c);
writeln('d: ', d);
readln;
end.
Once that's done, run it, and the output will look roughly like this.
a: false
b: true
c: false
d: true
In that example variable a has the value true, and variable b has the value false. We then use those values to run logical operations on variables c and d.
For variable c we perform an AND operation. So a AND b translates to true AND false, and the result is false.
For variable d we perform an OR operation. So a OR b translates to true OR false, and the result is true.
The
notoperation only needs one operand to the right of the sign (it works and is written like a minus sign)
Wrapping Up
That's all for this tutorial, hope it helps :D You'll use boolean expressions a lot in the next tutorials, when we build conditions and loops. Next up we'll learn about branching conditions with if else.