Last Updated:

Operators in Pascal

Operators in Pascal

A program is a series of instructions that ask a computer to check some situations and then act accordingly. To verify such situations, the computer spends a lot of time comparing the values. A comparison is a logical operation that results in a true or false result depending on the values on which the comparison is performed.

The comparison is made between two values of the same type. For example, you can compare two numbers, two symbols, or the names of two cities. On the other hand, comparing two disparate values makes no sense. For example, it's hard to compare a phone number and someone's age, or a music category and the distance between two dots. Like binary arithmetic operations, comparison operations are performed on two values of the same format.

Unlike arithmetic operations, where the results are different, comparison yields only one of two results. The result can be logical truth or falsehood. When a comparison is true, it has an integral value of 1 or positive; that is, a value greater than 0. If the comparison is not true, it is considered false and carries an integral value of 0.

ObjectPascal is equipped with various operators used to perform any type of comparison between similar values. Values can be numeric, string, or objects.

Equality operator

To compare two variables for equality, ObjectPascal uses the = operator. Its syntax is as follows:

Value1 = Value2

An equality operation is used to determine whether two variables (or one variable and a constant) have the same value. Based on our syntax, the compiler will compare the value of Value1 with the value of Value2. If Value1 and Value2 have the same value, the comparison produces a true result. If they are different, the comparison gives False or 0.

Most comparisons made in ObjectPascal will apply to conditional statements; however, since the comparison operation gives an integral result, the comparison result can be displayed on the monitor screen using the Write or Writeln procedures. Here's an example:

var:integer;

begin
Value := 15;

Writeln('Compare values = 32 produces ', (Value = 32));

Write ("Press any key to continue...");
Readln;
end.

This would lead to:

Value comparison = 32 gives FALSE
Press any key to continue...

Very important

The equality operator and the assignment operator are different. When you write StudentAge := 12, this means that a constant value of 12 is assigned to the StudentAge variable. The StudentAge variable can change at any time and can be assigned a different value. The constant 12 can never change and is always equal to 12.

For this type of operation, the StudentAge variable is always on the left side of the assignment statement. A constant, such as 12, is always to the right and can never be to the left of the assignment operator. This means that you can write StudentAge := 12, but never 12 := StudentAge, because when you write StudentAge := 12, you change the StudentAge variable from any previous value to 12.

Trying to write 12 := StudentAge means that you want to change the constant integer 12 and give it a new value, which is StudentAge: you will get an error. NumberOfStudents1 = NumberOfStudents2 means that both variables exactly mean the same thing. Regardless of whether NumberOfStudents1 or NumberOfStudents2 is used, the compiler treats each as meaning the other.

 

Logical Not operator

 

When a variable is declared and gets a value (this can be done by initializing or changing the value) in the program, it becomes alive. He can then participate in any necessary operation.

The compiler keeps track of every variable that exists in the program being processed. When a variable is not used or is not available for processing (it will be considered disabled in visual programming), you can reset its value to make the variable (temporarily) unusable.

Pascal believes that a variable whose value is zero is strict. To make a variable inaccessible during program evolution, use a logical NOT operator that is not a logical statement. Its syntax is as follows:

notValue

There are two main ways to use the logical not operator. As we learn when we study conditional operators, the most classic way to use the logical not operator is to check the state of a variable.

To reset a variable, you can write an not statement to the left of it. With this use, you can display its value using Write or Writeln. You can even assign it to another variable. Here's an example:

var
Value1, Value2, Value3: Integer;

begin
Value1 := 250;
Value2 := 32;
Value3 := not Value1;

Displaying the value of the
variable Writeln('Value1 = ', Value1);
// There is no logical operator and displays its value
Writeln('NOT Value2 = ', not Value2);
// Displays the value of a variable that has been logically "tagged"
by Writeln('Value3 = ', Value3);

Write(Chr(10), Press any key to continue’);
Readln;
end.

Exit:

Value 1 = 250
not Value2 = -33
Value 3 = -251
Press any key to continue...

When a variable contains a value, it is alive. To make it inaccessible, you can "not" it. When the variable was "recorded," its Boolean value changed. If the Boolean value had previously been true, that is, equal to 1, it would have been changed to false. So you can invert the Boolean value of a variable by "writing" or not "writing" it. This is illustrated in the following example:

var
Value1, Value2: Integer;

begin
Value1 := 482;
Value2 := not Value1;

Writeln(‘ Value1 = ‘, Value1);
Writeln(‘ Value2 = ‘, Value2);
Writeln(‘NOT Value2 = ‘, not Value2);

Write(Chr(10), 'Press any key to continue...');
Readln;
end.

This would lead to:

Value1 = 482
Value2 = -483
NOT Value2 = 482
Press any key to continue...

Inequality <>

Unlike Equality, ObjectPascal provides another operator used to compare two inequality values. This operation is represented by the <> symbol. Its syntax is as follows:

Value1 <> Value2

<> is a binary operator (like all logical operators except the Boolean not, which is an unary operator) that is used to compare two values. Values can come from two variables, as in Variable1 <> Variable2. When comparing values, if both variables have different values, the comparison gives a true or positive value. Otherwise, the comparison returns false or null.

Here's an example:

var
Value1, Value2:Integer;

Begin
Value1 := 212;
Value2 := -46;

Writeln(‘Value1 = ‘, Value1);
Writeln(‘Value2 = ‘, Value2);
Writeln(‘Value1 <> Value2 = ‘, Value1 <> Value2);

Write(Chr(10), 'Press any key to continue...');
Readln;
end.

This would lead to:

Value1 = 212
Value2 = -46
Value1 <> Value2 = TRUE

Press any key to continue...

Inequality is obviously the opposite of equality.

Lower < value

 

To find out if one value is lower than another, use the < operator. Its syntax is as follows:

Value1 < Value2

The value held by Value1 is compared to Value2. As would be done with other operations, a comparison can be made between two variables, as in Variable1 < Variable2. if the value of variable1 is lower than the value of variable2 the comparison gives a true or positive result

Here's an example:

varValue:Integer;

begin
Value:= 15;

Writeln(‘Value = ‘, Value);
Writeln(‘Value < 24 = ‘, Value < 24);

Write(Chr(10), 'Press any key to continue...');
Readln;
end.

This would lead to:

Value= 15
Value< 24 = TRUE

Press any key to continue...

 

Combination of Equality and Lesser value <=

 

The previous two operations can be combined to compare two values. This lets you know if two values are the same or if the first is less than the second. The <= operator used and its syntax are:

Value1 <= Value2

The <= operation performs a comparison like either of the previous two. If both Value1 and Value2 have the same value, the result will be true or positive. If the left operand, in this case Value1, has a value lower than the second operand, in this case Value2, the result is still true:

Here's an example:

Var value: Integer;

begin
Value:= 15;

Writeln(‘Value = ‘, Value);
Writeln(‘Value <= 24 = ‘, Value <= 24);

Write(Chr(10), 'Press any key to continue...');
Readln;
end.

This would lead to:

Value= 15
Value<= 24 = TRUE

Press any key to continue...

Greater > value

 

When two values of the same type are different, one is usually higher than the other. ObjectPascal provides a Boolean operator that lets you know whether one of the two values of the other is greater. The operator used for this operation uses a character >. Its syntax is as follows:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be variables, or the left operand can be variable, while the right operand is a constant. If the value to the left of the operator > greater than the value to the right or constant, the comparison gives a true or positive value. Otherwise, the comparison becomes false or null.

Greater than or equal to >=

The operators greater and equality can be combined to obtain the operator as follows: >=. This is a "greater than or equal" operator. Its syntax is as follows:

Value1 >= Value2

Conclusion

Today you have seen all the operators of the Pascal language. Hopefully this will help you in further learning programming!