Conditional Statements in VBScript - Conditional Expressions
(Page 4 of 4 )
When discussing conditional statements, it's important to discuss conditional expressions. These are the expressions that allow conditional statements to function.
Conditional expressions always return a Boolean value of either True of False depending upon whether the expression can be successfully evaluated.
The most common type of conditional expression is an equality test. This tests to see if the value of one variable is equal to the value of another. Equality is determined based upon the variable type being used. For example, two strings must contain the same characters or two integers must contain the same values.
You can use the NOT keyword to indicate the converse of an expression. In other words, NOT returns the opposite of whatever the expression evaluates to.
Each of the elements in an expression may be either a variable or a literal. In other words, any of the following are perfectly acceptable.
strColor = "red" returns True if strColor contains "red"
strColor1 = strColor2 returns either True or False
"red" = "blue" returns False
NOT "blue" = "blue" returns False
While it's completely obvious that some of these will return False, they are nonetheless acceptable.
You can also make decisions based upon more than one conditional expression using logical operators. The logical operators are AND and OR.
When using logical operators, a single value is returned based upon the conditions provided. When using AND, True is returned only if both supplied conditions evaluate to True, and False otherwise. When using OR, True is returned if either (or both) of the conditions returns True.
"blue" = "blue" AND "red" = "red returns True
"blue" = "blue" AND "blue" = "red" returns False
NOT "blue" = "red" AND NOT 3 = 4 returns True
"blue" = "blue" OR "red"="red" returns True
"blue" = "blue" OR "blue" = "red" returns True
5 = 4 OR 3 = 2 returns False
In the above examples, you can see how the logical operators can be used to combine expressions. They are evaluated as follows:
True AND True returns True
True AND False returns False
False AND False returns False
True OR True returns True
True OR False returns True
False OR False returns False
Multiple logical operators are evaluated from left to right. This can produce some unwanted results. As in algebra, you can use a pair of parenthesis to control the order of operations. Take the following for example.
Condition1 AND Condition2 OR Condition3
This expression will evaluate to True if Condition1 and Condition2 are both true OR if only Condition3 is true. (It also evaluates to true if all three conditions are true).
That 's fine, if that's what you wanted. But what if you were trying to evaluate whether Condition1 existed with either of the other two?
Condition1 AND (Condition2 OR Condition3)
By using parenthesis, we are now forcing the OR operator to evaluate before the AND. This achieves our goal and will only return true if Condition1 is true along with either or both of the other conditions.
This seems a little confusing at first, but you will begin to get the hang of things much more quickly once you start putting these into real-world use.
For the sake of brevity, I haven't gone into much detail surrounding conditional expressions. Be aware that they may use any of the available operators such as < (less than), > (greater than), or even bitwise operators such as XAND and XOR.
These are far beyond the scope of this article, so stay tuned for a future release dedicated strictly to VBScript operators. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |