逻辑运算的优先顺序,带括号的(Java)

huangapple go评论65阅读模式
英文:

Order of precedence for logical operations with parentheses (Java)

问题

只想知道在Java中的布尔表达式中,括号是否比逻辑运算符具有更高的优先级。

例如:
(x > 0 || x < 10) && y < 0 与 (x > 0 || (x < 10 && y < 0)) 是否相同?

英文:

Just wondering if parentheses have higher precedence than logical operators in a boolean expression in java.

For example:
Is (x > 0 || x < 10) && y < 0 the same as (x > 0 || (x < 10 && y < 0))?

(edited format)

答案1

得分: 1

是的,他们做到了。就像在数学运算中添加括号来分隔项一样。您提供的示例是不同的。

在第一个示例中,您是在说:如果任何这些条件为真,并且另一个条件为真→那么返回真。这意味着第二个条件必须始终为真。

在第二个示例中,您是在说:如果第一个条件为真,或者这两个其他条件都为真→那么返回真。这意味着如果第一个条件为真,则不关心其余条件。

假设我们为X赋值1,为Y赋值2。第一个示例将返回false,因为不管X值如何,Y都必须小于2。
然而,第二种情况将返回true。因为X大于0,所以它将返回true,因为它不关心其余条件,因为它是一个或门。

英文:

Yes, they have. It's like adding parenthesis on a math operation to separate in terms. The examples you provide are NOT the same.

In the first one you are saying: if any of these conditions are true AND this other conditions is true→then return true. Meaning that the second condition always has to be true.

In the second one you are saying: If the first condition is true OR both this other condition are true→ then return true. This means that if the first condition is true, you don't care about the rest.

Let's say we plug in a value of 1 for X and a value of 2 for Y. The first one will return false because Y has to be smaller than 2 regardless of the X value.
The second case however, will return true. Since X is greater than 0 then it will return true as it doesn't care about the rest because it's an OR gate.

答案2

得分: 1

在逻辑条件中使用括号不会改变条件的优先级,因为布尔操作始终会从左到右执行。

括号可以用来“分组”子条件,这些子条件在较大的表达式中使用,但是这些括号的存在不会优先执行它们。请看下面的示例:

class Scratch {
    public static void main(String[] args) {
        if (f() || (t() && f())){
            System.out.println("finish");
        }
    }

    public static boolean f() {
        System.out.println("false");
        return false;
    }

    public static boolean t() {
        System.out.println("true");
        return true;
    }
}

这将输出:

false
true
false

因为这对应于从左到右的执行顺序。请注意,如果对 OR 函数的第一个条目为 true(在我的示例代码中使用 t()),则后面的条件甚至不会被评估,因为 OR 条件已经满足。

英文:

The presence of brackets within logical conditions do not change the precedence of your condition, as boolean operations will always execute from left to right.

Brackets can be used to "group" sub-conditions that are used in a larger expression but the presence of these will not give any priority to their execution. See the below example:

 class Scratch {
    public static void main(String[] args) {
        if (f() || (t() &amp;&amp; f())){
            System.out.println(&quot;finish&quot;);
        }
    }

    public static boolean f() {
        System.out.println(&quot;false&quot;);
        return false;
    }

    public static boolean t() {
        System.out.println(&quot;true&quot;);
        return true;
    }
}

This will output:

false
true
false

As this corresponds to the left to right order of execution. Note that if the first entry to an OR function is true (Using t() in my example code) then the latter condition isn't even evaluated as the OR condition has already been satisfied.

huangapple
  • 本文由 发表于 2020年8月23日 00:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63538765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定