VB.NET 中 IF 语句的计算
对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
从 C 程序员的角度来看,VB.NET 是一个非常奇怪的野兽。 正如 Gerrie 在另一个答案中提到的那样,所有三个条件都会得到完整的评估,而不会短路。 如果您想要的话,AndAlso 和 OrElse 可以拯救您的一天。
至于最后一个if,求值顺序如下:
根据经验:如果有任何歧义,请使用括号明确指定求值顺序。
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:
As a rule of thumb: if there's any ambiguitiy, use brackets to specify order of evaluation explicitly.
这并没有完全回答手头的问题,因为它已经被回答了,我觉得有必要扩展安东提到的“OrElse”关键字,以及它的相对:“AndAlso”,因为有人登陆这个问题我的实际上想要对这些进行解释。
如果您需要显式排序的计算,“OrElse”和“AndAlso”很有用。
与“Or”和“And”不同,如果所有表达式都被求值,那么当您使用“OrElse”或“AndAlso”时,如果第一个表达式的求值结果为所需值,则 If 语句可以跳过剩余的表达式。
在以下情况下,表达式的顺序适用于从左到右的“OrElse”,但并不总是根据前面值的结果计算所有表达式:
如果表达式 1 为 true,则忽略表达式 2 和 3 。
如果表达式 1 为 False,则计算表达式 2;如果表达式 2 计算结果为 True,则计算表达式 3。
'OrElse' 用法的另一个示例:
上面的示例与下面的相同:
还有 AndALso 关键字:
可以这样使用:
与 'And' 不同,就像 'Or'-将始终评估所有表达式:
但由于顺序会对“OrElse”和“AndAlso”产生影响,因此应首先评估对象是否为空,如上面的示例所示。
如果“MyObject”为 NULL,以下内容将导致异常
如果我在这里犯了错误或拼写错误,请发表评论,因为我在两天不睡觉后的凌晨 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 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.
An alternate example of the 'OrElse' usage:
The above example is the same as the following:
There is also the AndALso keyword:
Which is usable like this:
Unlike 'And', like 'Or'- will always evaluate all expressions:
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
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.