Logical Comparisons |
|
The result of a comparison can also be assigned to a Boolean variable. Here is an example: Private Sub Command1_Click() Dim value1 As Integer Dim value2 As Boolean value1 = 15 value2 = (value1 = 32) Text1.Text = "Comparison of Value = 32 produces " & value2 End Sub |
|
Logical Not |
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. To render a variable unavailable during the evolution of a program, apply the logical not operator which is Not. Its formula 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 Not to its left. When used like that, you can display its value. You can even assign it to another variable. Here is an example: Private Sub Command1_Click() Dim value1 As Integer Dim value2 As Boolean value1 = 250 value2 = Not value1 Text1.Text = value2 End Sub |
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. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it. This is done by typing Not to its left. |
Inequality <> |
Visual Basic provides an operator used to compare two values for inequality. Its formula is: Value1 <> Value2 <> 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: |
Private Sub Command1_Click() Dim value1 As Integer Dim value2 As Integer Dim value3 As Boolean value1 = 212 value2 = -46 value3 = (value1 <> value2) Text1.Text = value1 & " <> " & value2 & " = " & value3 End Sub |
The inequality is obviously the opposite of the equality. |
Less Than < |
To find out whether one value is lower than another, use the < operator. Its formula 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: |
Private Sub Command1_Click() Dim value1 As Integer Dim value2 As Integer Dim value3 As Boolean value1 = 212 value2 = -46 value3 = (value1 < value2) Text1.Text = value1 & " < " & value2 & " = " & value3 End Sub |
Less Than Or Equal <= |
The previous two operations can be combined to compare two values. This allows you to know if two values are the same or if the first is less than the second. The operator used is <= and its formula is: Value1 <= Value2 The <= operation performs a comparison as any of the last two. If both Value1 and Value2 hold the same value, the result is true or not null. 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: |
Private Sub Command1_Click() Dim value1 As Integer Dim value2 As Integer Dim value3 As Boolean value1 = 212 value2 = -46 value3 = (value1 <= value2) Text1.Text = value1 & " <= " & value2 & " = " & value3 End Sub |
Greater Than > |
When two values of the same type are distinct, one of them is usually higher than the other. Visual Basic 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 formula 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. This can be illustrated as follows:
Here is an example: |
Private Sub Command1_Click() Dim value1 As Integer Dim value2 As Integer Dim value3 As Boolean value1 = 212 value2 = -46 value3 = (value1 > value2) Text1.Text = value1 & " > " & value2 & " = " & value3 End Sub |
Greater Than or Equal >= |
The greater than or the equality operators can be combined to produce an operator as follows: >=. This is the "greater than or equal to" operator. Its formula 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. This can be illustrated as follows:
Here is an example: |
Private Sub Command1_Click() Dim value1 As Integer Dim value2 As Integer Dim value3 As Boolean value1 = 212 value2 = -46 value3 = (value1 >= value2) Text1.Text = value1 & " >= " & value2 & " produces " & value3 End Sub |
Here is a summary table of the logical operators we have studied:
|
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|