Boolean Operators (Formula Language)

An expression consisting of two operands and a Boolean operator evaluates to True if the expression is true, and False if it is false, unless one of the operands is NULL. In that case, the result may be NULL or True or False, depending on the operator and the operand. The possibilities are:

Operator

If expr1 is

And expr2 is

The expression evaluates to

And

True

True

True

True

False

False

False

True

False

False

False

False

Or

True

True

True

True

False

True

False

True

True

False

False

False

Xor

True

True

False

True

False

True

False

True

True

False

False

False

Eqv

True

True

True

True

False

False

False

True

False

False

False

True

Imp

True

True

True

True

False

False

False

True

True

False

False

True

When an operand in a numeric expression including a Boolean operator is NULL, the whole expression evaluates to NULL except under the following circumstances:

  • If the operator is AND and the other operand is False, then the expression evaluates to False.
  • If the operator is OR and the other operand is True, then the expression evaluates to True.
  • If the operator is IMP and the first operand is False, then the expression evaluates to True.
  • If the operator is IMP and the second operand is True, then the expression evaluates to True.

This example has the user enter two integers between 1 and 10. It tests to see if the first (num1%) is less than 6 and if the second (num2%) is greater than 5. Then it displays a message according to the truth value of the two tests.

Dim num1 As Integer
Dim num2 As Integer 
num1% = InputBox("Enter an integer between 1 and 10:")
num2% = InputBox("Enter an integer between 1 and 10:")
Print "num1 = " & num1% & " num2 = " & num2%
If num1% <6 And num2% >5 Then 
   Print "And:" & num1% & " is less than 6 and " & num2% & _
      " is greater than 5."
End If
If num1% <6 Or num2% >5 Then
   Print "Or:" & num1% & " is less than 6 or " & num2% & _  
      " is greater than 5, or both."
End If
If num1% <6 XOr num2% >5 Then
   Print "XOr: " & num1% & " is less than 6 or " & num2% & _
      " is greater than 5, but not both."
End If
If num1% <6 Eqv num2% >5 Then
   Print "Eqv:" & num1% & " is less than 6 and " & num2% & _
      " is greater than 5, or " & num1% & _
      " is greater than 5 and " & num2% & " is less than 6."
End If
If num1% <6 Imp num2% >5 Then 
   Print "Imp:" & num1% & " is less than 6 and " & num2% & _
      " is greater than 5, or " & num1% & _
      " is greater than 5 and " & num2% & _
      " is less than 6, or " & num1% &  _
      " is greater than 5 and " & num2% & _
      " is greater than 5."
End If
' Sample Output:
' num1 = 6 num2 = 6 
' Or: 6 is less than 6 or 6 is greater than 5, or both.
' XOr: 6 is less than 6 or 6 is greater than 5, but not both.
' Imp: 6 is less than 6 and 6 is greater than 5, or 6 is 
' greater than 5 and 6 is less than 6, or 6 
' is greater than 5 and 6 is greater than 5.

' num1 = 10 num2 = 1 
' Eqv: 10 is less than 6 and 1 is greater than 5, or 10
' is greater than 5 and 1 is less than 6.
' Imp: 10 is less than 6 and 1 is greater than 5, or 10 is
' greater than 5 and 1 is less than 6, or 
' 10 is greater than 5 and 1 is greater than 5.

' num1 = 5 num2 = 9 
' And: 5 is less than 6 and 9 is greater than 5.
' Or: 5 is less than 6 or 9 is greater than 5, or both.
' Eqv: 5 is less than 6 and 9 is greater than 5, or 5
' is greater than 5 and 9 is less than 6.
' Imp: 5 is less than 6 and 9 is greater than 5, or 5 is
' greater than 5 and 9 is less than 6, or 
' 5 is greater than 5 and 9 is greater than 5.