When you think ASP, think...
 Recent Articles All Articles ASP.NET Articles ASPFAQs.com Message Board Related Web Technologies User Tips! Coding Tips

 Sections: Sample Chapters Commonly Asked Message Board Questions JavaScript Tutorials MSDN Communities Hub Official Docs Security Stump the SQL Guru! XML Info Information: Feedback Author an Article
Published: Tuesday, February 15, 2000

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!

- continued -

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!

 ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article

 #developerBrand-footer{text-align:center;margin:0 auto} #footerlinks{font-size:12px;word-spacing:1px} #footerlinks a{color:#000;margin:5px 0 0} #itbeBrand-footer{text-align:center;margin:0 auto} #itbusinessedge-footer{text-align:center;margin:0 auto} #footercopyright{font-size:12px;margin:5px 0 0} #developerBrandForum-footer{text-align:center;margin:0 auto} #itbeBrandForum-footer{text-align:center;margin:0 auto} #eweek-truste-footer{text-align:center;margin:0 auto} #developerBrand-truste-footer{text-align:center;margin:0 auto} #itbeBrand-truste-footer{text-align:center;margin:0 auto} #weboitbeBrand-truste-footer{text-align:center;margin:0 auto} #itbusinessedge-truste-footer{text-align:center;margin:0 auto} .footerLogoClass{text-align:center;margin:0 auto} #sitePrivacyModal{display: none;} #sitePrivacyModal .privacy-modal {display: none;position: fixed;z-index: 10000;left: 0;top: 0;overflow: auto;} #sitePrivacyModal .privacy-modal-content {background-color: #efefef;position: fixed;bottom: 0;width: 30%;-webkit-animation-name: slideIn;-webkit-animation-duration: 0.4s;animation-name: slideIn;animation-duration: 0.4s;font-family: inherit;font-size: 15px;border: 1px solid darkgray;border-radius: 10px;border: 1px solid #c5c5c5;padding: 5px;z-index: 10000;}#sitePrivacyModal .close {color: #000;float: right;font-size: 25px;font-weight: bold;margin-top: -5px;width: 15px;}#sitePrivacyModal .close:hover,#sitePrivacyModal .close:focus {text-decoration: none;cursor: pointer;} #sitePrivacyModal .privacy-modal-header {color: white;}#sitePrivacyModal .privacy-modal-body {padding: 2px 16px;} #sitePrivacyModal .modal-footer {padding: 2px 16px;color: white;}@-webkit-keyframes slideIn {from {bottom: -300px; opacity: 0} to {bottom: 0; opacity: 1}}@keyframes slideIn {from {bottom: -300px; opacity: 0} to {bottom: 0; opacity: 1}} @-webkit-keyframes fadeIn {from {opacity: 0} to {opacity: 1}}@keyframes fadeIn {from {opacity: 0} to {opacity: 1}} @media (max-width: 767px){.privacy-modal-content {position: fixed;bottom: 0;width: 50% !important;}} ×We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.