Or 运算符
用于对两个表达式执行按位析取。
语法:
result = expression1 Or expression2
- result
- 任意数值变量。
- expression1, expression2
- 任意表达式。
如果一个或两个表达式的计算结果为True,则result为True。下表说明result的确定方式:
| 如果 expression1 为 | 且 expression2 为 | 则 result 为 |
|---|---|---|
| True | True | True |
| True | False | True |
| True | Null | True |
| False | True | True |
| False | False | False |
| False | Null | Null |
| Null | True | True |
| Null | False | Null |
| Null | Null | Null |
Or运算符对两个数值表达式中相同位置的位执行按位比较,并根据下表设置result中的相应位:
| 如果 expression1 中的位为 | 且 expression2 中的位为 | 则 result 为 |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
INFO
Or每次都会计算两个操作数,即使仅expression1就能确定结果。使用OrElse进行短路求值——例如,当expression2计算开销大、有副作用,或仅当expression1为False时才有意义。
示例
本示例使用Or运算符对两个表达式执行逻辑析取。
vb
Dim A, B, C, D, MyCheck
A = 10: B = 8: C = 6: D = Null ' Initialize variables.
MyCheck = A > B Or B > C ' Returns True.
MyCheck = B > A Or B > C ' Returns True.
MyCheck = A > B Or B > D ' Returns True.
MyCheck = B > D Or B > A ' Returns Null.
MyCheck = A Or B ' Returns 10 (bitwise comparison).