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.

Open table as spreadsheet

Operator

Purpose

Example

Result

Not

Logical or bitwise negation

Not A

True if A is false

And

Logical or bitwise And

A And B

True if A and B are both true

Or

Logical or bitwise Or

A Or B

True if A or B or both are true

Xor

Logical or bitwise exclusive Or

A Xor B

True if A or B but not both is true

AndAlso

Logical or bitwise And with short-circuit evaluation

A AndAlso B

True if A and B are both true (see notes)

OrElse

Logical or bitwise Or with short-circuit evaluation

A OrElse B

True if A or B or both are true (see the following notes)

The operators Not, And, and Or are relatively straightforward. The Xor operator returns True if one of its operands is true and the other one is false.

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, as shown in the following code. For example, the TimeConsumingFunction function might need to look up values in a database or download data from a web site. 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.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net