To read the article online, visit http://www.4GuysFromRolla.com/webtech/021500-1.shtml

Rarely Used Logical Operators in VBScript


So What the Heck's a Logical Operator?
Glad you asked! A logical operator is an operator that compares the truth value of two statements (one to its right and one to its left, like: Expr1 AND Expr2), and, depending upon the truth value of these two statements, returns true or false. While that may sound confusing, once we look at an example I think things will clear up a bit!

The And operator is a logical operator. And returns a true value only when the expressions are true. For example:

4 < 5 AND 6 < 7

Returns true, since both 4 < 5 is true and 6 < 7 is true. To understand a truth statement fully, it helps to look at a truth table. A truth table contains all of the possible true/false combinations of the two expressions, and then the resulting value of the logical operator based upon the truth value of the two expressions. So, the truth table for And looks like:

If Expr1 is...And Expr2 is...Then Expr1 AND Expr2 is...
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

As the truth table shows, Expr1 AND Expr2 is only true when Expr1 and Expr2 are true.

Now that you have an understanding of what a logical operator is, and how to use a truth table to determine the behavior of the logical operator, let's look at three less often used operators: Xor, Eqv, and Imp.

Xor
Xor stands for eXclusive or. The logical operator Or is referred to as an inclusive or. The distinction is that an inclusive or is true when either Expr1 or Expr2 is true, or bothExpr1 and Expr2 are true; an exclusive or is true only when Expr1 or Expr2 is true - if they are both true, an exclusive or will return false.

The truth table for Xor is as follows:

If Expr1 is...And Expr2 is...Then Expr1 XOR Expr2 is...
TrueTrueFalse
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Eqv
The Eqv logical operator only returns true when Expr1 and Expr2 are equivelent - that is, they both have the same value. So, if Expr1 and Expr2 are both false, Eqv will return true; similarly, if Expr1 and Expr2 are both true, Eqv will return true. In all other situations, Eqv will return false.

The truth table for Eqv is as follows:

If Expr1 is...And Expr2 is...Then Expr1 EQV Expr2 is...
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseTrue

Imp
The Imp logical operator stands for Logical Implication, which is an if ... then type statement. "If Expr1 then Expr2..." The only time that this statement is false is when Expr1 is true and Expr2 is false.

The truth table for Imp is as follows:

If Expr1 is...And Expr2 is...Then Expr1 IMP Expr2 is...
TrueTrueTrue
TrueFalseFalse
FalseTrueTrue
FalseFalseTrue

Hopefully you've learned something new! You may wonder when you will find these operators of use, seeing as particular combinations of And, Or, and Not can be used to generate the truth tables that the above three logical operators represent. I find these logical operators are neat to use, and save typing/screen space! That's why I use them when the opportunity arises.

Happy Programming!


Article Information
Article Title: Rarely Used Logical Operators in VBScript
Article Author: Scott Mitchell
Published Date: Tuesday, February 15, 2000
Article URL: http://www.4GuysFromRolla.com/webtech/021500-1.shtml


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers