8

对于 VB.NET 中的以下 If 语句,评估条件的顺序是什么?

情况1:

If ( condition1 AND condition2 AND condition3 )
.
.
End If

案例二:

If ( condition1 OR condition2 OR condition3 )
.
.
End If

案例3:

If ( condition1 OR condition2 AND condition3  OR condition4)
.
.
End If
4

3 回答 3

11

VB.Net 评估所有条件,所以这里的顺序并不重要。

如果要使用短路,请使用 AndAlso 和 OrElse 关键字。

于 2009-02-02T08:39:31.403 回答
9

从 C 程序员的角度来看,VB.NET 是一头非常奇怪的野兽。正如 Gerrie 在另一个答案中提到的那样,所有三个条件都经过完整评估,没有短路。如果您愿意,AndAlso 和 OrElse 可以为您节省一天的时间。

至于最后一个if,求值顺序如下:

If ((condition1 OR (condition2 AND condition3))  OR condition4)

根据经验:如果有任何歧义,请使用方括号明确指定评估顺序。

于 2009-02-02T08:43:25.407 回答
1

这并不完全回答手头的问题,因为已经回答了,我觉得有必要扩展 Anton 提到的“OrElse”关键字,以及它的相对:“AndAlso”,因为有人登陆这个问题我实际上想要对这些进行解释。

如果您需要明确排序的评估,则“OrElse”和“AndAlso”很有用。

与 'Or' 和 'And' 不同的是,如果所有表达式都被求值,如果使用 'OrElse' 或 'AndAlso',如果第一个表达式求值为所需值,则 If 语句可以跳过剩余的表达式。

在以下情况下,表达式的顺序适用于从左到右的“OrElse”,但并不总是根据前一个值的结果计算所有表达式:

if( Expression1 orelse Expression2 orelse Expression3 )

    ' Code...

End If

如果 Expression1 为真,则表达式 2 和 3 将被忽略。如果 Expression1 为 False,则计算 Expression2,如果 Expression2 计算为 True,则计算 Expression3。

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 

还有AndALso关键字:

' 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 

但是因为 order 可以对 '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 写了这篇文章。

于 2013-12-17T03:17:13.277 回答