The And Also Operator

The AndAlso operator is almost identical to the And operator in comparing two Boolean expressions. As Table 6-5 shows, the only difference is that if the first expression is False, the second expression is not evaluated. The following Ask the Exper" discusses the consequences of the second expression not being evaluated and why you might use the AndAlso operator instead of the And operator.

Table 6-5: The AndAlso Operator

If First Expression Is

And Second Expression Is

Result Is

True True

True False

True False

False

(not evaluated)

False

Note The AndAlso operator, unlike the And operator, only performs a logical comparison of two Boolean expressions, and cannot be used to perform a bitwise comparison of two numeric expressions.

Note The AndAlso operator, unlike the And operator, only performs a logical comparison of two Boolean expressions, and cannot be used to perform a bitwise comparison of two numeric expressions.

The Or Operator

The Or operator performs a logical disjunction operation on two Boolean expressions and returns a Boolean value which, as Table 6-6 shows, is True unless both of the Boolean expressions being compared are False.

The Or Operator

The Or operator performs a logical disjunction operation on two Boolean expressions and returns a Boolean value which, as Table 6-6 shows, is True unless both of the Boolean expressions being compared are False.

Table 6-6: The Or Operator with Boolean Expressions

If First Expression Is

And Second Expression Is

Result Is

True

True

True

True

False

True

False

True

True

False

False

False

The following code shows how the Or operator works with Boolean expressions:

Dim A As Integer = 10

Dim B As Integer = 8

Dim C As Integer = 6

Dim myCheck As Boolean myCheck = A > B Or B > C 'Returns True.

The free movie ticket example discussed at the beginning of this module is a good example of when you would use the And operator, since if either condition (minor or senior citizen) is True, the result (get in free) is True. Only if both conditions are False do you have to pay.

Was this article helpful?

0 0

Post a comment