VB.NET中IF语句的求值
对于VB.NET中的以下If语句,条件的求值顺序是什么 案例1:VB.NET中IF语句的求值,vb.net,evaluation,if-statement,Vb.net,Evaluation,If Statement,对于VB.NET中的以下If语句,条件的求值顺序是什么 案例1: If ( condition1 AND condition2 AND condition3 ) . . End If If ( condition1 OR condition2 OR condition3 ) . . End If If ( condition1 OR condition2 AND condition3 OR condition4) . . End If 案例2: If ( condition1 AND c
If ( condition1 AND condition2 AND condition3 )
.
.
End If
If ( condition1 OR condition2 OR condition3 )
.
.
End If
If ( condition1 OR condition2 AND condition3 OR condition4)
.
.
End If
案例2:
If ( condition1 AND condition2 AND condition3 )
.
.
End If
If ( condition1 OR condition2 OR condition3 )
.
.
End If
If ( condition1 OR condition2 AND condition3 OR condition4)
.
.
End If
案例3:
If ( condition1 AND condition2 AND condition3 )
.
.
End If
If ( condition1 OR condition2 OR condition3 )
.
.
End If
If ( condition1 OR condition2 AND condition3 OR condition4)
.
.
End If
Net计算所有条件,因此顺序在这里并不重要
如果要使用短路,请使用AndAlso和OrElse关键字。从C程序员的角度来看,VB.NET是一个非常奇怪的野兽。正如Gerrie在另一个回答中提到的,所有三个条件都是在没有短路的情况下进行完整评估的。如果你想的话,他们也可以帮你节省时间 对于最后一个if,评估顺序如下:
If ((condition1 OR (condition2 AND condition3)) OR condition4)
经验法则:如果有任何含糊不清的地方,请使用括号明确指定评估顺序。这并不能完全回答手边的问题,因为已经回答了,我觉得需要扩展Anton提到的“OrElse”关键字,以及它的相对值:“AndAlso”,因为有人提出了这个问题,我真的想解释一下 “OrElse”和“AndAlso”在需要显式排序的求值时非常有用 与“Or”和“and”不同,如果所有表达式都已计算,则当使用“OrElse”或“AndAlso”时,如果第一个表达式的计算结果为所需值,If语句可以跳过其余表达式 在以下情况下,表达式的顺序从左到右适用于“OrElse”,但并不总是根据前面值的结果计算所有表达式:
if( Expression1 orelse Expression2 orelse Expression3 )
' Code...
End If
如果表达式1为true,则忽略表达式2和3。
如果表达式1为False,则计算表达式2,如果表达式2的计算结果为True,则计算表达式3
If (False OrElse True OrElse False ) then
' Expression 1 and 2 are evaluated, Expression 3 is ignored.
End If
If (True OrElse True OrElse True ) then
' only Expression 1 is evaluated.
End If
If (True OrElse True OrElse True ) then
' only Expression 1 is evaluated.
End If
If (False OrElse False OrElse True ) then
' All three expressions are evaluated
End If
“OrElse”用法的另一个示例:
If( (Object1 is Nothing) OrElse (Object2 is Nothing) OrElse (Object3 is Nothing) ) then
' At least one of the 3 objects is not null, but we dont know which one.
' It is possible that all three objects evaluated to null.
' If the first object is null, the remaining objects are not evaluated.
' if Object1 is not NULL and Object2 is null then Object3 is not evaluated
Else
' None of the objects evaluated to null.
Endif
上述示例与以下示例相同:
If(Object1 Is Nothing)
' Object 1 is Nothing
Return True
Else
If(Object2 Is Nothing)
' Object 2 is Nothing
Return True
Else
If(Object3 Is Nothing)
' Object 3 is Nothing
Return True
Else
' One of the objects evaluate to null
Return False
End If
End If
End If
还有和关键字:
' If the first expression evaluates to false, the remaining two expressions are ignored
If( Expression1 AndAlso Expression2 AndAlso Expression3 ) Then ...
它的可用性如下:
If( (Not MyObject is Nothing) AndAlso MyObject.Enabled ) Then ...
' The above will not evaluate the 'MyObject.Enabled' if 'MyObject is null (Nothing)'
' MyObject.Enabled will ONLY be evaluated if MyObject is not Null.
' If we are here, the object is NOT null, and it's Enabled property evaluated to true
Else
' If we are here, it is because either the object is null, or it's enabled property evaluated to false;
End If
与“And”不同,类似“Or”将始终计算所有表达式:
If( (Not MyObject is Nothing) And MyObject.Enabled ) Then ...
' MyObject.Enabled will ALWAYS be evaluated, even if MyObject is NULL,
' --- which will cause an Exception to be thrown if MyObject is Null.
End If
但由于顺序可能对“OrElse”和“AndAlso”产生影响,因此应首先评估对象是否为null,如上面的示例所示
如果“MyObject”为NUll,则以下内容将导致异常
Try
If( MyObject.Enabled AndAlso (Not MyObject is Nothing) ) Then ...
' This means that first expression MyObject.Enabled Was evaluated to True,
' Second Expression also Evaluated to True
Else
' This means MyObject.Enabled evaluated to False, thus also meaning the object is not null.
' Second Expression "Not MyObject is Nothing" was not evaluated.
End If
Catch(e as Exception)
' An exception was caused because we attempted to evaluate MyObject.Enabled while MyObject is Nothing, before evaluating Null check against the object.
End Try
如果我在这里犯了错误或输入错误,请发表评论,因为我在两天没有睡觉的情况下,在早上5:16写了这篇文章。理论上,顺序可能很重要,例如:IsStatusOkAndIfNotChangePropertyX(MyObject)或PropertyXHasValueN(MyObject)然后…可能是的,但我不知道你是否可以相信。我在这里着陆,发现你的信息非常有用。