Logical Operators

 

Introduction

A program is a series of instructions that ask the computer to check some situations before taking action. 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. 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.

The Equality Operator =

To compare two values for equality, you 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 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 will be applied to conditional statements. Here is an example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h3>Logical Operations</h3>

<%
    Dim Number1
    Dim Number2
    
    Number1 = 42
    Number2 = 38

    Response.Write("Number 1 = " & Number1)
    Response.Write("<br>")
    Response.Write("Number 2 = " & Number2)
    Response.Write("<br>")
    Response.Write("The Comparison of Number1 = Number2 produces " & _
                   (Number1 = Number2))
%>

</body>
</html>

This would produce:

 

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 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 Not operator. In bit manipulation, Not can be used to reverse the values of bits. Here is an example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h3>Logical Operations</h3>

<%
    Dim Number
    Number = 2248

    Response.Write("    Number = " & Number)
    Response.Write("<br>")
    Response.Write("Not Number = " & (Not Number))
%>

</body>
</html>

This would produce:

In logical operations, the Not operator can be used to reverse the truthfulness of a value. When a variable holds a true value, you can reverse it to false by Notting it and vice versa. This is illustrated in the following example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h3>Logical Operations</h3>

<pre>
<%
    Dim IsInLove
    IsInLove = True

    Response.Write("    IsInLove = " & IsInLove)
    Response.Write("<br>")
    Response.Write("Not IsInLove = " & (Not IsInLove))
%>
</pre>

</body>
</html>

This would produce:

 

Inequality <>

As opposed to Equality, another operator is used to compare two values for inequality. This operation is carried 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 value. Otherwise, the comparison renders false or a null value:

Here is an example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h3>Logical Operations</h3>

<%
    Dim Number1
    Dim Number2
    
    Number1 = 42
    Number2 = 38
    
    Response.Write("Number 1 = " & Number1)
    Response.Write("<br>")
    Response.Write("Number 2 = " & Number2)
    Response.Write("<br>")
    Response.Write("The Comparison of Number1 <> Number2 produces " & _
                   (Number1 <> Number2))
%>

</body>
</html>

This would produce:

 

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

Here is an example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h3>Logical Operations</h3>

<%
    Dim Number1
    Dim Number2
    
    Number1 = 42
    Number2 = 38
    
    Response.Write("Number 1 = " & Number1)
    Response.Write("<br>")
    Response.Write("Number 2 = " & Number2)
    Response.Write("<br>")
    Response.Write("The Comparison of Number1 < Number2 produces " & _
                   (Number1 < Number2))
%>

</body>
</html>

This would produce:

 
 

Combining Equality and Lower Value <=

To know whether two values are the same or the first is less than the second, you can combine the Less Than "<" and the equality "=" operators. 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:

Less Than Or Equal To

Here is an example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h3>Logical Operations</h3>

<%
    Dim Number1
    Dim Number2
    
    Number1 = 42
    Number2 = 38
    
    Response.Write("Number 1 = " & Number1)
    Response.Write("<br>")
    Response.Write("Number 2 = " & Number2)
    Response.Write("<br>")
    Response.Write("The Comparison of Number1 <= Number2 produces " & _
                   (Number1 <= Number2))
%>

</body>
</html>

This would produce:

 

A Greater Value >

When two values of the same type are distinct, one of them may be higher than the other. To perform this comparison, you use the > operator. 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 :

Here is an example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h3>Logical Operations</h3>

<%
    Dim Number1
    Dim Number2
    
    Number1 = 42
    Number2 = 38
    
    Response.Write("Number 1 = " & Number1)
    Response.Write("<br>")
    Response.Write("Number 2 = " & Number2)
    Response.Write("<br>")
    Response.Write("The Comparison of Number1 > Number2 produces " & _
                   (Number1 > Number2))
%>

</body>
</html>

This would produce:

 

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 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 produces true also. 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 an example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h3>Logical Operations</h3>

<%
    Dim Number1
    Dim Number2
    
    Number1 = 42
    Number2 = 38
    
    Response.Write("Number 1 = " & Number1)
    Response.Write("<br>")
    Response.Write("Number 2 = " & Number2)
    Response.Write("<br>")
    Response.Write("The Comparison of Number1 >= Number2 produces " & _
                   (Number1 >= Number2))
%>

</body>
</html>

This would produce:

 

Built-in Logical Constants: True and False

An expression is said to be false if the result of its comparison is 0. Otherwise, the expression is said to bear a true result. To represent these two values, the Visual Basic language provides the True and the False constants.

 

Previous Copyright © 2005 FunctionX, Inc. Next