is And 运算符总是在 Or 之前计算

发布于 2024-11-29 00:08:09 字数 505 浏览 1 评论 0原文

今天早上我在一些 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 技术交流群。

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

发布评论

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

评论(2

苍暮颜 2024-12-06 00:08:09

Visual Basic 中的运算符

优先级 出现顺序:

Negation (Not)

Conjunction (And, AndAlso)

Inclusive disjunction (Or, OrElse)

Exclusive disjunction (Xor)

Operator Precedence in Visual Basic

Precedence in order of appearance:

Negation (Not)

Conjunction (And, AndAlso)

Inclusive disjunction (Or, OrElse)

Exclusive disjunction (Xor)
落墨 2024-12-06 00:08:09

是的,就像在大多数编程语言中一样,and 的绑定比 or 更强,因此在这种情况下括号是必要的。

有趣的是,VB6 没有短路操作,这意味着如果您有 if isNumeric(var) and myFunc(var) then ... myFunc 甚至会在您的 var 被执行时被执行不是数字!这对性能和正确性有影响。

Yes, like in most programming languages, and is binding stronger than or, 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.

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