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:
|
|
No comments:
Post a Comment