2 つのオペランドと 1 つのブール演算子で構成される式は、式が真の場合は True に評価され、式が偽の場合は False に評価されます。ただし、オペランドの一方が NULL の場合は、演算子とオペランドの種類により、結果は True、False、NULL のいずれかになります。可能な組み合わせは次のとおりです。
演算子 |
expr1 |
expr2 |
式の評価結果 |
---|---|---|---|
And |
はい |
はい |
はい |
はい |
いいえ |
いいえ |
|
いいえ |
はい |
いいえ |
|
いいえ |
いいえ |
いいえ |
|
Or |
はい |
はい |
はい |
はい |
いいえ |
はい |
|
いいえ |
はい |
はい |
|
いいえ |
いいえ |
いいえ |
|
Xor |
はい |
はい |
いいえ |
はい |
いいえ |
True |
|
いいえ |
はい |
はい |
|
いいえ |
いいえ |
いいえ |
|
Eqv |
はい |
はい |
はい |
はい |
いいえ |
いいえ |
|
いいえ |
はい |
いいえ |
|
いいえ |
いいえ |
はい |
|
Imp |
はい |
はい |
はい |
はい |
いいえ |
いいえ |
|
いいえ |
はい |
はい |
|
いいえ |
いいえ |
はい |
ブール演算子を含む数式の一方のオペランドが NULL の場合は、次の場合を除いて、式全体が NULL に評価されます。
次の例では、1 から 10 までの 2 つの整数をユーザーが入力し、最初の数値 (num1%) が 6 より小さいかどうか、そして 2 番目の値 (num2%) が 5 より大きいかどうかをテストします。次にこの 2 つのテストの真の値に基づいてメッセージを表示します。
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.