Visual Basic .NET/Logical operators

Logical OperatorsEdit

NotEdit

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

AndEdit

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

AndAlsoEdit

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

OrEdit

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

OrElseEdit

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

XorEdit

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
Last modified on 20 March 2014, at 11:34