Page 123 - Open Soource Technologies 304.indd
P. 123
Unit 5: VB String and Operators
Dim x As Boolean
x = Not 23 > 12 ‘ x equals False
.x = Not 23 > 67 ‘ x equals True.
The And operator performs logical conjunction on two Boolean expressions. That is, if both
expressions evaluate to True, then the And operator returns True. If either or both expressions
evaluate to False, then And returns False.
The Or operator performs logical disjunction on two Boolean expressions. If either expression
evaluates to True, Or returns True. If neither expression evaluates to True, Or returns False.
Xor performs logical exclusion on two expressions. If either expression evaluates to True, but not
both, Xor returns True. If both expressions evaluate to True or both expressions evaluate to False,
Xor returns False.
Examples of the And, Or, and Xor operators are shown below:
Dim x As Boolean
x = 23 > 12 And 12 >4 ‘ x = True
x = 12 > 23 And 12 > 4 ‘ x = False
x = 23 > 12 Or 4 > 12` ‘ x = True
x = 23 > 45 Or 4 > 12 ‘ x = False
x = 23 > 45 Xor 12 > 4 ‘ x = True
x = 23 > 12 Xor 12 > 4 ‘ x = False
x = 12 > 23 Xor 4 > 12 ‘ x = False
In addition to being logical operators, Not, Or, And, and Xor also perform
bitwise arithmetic when used on numeric values. Information on this
functionality can be found at Arithmetic Operators.
The AndAlso operator is very similar to the And operator, in that it also performs logical
conjunction on two Boolean expressions. The key difference between AndAlso and And is that
AndAlso exhibits short-circuiting behavior. If the first expression in an AndAlso expression
evaluates to False, then the second expression is not evaluated and False is returned for the
AndAlso expression.
Similarly, the OrElse operator performs short-circuiting logical disjunction on two Boolean
expressions. If the first expression in an OrElse expression evaluates to True, then the second
expression is not evaluated and True is returned for the OrElse expression. Below are some
examples that illustrate the difference between And, Or, and their counterparts:
12 > 45 And MyFunction(4) ‘ MyFunction() is called.
12 > 45 AndAlso MyFunction(4) ‘ MyFunction() is not called.
45 > 12 Or MyFunction(4) ‘ MyFunction is called.
45 > 12 OrElse MyFunction(4) ‘ MyFunction is not called
LOVELY PROFESSIONAL UNIVERSITY 117