Logical Operators

 

Introduction

A program is a series of instructions that ask the computer to check some situations and then act accordingly. To check such situations, the computer spends a great deal of its time performing comparisons between values. A comparison is a Boolean operation that produces a true or a false result, depending on the values on which the comparison is performed.

A comparison is performed between two values of the same type. For example, you can compare two numbers, two characters, or the names of two cities. On the other hand, a comparison between two disparate values doesn't bear any meaning. For example, it is difficult to compare a telephone number and somebody's age, or a music category and the distance between two points. Like the binary arithmetic operations, the comparison operations are performed on two values. Unlike arithmetic operations where results are varied, a comparison produces only one of two results. The result can be a logical true or false. 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.

The Object Pascal language is equipped with various operators used to perform any type of comparison between similar values. The values could be numeric, strings, or objects.

The Equality Operator =

To compare two variables for equality, Object Pascal uses the = operator. Its syntax is:

Value1 = Value2

The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. From our syntax, the compiler would compare the value of Value1 with that of Value2. If Value1 and Value2 hold the same value, the comparison produces a True result. If they are different, the comparison renders False or 0.

Most of the comparisons performed in Object Pascal will be applied to conditional statements; but because a comparison operation produces an integral result, the result of the comparison can be displayed on the monitor screen using the Write or Writeln procedures. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var Value: Integer;

begin
	Value := 15;

	Writeln('Comparison of Value = 32 produces ', (Value = 32));

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

This would produce:

Comparison of Value = 32 produces FALSE
Press any key to continue...

Very important
The equality operator and the assignment operator are different. When writing StudentAge := 12, this means the constant value 12 is assigned to the variable StudentAge. The variable StudentAge can change anytime and can be assigned another value. The constant 12 can never change and is always 12. For this type of operation, the variable StudentAge is always on the left side of the assignment operator. A constant, such as 12, is always on the right side and can never be on the left side of the assignment operator. This means that you can write StudentAge := 12 but never 12 := StudentAge because when writing StudentAge := 12, you are modifying the variable StudentAge from any previous value to 12. Attempting to write 12 := StudentAge means that you want to modify the constant integer 12 and give it a new value which is StudentAge: you would receive an error.NumberOfStudents1 = NumberOfStudents2 means both variables exactly mean the same thing. Whether using NumberOfStudents1 or NumberOfStudents2, the compiler considers each as meaning the other.

 

 

The Logical Not Operator

When a variable is declared and receives a value (this could be done through initialization or a change of value) in a program, it becomes alive. It 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 being used or is not available for processing (in visual programming, it would be considered as disabled) to make a variable (temporarily) unusable, you can nullify its value. Object Pascal considers that a variable whose value is nil is stern. To render a variable unavailable during the evolution of a program, apply the logical NOT operator which is not. Its syntax is:

not Value

There are two main ways you can use the logical not operator. As we will learn when studying conditional statements, the most classic way of using the logical not operator is to check the state of a variable.

To nullify a variable, you can write the not operator to its left. When used like that, you can display its value using Write or Writeln. You can even assign it to another variable. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var
	Value1, Value2, Value3: Integer;

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

	// Display the value of a variable
	Writeln('Value1 = ', Value1);
	// Logical Not a variable and display its value
	Writeln('NOT Value2 = ', not Value2);
	// Display the value of a variable that was logically "notted"
	Writeln('Value3 = ', Value3);

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

This would produce:

Value1 = 250
NOT Value2 = -33
Value3 = -251

Press any key to continue...

When a variable holds a value, it is "alive". To make it not available, you can "not" it. When a variable has been "notted", its logical value has changed. If the logical value was previously true, which is 1, it would be changed to false. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it. This is illustrated in the following example:

program Project1;

{$APPTYPE CONSOLE}

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 produce:

Value1 = 482
Value2 = -483
NOT Value2 = 482

Press any key to continue...

Inequality <>

As opposed to Equality, Object Pascal provides another operator used to compare two values for inequality. This operation is represented with the <> symbol. Its syntax is: 

Value1 <> Value2

The <> is a binary operator (like all logical operators except the logical not, which is a unary operator) that is used to compare two values. The values can come from two variables as in Variable1 <> Variable2. Upon comparing the values, if both variables hold different values, the comparison produces a true or positive value. Otherwise, the comparison renders false or a null value:

Here is an example:

program Project1;

{$APPTYPE CONSOLE}

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 produce:

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

Press any key to continue...

The inequality is obviously the opposite of the equality.

 

A Lower Value <

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

Value1 < Value2

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

Here is an example:

program Project1;

{$APPTYPE CONSOLE}

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 produce:

Value = 15
Value < 24 = TRUE

Press any key to continue...

Combining Equality and Lower Value <=

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

Value1 <= Value2

The <= operation performs a comparison as any of the previous two. If both Value1 and Value2 hold the same value, the result is true or positive. If the left operand, in this case Value1, holds a value lower than the second operand, in this case Value2, the result is still true:

Here is an example:

program Project1;

{$APPTYPE CONSOLE}

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 produce:

Value = 15
Value <= 24 = TRUE

Press any key to continue...

A Greater Value >

When two values of the same type are distinct, one of them is usually higher than the other. Object Pascal provides a logical operator that allows you to find out if one of two values is greater than the other. The operator used for this operation uses the > symbol. Its syntax is:

Value1 > Value2

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

Greater or Equal Value >=

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

Value1 >= Value2

A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of Value2 are the same, the comparison produces a true or positive value. If the value of the left operand is greater than that of the right operand,, the comparison produces true or positive also. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a false or null result

Here is a summary table of the logical operators we have studied:

Operator Meaning Example Opposite
== Equality to a == b !=
!= Not equal to 12 != 7 ==
< Less than 25 < 84 >=
<= Less than or equal to Cab <= Tab >
> Greater than 248 > 55 <=
>= Greater than or equal to Val1 >= Val2 <

 


Previous Copyright © 2004 FunctionX, Inc. Next