[英]How is an Or statement evaluated in VBA?
我只是想知道Or
條件語句在VBA / VB6中是如何工作的。 基本上,如果我們有If A Or B Then
,布爾表達式的評估順序是什么? 如果A為真,B還會被評估嗎?
以下是一些測試結果:
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
因此,在這兩個表達式Or
或And
一直被運用,為了不分勝負。 您可以使用If ... Then If ... Then
實現簡單的內聯短路。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.