Visual Basic .NET/Logical operators

Logical Operators edit

Not edit

The Not operator returns True when the condition is False. Otherwise, it returns False. Example,

If Not (1 = 2) Then
   MessageBox.Show("(1 = 2) is False. So Not False is True")
End If
Truth Table
Condition Not Condition
True False
False True

And edit

The And operator returns True when the conditions of the left and right are True. Otherwise, it returns False. Both conditions are evaluated before the result is returned. Example,

If (1 = 1) And (2 = 2) Then
   MessageBox.Show("(1 = 1) is True. (2 = 2) is True. So True And True is True")
End If
Truth Table
Condition1 Condition2 Condition1 And Condition2
True True True
True False False
False True False
False False False

AndAlso edit

The AndAlso operator returns False when the condition on the left hand side is False. Else, it returns True when the conditions of the left and right are True. Otherwise, it returns False. The condition on the right hand side is never evaluated when that on the left hand side is False. This is called short-circuited logic.

Truth Table
Condition1 Condition2 Condition1 AndAlso Condition2
True True True
True False False
False - False

Or edit

The Or operator returns True when the condition on either side is True. Otherwise, it returns False. Both conditions are evaluated before the result is returned.

Truth Table
Condition1 Condition2 Condition1 Or Condition2
True True True
True False True
False True True
False False False

OrElse edit

The OrElse operator returns True when the condition on the left hand side is True. Else, if the condition on the right hand side is True, it returns True. Otherwise, it returns False. The condition on the right hand side is never evaluated when that on the left hand side is True. This is called short-circuited logic.

Truth Table
Condition1 Condition2 Condition1 OrElse Condition2
True - True
False True True
False False False

Xor edit

The Xor operator returns True when the condition on the left hand side or right hand side is True, but not when both are True. Xor means "Exclusive OR".

Truth Table
Condition1 Condition2 Condition1 Xor Condition2
True True False
True False True
False True True
False False False