js中andalso用法_VB.NET的逻辑运算符AndAlso和OrElse

js中andalso用法

VB.NET features two logical operators that help make your programming ... well ... more logical. The new operators are AndAlso and OrElse and they add a lot to the old And and Or operators.

VB.NET具有两个逻辑运算符,这些逻辑运算符有助于使您的编程……变得更……更逻辑。 新的运算符是AndAlsoOrElse ,它们为旧的And和Or运算符增加了很多。

什么是新的 ( What's New )

AndAlso and OrElse have some properties that enhance your code in ways that previous VB versions couldn't match. They offer advantages in two general categories:

AndAlso和OrElse具有一些属性,这些属性以以前的VB版本无法匹配的方式增强了您的代码。 它们在两个一般类别中提供优势:

  • You can avoid executing part of a logical expression to avoid problems.

    您可以避免执行逻辑表达式的一部分以避免出现问题。
  • You can optimize code by not executing any more of a compound expression than required.

    您可以通过不执行超出所需数量的复合表达式来优化代码。

AndAlso and OrElse are pretty much like And and Or except that they will "short circuit" an expression once the outcome is guaranteed.

AndAlso和OrElse非常类似于And and Or,不同之处在于一旦确定结果,它们将“短路”一个表达式。

( Example )

Suppose you're coding a test of a calculation result like this:

假设您要对计算结果的测试进行编码,如下所示:

The if expression generates a "divide by zero" error in VB 6 because Value3 is zero. (But see the Quick Tip on divide by zero for more on that.) It could be that the cases that result in Value3 being zero are very rare and only occur when you're enjoying a vacation a thousand miles away so you can be called back to fix the program in an emergency mode. (Hey! It happens!)

if表达式在VB 6中生成“被零除”错误,因为Value3为零。 (但有关更多信息,请参见除以零快速技巧 。)可能导致Value3为零的情况非常少见,只有在您享受千里之外的假期时才会发生,因此可以称为返回以在紧急模式下修复程序。 (嘿!发生了!)

Let's recode the program as a .NET program using AndAlso and see what happens.

让我们使用AndAlso将程序重新编码为.NET程序,然后看看会发生什么。

After changing And to AndAlso, the program works! The reason is that the last part of the compound If condition—(value 2 \ value3)—is never actually executed. When you use AndAlso, VB.NET knows that the expression can't succeed once it is determined that the first part of the condition—a is not greater than Value1—is false. So VB.NET stops evaluating the expression right there. A similar example could be constructed using OrElse.

将And更改为AndAlso后,该程序即可运行! 原因是复合If条件的最后一部分-(value 2 \ value3)-从未真正执行过。 当您使用AndAlso时,一旦确定条件的第一部分(a不大于Value1)为假,VB.NET就会知道表达式无法成功。 因此,VB.NET不再在那里评估表达式。 可以使用OrElse构造类似的示例。

This analysis also suggests how you can add some efficiency to your code by arranging a compound logical expression correctly. If you place the expression that is most likely to be false in the leftmost position when using AndAlso, you can prevent execution cycles from being used to evaluate the rightmost expression. In a single test, it wouldn't make enough difference to be worth even thinking about. But if your test is inside a loop of some kind and is executed zillions of times, it could make a big difference.

此分析还建议您如何通过正确安排复合逻辑表达式来提高代码效率。 如果在使用AndAlso时将最可能为假的表达式放在最左边,则可以防止执行周期被用来评估最右边的表达式。 在单个测试中,它不会产生什至值得考虑的差异。 但是,如果您的测试处于某种循环中并且执行了无数次,则可能会有很大的不同。

Knowing about these two new VB .NET logical operators can help you avoid very subtle errors or achieve subtle efficiencies.

了解这两个新的VB .NET逻辑运算符可以帮助您避免非常细微的错误或实现细微的效率。

翻译自: https://www.thoughtco.com/vbnets-logical-operators-andalso-and-orelse-3424268

js中andalso用法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值