C# 条件 AND (&&) OR (||) 优先级
在我的工作中,我们总是陷入不必要的编码争论。 今天我问条件 AND (&&) 或 OR (||) 是否具有更高的优先级。 我的一位同事坚持认为他们具有相同的优先级,我对此表示怀疑,所以我查了一下。
根据 MSDN,AND (&&) 的优先级高于 OR (||)。 但是,你能向持怀疑态度的同事证明这一点吗?
http://msdn.microsoft.com/en-us/ library/aa691323(VS.71).aspx
bool result = false || true && false; // --> false
// is the same result as
bool result = (false || true) && false; // --> false
// even though I know that the first statement is evaluated as
bool result = false || (true && false); // --> false
所以我的问题是如何用代码证明 AND (&&) 的优先级高于 OR (||)? 如果你的答案是没关系,那么为什么它在语言中是这样构建的呢?
We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up.
According to MSDN AND (&&) has higher precedence than OR (||). But, can you prove it to a skeptical coworker?
http://msdn.microsoft.com/en-us/library/aa691323(VS.71).aspx
bool result = false || true && false; // --> false
// is the same result as
bool result = (false || true) && false; // --> false
// even though I know that the first statement is evaluated as
bool result = false || (true && false); // --> false
So my question is how do you prove with code that AND (&&) has a higher precedence that OR (||)? If your answer is it doesn't matter, then why is it built that way in the language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你真的想吓唬他,请尝试:
这将打印:
编辑:
响应评论:
在 C# 中,
|
是一个逻辑运算符,执行与||< 相同的布尔逻辑/code>,但不短路。 同样在 C# 中,
|
运算符的优先级高于||
和&&
。通过打印这些值,您可以看到,如果我使用典型的
||
运算符,则只会打印第一个True
- 后面是表达式的结果也是True
。但由于
|
的优先级较高,所以true | 首先评估 false
(得到true
),然后将结果用&&
编辑为false< /code> 产生
false
。我并不是想展示评估的顺序,只是事实是
|
的右半部分被评估了,而通常情况下不会:)If you really want to freak him out try:
This will print:
Edit:
In response to the comment:
In C#,
|
is a logical operator that performs the same boolean logic as||
, but does not short-circuit. Also in C#, the|
operator has a higher precedence than both||
and&&
.By printing out the values, you can see that if I used the typical
||
operator, only the firstTrue
would be printed - followed by the result of the expression which would have beenTrue
also.But because of the higher precedence of
|
, thetrue | false
is evaluated first (resulting intrue
) and then that result is&&
ed withfalse
to yieldfalse
.I wasn't trying to show the order of evaluation, just the fact that the right half of the
|
was evaluated period when it normally wouldn't be :)将第一个 false 更改为 true。 我知道拥有 (true || true) 似乎很愚蠢,但它证明了你的观点。
Change the first false by true. I know it seems stupid to have (true || true) but it proves your point.
这难道不会得到你所追求的吗? 或者也许我错过了一些东西......
Wouldn't this get you what you're after? Or maybe I'm missing something...
你不用代码来证明,而是用逻辑来证明。 AND 是布尔乘法,而 OR 是布尔加法。 现在哪一个具有更高的优先级?
You don't prove it with code but with logic. AND is boolean multiplication whereas OR is boolean addition. Now which one has higher precedence?
<代码>假|| 真&& true
产量:
true
false && 真实 || true
产量:
true
false || true && true
Yields:
true
false && true || true
Yields:
true
当布尔表达式短路时,您不能只显示最终结果。 这是解决您问题的片段。
它依赖于实施和 和| && 使用的运算符 和 ||,如 MSDN 7.11 条件逻辑运算符<中所述/a>
输出应显示 && 首先在 || 之前评估。
为了获得额外的乐趣,请尝试使用 result = t || t&& f 看看短路会发生什么。
You cannot just show the end result when your boolean expressions are being short-circuited. Here's a snippet that settles your case.
It relies on implementing & and | operators used by && and ||, as stated in MSDN 7.11 Conditional logical operators
The output should show that && is evaluated first before ||.
For extra fun, try it with result = t || t && f and see what happens with short-circuiting.