Logical Operators |
|
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.
Comparison of Value = 32 produces FALSE Press any key to continue...
|
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: |
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: |
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: |
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:
Here is a summary table of the logical operators we have studied: |
|
|
||
Previous | Copyright © 2004 FunctionX, Inc. | Next |
|