[英]How is an Or statement evaluated in VBA?
I am just wondering how does an Or
conditional statement work in VBA/VB6. 我只是想知道
Or
条件语句在VBA / VB6中是如何工作的。 Basically, if we have If A Or B Then
, in what order are the Boolean expressions evaluated? 基本上,如果我们有
If A Or B Then
,布尔表达式的评估顺序是什么? If A is true, is B also evaluated? 如果A为真,B还会被评估吗?
Here are some test results for you: 以下是一些测试结果:
Public Sub DoTesting()
' Displays "Test1" followed by "Test2", so they're evaluated in order.
' Also, both expressions are evaluated even though Test1() is False.
If Test1() And Test2() Then
End If
' Displays "Test2" followed by "Test1", so they're evaluated in order.
' Also, both expressions are evaluated even though Test2() is True.
If Test2() Or Test1() Then
End If
' Displays "Test1" only. Test2() is not evaluated.
If Test1() Then If Test2() Then Debug.Print ""
End Sub
Public Function Test1() As Boolean
MsgBox "Test1"
Test1 = False
End Function
Public Function Test2() As Boolean
MsgBox "Test2"
Test2 = True
End Function
So, both expressions in an Or
or an And
are always evaluated, in order, regardless of outcome. 因此,在这两个表达式
Or
或And
一直被运用,为了不分胜负。 You can use If ... Then If ... Then
to achieve simple inline short-circuiting. 您可以使用
If ... Then If ... Then
实现简单的内联短路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.