is And 运算符总是在 Or 之前计算
今天早上我在一些 VB6 代码中发现了一个错误,该错误无法正确评估。代码采用以下格式:
<Boolean Value 1> Or <Boolean Value 2> And <Boolean Value 3>
修复(在本例中)是将括号放入如下所示:
(<Boolean Value 1> Or <Boolean Value 2>) And <Boolean Value 3>
因为 And
首先被错误评估,所以我的问题是 - 情况总是如此吗?
我假设像 + - * 这样的东西是使用 BIDMAS 规则进行评估的,但是这些运算符又如何与或非异或是<>
等
I came across a bug in some VB6 code this morning which wasn't evaluating correctly. The code is in the following format:
<Boolean Value 1> Or <Boolean Value 2> And <Boolean Value 3>
The fix (in this case) was to put parentheses in as follows:
(<Boolean Value 1> Or <Boolean Value 2>) And <Boolean Value 3>
Because the And
was being incorrectly evaluated first so my question is - Is this always the case?
I assume that things like + - * are evaluated using the BIDMAS rule but what about these operators And Or Not XOr Is <>
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Visual Basic 中的运算符
优先级 出现顺序:
Operator Precedence in Visual Basic
Precedence in order of appearance:
是的,就像在大多数编程语言中一样,
and
的绑定比or
更强,因此在这种情况下括号是必要的。有趣的是,VB6 没有短路操作,这意味着如果您有
if isNumeric(var) and myFunc(var) then ...
myFunc 甚至会在您的 var 被执行时被执行不是数字!这对性能和正确性有影响。Yes, like in most programming languages,
and
is binding stronger thanor
, so the brackets are necessary in this case.What also might be interesting is that VB6 has no short-circuit operations, meaning that if you have
if isNumeric(var) and myFunc(var) then ...
myFunc is even executed in case your var is not numeric! This has implications on performance and correctness.