The OR operator is used in a boolean expression to check that there is at least one true. If both sides are true, the entire expression is true. If just one side is true, the entire expression is true. If both sides are false, the entire expression is false. The OR operator is a logical operator because it combines two true/false values into a single true/false value.
Here is how || works:
- true || true = true
- false || true = true
- true || false = true
- false || false = false
OR checks that at least one requirement is met. This type of OR is called an inclusive OR because its value is true for one or two true values.
Often in English word "or" is used when any number of conditions can be true. For example, in this sentence
Successful job seekers must have experience or training.
Sometimes the English word "or" is used when only one condition can be true at a time. For example, in this sentence
It will rain today or it will be clear.
only one condition, "rain" or "clear", can be true. This is called an exclusive OR. In programming, "or" means inclusive or.
Skip to main content
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Boolean Expressions [Visual Basic]
- Article
- 09/15/2021
- 3 minutes to read
In this article
A Boolean expression is an expression that evaluates to a value of the Boolean Data Type: True or False. 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 the following example.
If newCustomer = True Then ' Insert code to execute if newCustomer = True. Else ' Insert code to execute if newCustomer = False. End IfTwo Meanings of the = Operator
Notice that the assignment statement newCustomer = True looks the same as the expression in the preceding example, but it performs a different function and is used differently. In the preceding example, the expression newCustomer = True represents a Boolean value, and the = sign is interpreted as a comparison operator. In a stand-alone statement, the = sign is interpreted as an assignment operator and assigns the value on the right to the variable on the left. The following example illustrates this.
If newCustomer = True Then newCustomer = False End IfFor further information, see Value Comparisons and Statements.
Comparison Operators
Comparison operators such as =, , , = 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. The following example illustrates this.
42 < 81
Because 42 is less than 81, the Boolean expression in the preceding example evaluates to True. For more information on this kind of expression, see Value Comparisons.
Comparison Operators Combined with Logical Operators
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 preceding 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 to 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 operand first. If the left operand determines the value of the entire expression, then program execution proceeds without evaluating the right expression. The following example illustrates this.
If 45 < 12 AndAlso testFunction[3] = 81 Then ' Add code to continue execution. End IfIn the preceding example, the operator evaluates the left expression, 45 < 12. Because the left expression evaluates to False, the entire logical expression must evaluate to False. Program execution thus skips execution of the code within the If block without evaluating the right expression, testFunction[3]. This example does not call testFunction[] 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 right expression, because the left expression has already validated the entire expression.
Comparison with Non-Short-Circuiting Operators
By contrast, both sides of the logical operator are evaluated when the logical operators And and Or are used. The following example illustrates this.
If 45 < 12 And testFunction[3] = 81 Then ' Add code to continue execution. End IfThe preceding example calls testFunction[] even though the left expression evaluates to False.
Parenthetical Expressions
You can use parentheses to control the order of 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.
See also
- Logical and Bitwise Operators in Visual Basic
- Value Comparisons
- Statements
- Comparison Operators
- Efficient Combination of Operators
- Operator Precedence in Visual Basic
- Boolean Data Type
Feedback
Submit and view feedback for