Home

Logical Operators

 

Introduction

One of the goals of computer programming is telling the computer what to do when something occurs, and how to do it. This is performed by setting conditions, examining them and stating what decisions the computer should make. To perform the necessary conditions, you have two main options: Microsoft Access or Microsoft Visual Basic. Microsoft Access is equipped with a series of operators and functions destined to perform various operations. To use a condition in Microsoft Access, if you know the structure of the conditional statement, you can write it after typing an assignment operator. Because most conditions in Microsoft Access are in the form of functions, we will study them later on. For now, we will learn how to write conditions in the Microsoft Visual Basic language.

Microsoft Visual Basic comes with a lot of conditional statements for almost any situation your computer can encounter. As the application developer, it is up to you to anticipate these situations and make your program act accordingly.

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 grand-mother name, or a music category and the distance between two points. Like the 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.

To perform the necessary comparisons, Microsoft Access and Visual Basic use a series of logical operators and constants.

The Comparison for Equality =

To compare two variables for equality, use the = operator. Its formula 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 formula, the value of Value1 would be compared with the value 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. After performing the comparison and finding out its result, you can carry an assignment. This can be illustrated as follows:

 

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. 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. 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.

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 True, it would be changed to False. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it.

For Inequality <>

As opposed to equality, Microsoft Visual Basic provides an operator used to compare two values for inequality, which is <>. 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 value. Otherwise, the comparison renders False or a null value. This can be illustrated as follows:

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 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. This can be illustrated with the following:

 

Combining Equality and Lower Value <=

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. 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. This can be illustrated as follows:

The result of this comparison is false only if the value of the left operand is strictly higher than that of the right operand.

A Greater Value >

When two values of the same type are distinct, one of them is usually higher than the other. Microsoft 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 is 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 value. Otherwise, the comparison renders False or null. This would illustrated as follows:

The > and the <= are opposite operators.

Greater or Equal Value >=

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 value. If the value of the left operand is greater than that of the right operand, the comparison still produces True. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a False result:

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

Operator Meaning Example Opposite
= Equality to a = b Not
Not 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 © 2005-2010 FunctionX, Inc. Next