Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Boolean Expressions

A Boolean expression is an expression that evaluates to a Boolean value. Boolean expressions can take several forms. The simplest is the direct comparison of the value of a Boolean variable to a Boolean literal, as shown in this example:

If x = True Then   ' Compares x to the Boolean value True.
   ' Insert code to execute if x = True.
   ' Insert code to execute if x = False.
End If

Note that the assignment statement x = True looks the same as the expression in the example, but it performs a different function and is used differently. In the above example, the expression x = True represents a value, and thus the = sign is interpreted as a comparison operator. In a stand-alone statement, it would be interpreted as an assignment operator and assign the value on the right to the variable on the left. The following example illustrates this:

If x = True then   ' Compare the value of x to Boolean value True. 
   y = False      ' Assign the Boolean value True to variable y.
End If

For further information, see Value Comparisons and Assignment Statements.

Comparison operators, such as =, <, >, <>, <=, and >=, produce Boolean expressions by comparing the expression on the left side of the operator to the expression on the right side of the operator and evaluating the result as true or false. For instance:

42 < 81   ' Evaluates to True.

For more information on this kind of expression, see Value Comparisons.

Comparison expressions can be combined using logical operators to produce more complex Boolean expressions. The following example demonstrates the use of comparison operators in conjunction with a logical operator:

x > y And x < 1000

In the example, the value of the overall expression depends on the values of the expressions on each side of the And operator. If both expressions are true, then the overall expression evaluates as True. If either expression is false, then the entire expression evaluates to False.

Short-Circuiting Operators

The logical operators AndAlso and OrElse exhibit behavior known as short-circuiting. A short-circuiting operator evaluates the left expression first. If the left expression falsifies (in the case of AndAlso) or validates (in the case of OrElse) the entire expression, then program execution proceeds without evaluating the right expression. Take the following example:

If 45 < 12 AndAlso MyFunction(3) = 81 Then
   ' Add code to continue execution.

In this example, the operator evaluates the left expression, 45 < 12. Since this expression evaluates to False, the entire logical expression evaluates to False. Program execution thus begins execution of the code within the If block without evaluating the right expression, MyFunction(3). In this example, MyFunction() is never called because the left expression falsifies the entire expression.

Similarly, if the left expression in a logical expression using OrElse evaluates to True, execution proceeds to the next line of code without evaluating the second expression because the first expression has already enabled the entire expression.

By contrast, both sides of the logical operator are evaluated when the logical operators And and Or are used. Consider the following expression for example:

If 45 < 12 And MyFunction(3) = 81 Then
   ' Add code to continue execution.

In this example, a call is made to MyFunction(), even though the first expression evaluates to False.

Parenthetical Expressions

You can use parentheses to control the evaluation of Boolean expressions. Expressions enclosed by parentheses evaluate first. For multiple levels of nesting, precedence is granted to the most deeply-nested expressions. Within parentheses, evaluation proceeds according to the rules of operator precedence. For more information, see Operator Precedence in Visual Basic and Efficient Combination of Operators.

See Also

Logical Operators | Value Comparisons | Assignment Statements | Comparison Operators | Efficient Combination of Operators | Operator Precedence in Visual Basic

© 2015 Microsoft