Important | This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer |

# Logical Operators

**Visual Studio .NET 2003**

The logical operators compare **Boolean** expressions and return a **Boolean** result. The **And**, **Or**, **AndAlso**, **OrElse**, and **Xor** operators take two operands, and the **Not** operator takes a single operand.

The **Not** operator performs logical negation on a **Boolean** expression. Put simply, it yields the opposite of the expression it evaluates. If the expression evaluates to **True**, **Not** yields **False**; if the expression evaluates to **False**, **Not** yields **True**. An example is shown below:

Dim x As Boolean x = Not 23 > 12 ' x equals False. x = Not 23 > 67 ' x equals True.

The **And** operator performs logical conjunction on two **Boolean** expressions. That is, if both expressions evaluate to **True**, then the **And** operator returns **True**. If either or both expressions evaluate to **False**, then **And** returns **False**.

The **Or** operator performs logical disjunction on two **Boolean** expressions. If either expression evaluates to **True**, **Or** returns **True**. If neither expression evaluates to **True**, **Or** returns **False**.

**Xor** performs logical exclusion on two expressions. If either expression evaluates to **True**, but not both, **Xor** returns **True**. If both expressions evaluate to **True** or both expressions evaluate to **False**,** Xor** returns **False**.

Examples of the **And**, **Or**, and **Xor** operators are shown below:

Dim x As Boolean x = 23 > 12 And 12 >4 ' x = True x = 12 > 23 And 12 > 4 ' x = False x = 23 > 12 Or 4 > 12` ' x = True x = 23 > 45 Or 4 > 12 ' x = False x = 23 > 45 Xor 12 > 4 ' x = True x = 23 > 12 Xor 12 > 4 ' x = False x = 12 > 23 Xor 4 > 12 ' x = False

In addition to being logical operators,NoteNot,Or,And, andXoralso perform bitwise arithmetic when used on numeric values. Information on this functionality can be found at Arithmetic Operators.

The **AndAlso** operator is very similar to the **And** operator, in that it also performs logical conjunction on two **Boolean** expressions. The key difference between **AndAlso** and **And** is that **AndAlso** exhibits short-circuiting behavior. If the first expression in an **AndAlso** expression evaluates to **False**, then the second expression is not evaluated and **False** is returned for the **AndAlso** expression.

Similarly, the **OrElse** operator performs short-circuiting logical disjunction on two **Boolean** expressions. If the first expression in an **OrElse** expression evaluates to **True**, then the second expression is not evaluated and **True** is returned for the **OrElse** expression. Below are some examples that illustrate the difference between **And**, **Or**, and their counterparts:

12 > 45 And MyFunction(4) ' MyFunction() is called. 12 > 45 AndAlso MyFunction(4) ' MyFunction() is not called. 45 > 12 Or MyFunction(4) ' MyFunction is called. 45 > 12 OrElse MyFunction(4) ' MyFunction is not called

In the first example, `MyFunction()`

is called, even though `12 > 45`

returns **False**, because **And **does not short circuit. In the second example, `MyFunction`

is not called, because `12 > 45`

returns **False**, so **AndAlso** short circuits the second expression. In the third example, `MyFunction`

is called, even though `45 > 12`

returns **True**, because **Or** does not short circuit. In the fourth example, `MyFunction`

is not called because `45 > 12 `

returns **True**, so **OrElse** short circuits the second expression.

NoteAlthoughAndandOrsupport bitwise operations with numeric values,AndAlsoandOrElsedo not.

#### See Also

Logical/Bitwise Operators | Arithmetic Operators | Boolean Expressions | Efficient Combination of Operators