C++ 二元运算符的优先顺序

发布于 2024-07-06 00:47:11 字数 377 浏览 6 评论 0原文

以下参数按什么顺序测试(在 C++ 中)?

if (a || b && c)
{
}

我刚刚在我们的应用程序中看到了这段代码,我讨厌它。 我想添加一些括号来澄清顺序。 但我不想添加括号,直到我知道我将它们添加到正确的位置。

C++ 内置运算符、优先级和关联性< /em> 有更多信息,但并不完全清楚它的含义。 看来|| 和&& 具有相同的优先级,在这种情况下,它们将从左到右进行计算。

In what order are the following parameters tested (in C++)?

if (a || b && c)
{
}

I've just seen this code in our application and I hate it. I want to add some brackets to just clarify the ordering. But I don't want to add the brackets until I know I'm adding them in the right place.

C++ built-in operators, precedence, and associativity has more information, but it's not totally clear what it means. It seems || and && are the same precedence, and in that case, they are evaluated left-to-right.

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

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

发布评论

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

评论(5

灵芸 2024-07-13 00:47:11

页面C++ 运算符优先级(通过谷歌搜索“C++ 运算符优先级”找到) ") 告诉我们第 13 组中的 && 的优先级高于第 14 组中的 ||,因此该表达式相当于 || (b && c)。

不幸的是,Wikipedia_文章C 和 C++ 中的运算符,运算符优先级 不同意这一点,但由于我有 C89 标准我的办公桌与第一个站点一致,我将修改维基百科文章。

The page C++ Operator Precedence (found by googling "C++ operator precedence") tells us that &&, in group 13, has higher precedence than || in group 14, so the expression is equivalent to a || (b && c).

Unfortunately, the Wikipedia_ article Operators in C and C++, Operator precedence disagrees with this, but since I have the C89 standard on my desk and it agrees with the first site, I'm going to revise the Wikipedia article.

多彩岁月 2024-07-13 00:47:11

来自此处

a || (b && c)

这是默认优先级。

From here:

a || (b && c)

This is the default precedence.

音盲 2024-07-13 00:47:11

&& (布尔 AND) 的优先级高于 || (布尔或)。 因此,以下内容是相同的:

a || b && c
a || (b && c)

一个好的助记规则是记住 AND 类似于乘法,OR 类似于加法。 如果我们用 * 代替 AND,用 + 代替 OR,我们会得到一个更熟悉的等价物:

a + b * c
a + (b * c)

实际上,在布尔逻辑中,AND 和 OR 的作用类似于这些算术运算符:

a  b   a AND b   a * b   a OR b   a + b
---------------------------------------
0  0      0        0       0        0
0  1      0        0       1        1
1  0      0        0       1        1
1  1      1        1       1        1 (2 really, but we pretend it's 1)

&& (boolean AND) has higher precedence than || (boolean OR). Therefore the following are identical:

a || b && c
a || (b && c)

A good mnemonic rule is to remember that AND is like multiplication and OR is like addition. If we replace AND with * and OR with +, we get a more familiar equivalent:

a + b * c
a + (b * c)

Actually, in Boolean logic, AND and OR act similar to these arithmetic operators:

a  b   a AND b   a * b   a OR b   a + b
---------------------------------------
0  0      0        0       0        0
0  1      0        0       1        1
1  0      0        0       1        1
1  1      1        1       1        1 (2 really, but we pretend it's 1)
§对你不离不弃 2024-07-13 00:47:11

回答后续问题:显然 MSDN 上的表格是拙劣的,可能是由某人无法制作一个像样的 HTML 表格(或使用 Microsoft 工具生成它!)。
我想它应该看起来更像维基百科表罗德里戈引用,我们有明确的小节。

但显然,接受的答案是正确的,不知何故,我们与 && 具有相同的优先级; 和 || 例如,与 * 和 + 相比。

您给出的代码片段对我来说是清晰明确的,但我想添加括号也不会造成伤害。

To answer the follow-up: obviously the table at MSDN is botched, perhaps by somebody unable to do a decent HTML table (or using a Microsoft tool to generate it!).
I suppose it should look more like the Wikipedia table referenced by Rodrigo, where we have clear sub-sections.

But clearly the accepted answer is right, somehow we have same priority with && and || than with * and +, for example.

The snippet you gave is clear and unambiguous for me, but I suppose adding parentheses wouldn't hurt either.

寒尘 2024-07-13 00:47:11

我不确定,但你应该很容易找到。

只需创建一个小程序,其中包含一条打印出以下值的真值的语句:
(true || false && true)

如果结果为 true,则 || 的优先级高于 && ,如果为假,则相反。

I'm not sure, but it should be easy for you to find out.

Just create a small program with a statement that prints out the truth value of:
(true || false && true)

If the result is true, then the || has higher precedence than &&, if it is false, it's the other way around.

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