&&的优先权高于|| [英] The precedence of && is higher than ||

查看:88
本文介绍了&&的优先权高于||的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码的输出为:

The output for the code below is:

1. true
2. false
1. true
2. true





我理解为什么第一行输出应为真。



我不明白为什么第二行输出应该是假的,因为13< = 5和13!= 12使得'false','false'和13> 10使'真''。



我不明白为什么第3行输出应该是真的,因为12> 10或12< = 5使'真''和'真''和12!= 12使得假。



我理解为什么第4行输出应该是真的。











I understand why the 1st line of output should be true.

I don't understand why the 2st line of output should be false, because 13 <= 5 and 13 != 12 makes 'false', 'false' and 13 > 10 makes 'true'.

I don't understand why the 3rd line of output should be true, because 12 > 10 or 12 <= 5 makes 'true', and 'true' and 12 != 12 makes false.

I understand why the 4rd line of output should be true.




public class MyProgram
{
    public void start()
    {
        doBooleans(12);
        doBooleans(13);
    }

    private void doBooleans(int value)
    {
        boolean result;

        result = value > 10 || value <= 5 && value != 12;
        System.out.println("1. " + result);

        result = (value > 10 || value <= 5) && value != 12;
        System.out.println("2. " + result);
    }
}

推荐答案

你可以通过以下链接。



http://msdn.microsoft.com/ en-us / library / aa691323(v = vs.71).aspx [ ^ ]
you can go through below link.

http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx[^]


Quote:

我不明白为什么第二行输出应该是假的,因为13< = 5和13!= 12使得'false','false'和13> 10使'真''。

I don't understand why the 2st line of output should be false, because 13 <= 5 and 13 != 12 makes 'false', 'false' and 13 > 10 makes 'true'.



因为第二行仍然是你的第一个方法调用的输出,你传递12作为参数(而不是13),所以你实际上有(12> 10 || 12 <= 5)&& 12!= 12 。括号内的部分是 true ,最后一部分是 false ,所以你有为真&安培;&安培; false 哪个是假的。


Because the second line is still output of your first method call, you pass 12 as parameter (and not 13), so you actually have (12 > 10 || 12 <= 5) && 12 != 12. The part inside parentheses is true, and the last part is false, so you have true && false which is false.

引用:

我不明白为什么第3个输出线应为真,因为12> 10或12< = 5使'真''和'真''和12!= 12使得假。

I don't understand why the 3rd line of output should be true, because 12 > 10 or 12 <= 5 makes 'true', and 'true' and 12 != 12 makes false.



第三行是第二种方法的输出,所以你通过13作为参数(而不是12),你有 13> 10 || 13< = 5&& 13!= 12 。那么,第一部分, 13> 10 为true,第二部分为false, true || false 为真。


The third line is the output of the second method, so you pass 13 as parameter (and not 12), and you have 13 > 10 || 13 <= 5 && 13 != 12. So, the first part, 13 > 10 is true, and the second part is false, and true || false is true.


首先,请保留此图表 [ ^ ]和你一起参考它一直都是。

让我们做一个例子,这一行没有括号

First, keep this chart [^] with you and refer to it all the times.
Let do one example, this line without parenthesis
12 > 10 || 12 <= 5 && 12 != 12



优先顺序如下:(左侧优先级较高)


The precedence order is like this: (Higher precedence on the left)

!, <= , !=, &&, ||



因此该行变为:


so that line becomes:

true || false && false



因为&&优先于||,接下来变为:


Since && takes precedence over ||, it next becomes:

true || false



最终结果是什么,是真的!

你应该能够找出另一个。我的建议总是使用括号来使你的意图清晰明确。


What is the final outcome, true!
You should be able to figure out the other one. My advice is always use parenthesis to make your intention clear and explicit.


这篇关于&amp;&amp;的优先权高于||的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆