Or Operator (Visual Basic)

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Performs a logical disjunction on two Boolean expressions, or a bitwise disjunction on two numeric expressions.

  
result = expression1 Or expression2  

result
Required. Any Boolean or numeric expression. For Boolean comparison, result is the inclusive logical disjunction of two Boolean values. For bitwise operations, result is a numeric value representing the inclusive bitwise disjunction of two numeric bit patterns.

expression1
Required. Any Boolean or numeric expression.

expression2
Required. Any Boolean or numeric expression.

For Boolean comparison, result is False if and only if both expression1 and expression2 evaluate to False. The following table illustrates how result is determined.

If expression1 isAnd expression2 isThe value of result is
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
System_CAPS_ICON_note.jpg Note

In a Boolean comparison, the Or operator always evaluates both expressions, which could include making procedure calls. The OrElse Operator performs short-circuiting, which means that if expression1 is True, then expression2 is not evaluated.

For bitwise operations, the Or operator performs a bitwise comparison of identically positioned bits in two numeric expressions and sets the corresponding bit in result according to the following table.

If bit in expression1 isAnd bit in expression2 isThe bit in result is
111
101
011
000
System_CAPS_ICON_note.jpg Note

Since the logical and bitwise operators have a lower precedence than other arithmetic and relational operators, any bitwise operations should be enclosed in parentheses to ensure accurate execution.

If the operands consist of one Boolean expression and one numeric expression, Visual Basic converts the Boolean expression to a numeric value (–1 for True and 0 for False) and performs a bitwise operation.

For a Boolean comparison, the data type of the result is Boolean. For a bitwise comparison, the result data type is a numeric type appropriate for the data types of expression1 and expression2. See the "Relational and Bitwise Comparisons" table in Data Types of Operator Results.

The Or operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

The following example uses the Or operator to perform an inclusive logical disjunction on two expressions. The result is a Boolean value that represents whether either of the two expressions is True.

        Dim a As Integer = 10
        Dim b As Integer = 8
        Dim c As Integer = 6
        Dim firstCheck, secondCheck, thirdCheck As Boolean
        firstCheck = a > b Or b > c
        secondCheck = b > a Or b > c
        thirdCheck = b > a Or c > b

The preceding example produces results of True, True, and False, respectively.

The following example uses the Or operator to perform inclusive logical disjunction on the individual bits of two numeric expressions. The bit in the result pattern is set if either of the corresponding bits in the operands is set to 1.

        Dim a As Integer = 10
        Dim b As Integer = 8
        Dim c As Integer = 6
        Dim firstPattern, secondPattern, thirdPattern As Integer
        firstPattern = (a Or b)
        secondPattern = (a Or c)
        thirdPattern = (b Or c)

The preceding example produces results of 10, 14, and 14, respectively.

Logical/Bitwise Operators (Visual Basic)
Operator Precedence in Visual Basic
Operators Listed by Functionality
OrElse Operator
Logical and Bitwise Operators in Visual Basic

Show: