LOGICAL OPERATORS

Logical operators combine two Boolean values and return True or False, depending on the result. The following table summarizes Visual Basic’s logical operators.

image

The operators Not, And, and Or are relatively straightforward.

“Xor” stands for “exclusive or,” and the Xor operator returns True if one but not both of its operands is true. The expression A Xor B is true if A is true or B is true but both are not true.

Xor is useful for situations where exactly one of two things should be true. For example, suppose you’re running a small software conference with two tracks so two talks are going on at any given time. Each attendee should sign up for one talk in each time slot but cannot sign up for both because they’re at the same time. Then you might use code similar to the following to check whether an attendee has signed up for either talk 1a or talk 1b but not both:

If talk1a Xor talk1b Then
    ' This is okay
    ...
End If

The AndAlso and OrElse operators are similar to the And and Or operators, except that they provide short-circuit evaluation. In short-circuit evaluation, Visual Basic is allowed to stop evaluating operands if it can deduce the final result without them. For example, consider the expression A AndAlso B. If Visual Basic evaluates the value A and discovers that it is False, the program knows that the expression A AndAlso B is also False no matter what value B has, so it doesn’t need to evaluate B.

Whether the program evaluates both operands doesn’t matter much if A and B are simple Boolean variables. However, assume that they are time-consuming functions in the following code. For example, the TimeConsumingFunction routine might need to look up values in a database or download data from a website. In that case, not evaluating the second operand might save a lot of time.

If TimeConsumingFunction("A") AndAlso TimeConsumingFunction("B") Then ...

Just as AndAlso can stop evaluation if it discovers one of its operands is False, the OrElse operand can stop evaluating if it discovers that one of its operands is True. The expression A OrElse B is True if either A or B is True. If the program finds that A is True, it doesn’t need to evaluate B.

Because AndAlso and OrElse do the same thing as And and Or but sometimes faster, you might wonder why you would ever use And and Or. The main reason is that the operands may have side effects. A side effect is some action a routine performs that is not obviously part of the routine. For example, suppose that the NumEmployees function opens an employee database and returns the number of employee records, leaving the database open. The fact that this function leaves the database open is a side effect.

Now, suppose that the NumCustomers function similarly opens the customer database, and then consider the following statement:

If (NumEmployees() > 0) AndAlso (NumCustomers() > 0) Then ...

After this code executes, you cannot be certain which databases are open. If NumEmployees returns 0, the AndAlso operator’s first operand is False, so it doesn’t evaluate the NumCustomers function and that function doesn’t open the customer database.

The AndAlso and OrElse operators can improve application performance under some circumstances. However, to avoid possible confusion and long debugging sessions, do not use AndAlso or OrElse with operands that have side effects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.85.100