C# 条件 AND (&&) OR (||) 优先级

发布于 2024-07-30 02:21:46 字数 749 浏览 4 评论 0原文

在我的工作中,我们总是陷入不必要的编码争论。 今天我问条件 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 技术交流群。

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

发布评论

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

评论(6

我也只是我 2024-08-06 02:21:46

如果你真的想吓唬他,请尝试:

bool result = True() | False() && False();

Console.WriteLine("-----");
Console.WriteLine(result);

static bool True()
{
    Console.WriteLine(true);
    return true;
}

static bool False()
{
    Console.WriteLine(false);
    return false;
}

这将打印:

True
False
False
-----
False

编辑:

响应评论:

在 C# 中,| 是一个逻辑运算符,执行与 ||< 相同的布尔逻辑/code>,但不短路。 同样在 C# 中,| 运算符的优先级高于 ||&&

通过打印这些值,您可以看到,如果我使用典型的 || 运算符,则只会打印第一个 True - 后面是表达式的结果也是True

但由于|的优先级较高,所以true | 首先评估 false(得到 true),然后将结果用 && 编辑为 false< /code> 产生 false

我并不是想展示评估的顺序,只是事实是 | 的右半部分被评估了,而通常情况下不会:)

If you really want to freak him out try:

bool result = True() | False() && False();

Console.WriteLine("-----");
Console.WriteLine(result);

static bool True()
{
    Console.WriteLine(true);
    return true;
}

static bool False()
{
    Console.WriteLine(false);
    return false;
}

This will print:

True
False
False
-----
False

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 first True would be printed - followed by the result of the expression which would have been True also.

But because of the higher precedence of |, the true | false is evaluated first (resulting in true) and then that result is &&ed with false to yield false.

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 :)

吾家有女初长成 2024-08-06 02:21:46

将第一个 false 更改为 true。 我知道拥有 (true || true) 似乎很愚蠢,但它证明了你的观点。

bool result = true || true && false;   // --> true 
     result = (true || true) && false; // --> false
     result = true || (true && false); // --> true

Change the first false by true. I know it seems stupid to have (true || true) but it proves your point.

bool result = true || true && false;   // --> true 
     result = (true || true) && false; // --> false
     result = true || (true && false); // --> true
莫言歌 2024-08-06 02:21:46

这难道不会得到你所追求的吗? 或者也许我错过了一些东西......

bool result = true || false && false;

Wouldn't this get you what you're after? Or maybe I'm missing something...

bool result = true || false && false;
生死何惧 2024-08-06 02:21:46

你不用代码来证明,而是用逻辑来证明。 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?

夏尔 2024-08-06 02:21:46

<代码>假|| 真&& true

产量: true

false && 真实 || true

产量:true

false || true && true

Yields: true

false && true || true

Yields: true

街道布景 2024-08-06 02:21:46

当布尔表达式短路时,您不能只显示最终结果。 这是解决您问题的片段。

它依赖于实施和 和| && 使用的运算符 和 ||,如 MSDN 7.11 条件逻辑运算符<中所述/a>

public static void Test()
{
    B t = new B(true);
    B f = new B(false);

    B result = f || t && f;

    Console.WriteLine("-----");
    Console.WriteLine(result);
}

public class B {
    bool val;
    public B(bool val) { this.val = val; }
    public static bool operator true(B b) { return b.val; }
    public static bool operator false(B b) { return !b.val; }
    public static B operator &(B lhs, B rhs) { 
        Console.WriteLine(lhs.ToString() + " & " + rhs.ToString());
        return new B(lhs.val & rhs.val); 
    }
    public static B operator |(B lhs, B rhs) { 
        Console.WriteLine(lhs.ToString() + " | " + rhs.ToString());
        return new B(lhs.val | rhs.val); 
    }
    public override string ToString() { 
        return val.ToString(); 
    }
}

输出应显示 && 首先在 || 之前评估。

True & False
False | False
-----
False

为了获得额外的乐趣,请尝试使用 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

public static void Test()
{
    B t = new B(true);
    B f = new B(false);

    B result = f || t && f;

    Console.WriteLine("-----");
    Console.WriteLine(result);
}

public class B {
    bool val;
    public B(bool val) { this.val = val; }
    public static bool operator true(B b) { return b.val; }
    public static bool operator false(B b) { return !b.val; }
    public static B operator &(B lhs, B rhs) { 
        Console.WriteLine(lhs.ToString() + " & " + rhs.ToString());
        return new B(lhs.val & rhs.val); 
    }
    public static B operator |(B lhs, B rhs) { 
        Console.WriteLine(lhs.ToString() + " | " + rhs.ToString());
        return new B(lhs.val | rhs.val); 
    }
    public override string ToString() { 
        return val.ToString(); 
    }
}

The output should show that && is evaluated first before ||.

True & False
False | False
-----
False

For extra fun, try it with result = t || t && f and see what happens with short-circuiting.

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