VB.NET 中 IF 语句的计算

发布于 2024-07-13 05:31:51 字数 399 浏览 9 评论 0原文

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

情况1:

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

情况2:

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

情况3:

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

For the following If-statements in VB.NET, what will be the sequence in which the conditions will be evaluated?

Case 1:

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

Case 2:

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

Case 3:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾旎 2024-07-20 05:31:51

VB.Net 评估所有条件,因此顺序在这里并不重要。

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

VB.Net evaluates all the conditions, so the order isn't important here.

If you want to use short-circuiting, use the AndAlso and OrElse keywords.

浊酒尽余欢 2024-07-20 05:31:51

从 C 程序员的角度来看,VB.NET 是一个非常奇怪的野兽。 正如 Gerrie 在另一个答案中提到的那样,所有三个条件都会得到完整的评估,而不会短路。 如果您想要的话,AndAlso 和 OrElse 可以拯救您的一天。

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

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

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

VB.NET is a very strange beast from the C-programmer standpoint. As Gerrie mentioned in a different answer, all three conditions are evaluated in their full entirety without short-circuiting. AndAlso and OrElse can save your day if that's what you want.

As for the last if, the order of evaluation is as follows:

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

As a rule of thumb: if there's any ambiguitiy, use brackets to specify order of evaluation explicitly.

舂唻埖巳落 2024-07-20 05:31:51

这并没有完全回答手头的问题,因为它已经被回答了,我觉得有必要扩展安东提到的“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 

还有 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 

但由于顺序会对“OrElse”和“AndAlso”产生影响,因此应首先评估对象是否为空,如上面的示例所示。

如果“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 写了这篇文章。

This is not exactly answering the question at hand, as it was already answered, I felt the need to expand on the 'OrElse' keyword, mentioned by Anton, as well as it's relative: 'AndAlso', because someone landing on this question my actually want an explanation on these.

'OrElse' and 'AndAlso' Is useful if you require an explicitly ordered evaluation.

Unlike 'Or' and 'And', were all the expressions are evaluated, the If statement can skip the remaining expressions if the first expression evaluates to the desired value, when you make use of 'OrElse' or 'AndAlso'.

In the following case, order of the expressions are applicable to 'OrElse' from Left to Right, but also does not always evaluate all the expressions depending on the result of the preceding value :

if( Expression1 orelse Expression2 orelse Expression3 )

    ' Code...

End If

If Expression1 is true, then Expression 2 and 3 are ignored.
If Expression1 if False, then Expression2 is evaluated, and then Expression3 if Expression2 evaluates to True.

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

An alternate example of the 'OrElse' usage:

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

The above example is the same as the following:

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 

There is also the AndALso keyword:

' If the first expression evaluates to false, the remaining two expressions are ignored
If( Expression1 AndAlso Expression2 AndAlso Expression3 ) Then ...

Which is usable like this:

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 

Unlike 'And', like 'Or'- will always evaluate all expressions:

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 

But because order can have an effect with 'OrElse' and 'AndAlso', evaluating weather an object is null should be done first, as in the above examples.

The following will cause an exception if 'MyObject' is 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

DO please comment if I made a mistake or typo here, as I wrote this at 5:16AM in the morning after 2 days without sleep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文