给定的 if 条件如何在 java 中给出 true
How a given if condition is giving true in java
我无法理解为什么执行以下 if 块。如何评估 if 条件?
public class Test
{
public static void main(String[] args)
{
if (true || (false || true) && false)
{
System.out.println("How does this condition becomes true.");
}
if (false && (false || true) || true)
{
System.out.println("Same with this condition, why is it true.");
}
}
}
&&
的优先级高于 ||
(source)。所以添加括号以强调这些是如何评估的:
if (true || ((false || true) && false))
// -------------^-------------------------^
和
if ((false && (false || true)) || true)
// -----^-------------------------^
如您所见,您最终得到 x || y
,其中 x
在第一个示例中为 true
,而 y
在第二个示例中为真。 true || false
和false || true
自然都是true
.
你可以通过使用输出它正在做什么的方法来看到它的动作(live copy)(记住 ||
和 &&
短路,意思是他们不评估不能改变结果的操作数):
public class Test
{
private static boolean b(String label, boolean value) {
System.out.println(label + ": " + value);
return value;
}
public static void main(String[] args)
{
if (b("1", true) || (b("2", false) || b("3", true)) && b("4", false))
{
System.out.println("How does this condition becomes true.");
}
if (b("5", false) && (b("6", false) || b("7", true)) || b("8", true))
{
System.out.println("Same with this condition, why is it true.");
}
}
}
当运行时,输出:
1: true
How does this condition becomes true.
5: false
8: true
Same with this condition, why is it true.
好吧,我们来看看第一行:
if (true || (false || true) && false)
根据优先级,将首先评估 ()
之间的所有内容。所以,那将是 false || true
,我们可以将 ||
写成 OR
,这意味着我们得到 true OR false
,在这种情况下它将是 true
,因为一个这两个值中的一个是 true
.
好了我们继续,变成这样:
if (true || true && false)
现在我们来到你困惑的部分。在这种情况下,我们不会从左到右阅读。我们需要先看一下 &&
而不是 ||
。所以我们需要检查 true && false
评估,这将成为 false
:
然后我们留下以下语句:
if (true || false)
当然会变成true
。第二个也是一样。
首先,你必须已经知道false || true
是true
。
除此之外,你还需要知道另外两件事:
- short circuiting.
The && and || operators "short-circuit", meaning they don't evaluate the right hand side if it isn't necessary.
When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
从优先级table,我们知道&&
高于||
。
所以在你的问题中,让我们进行基本推理。
if (true || (false || true) && false)
true || no_matter_what_is_in_right_side
将是 true
,所以我们得到了 true
。
if (false && (false || true) || true)
这个比上一个更棘手,这次我们必须考虑烦人的优先级。我们的推理如下:
false && no_matter_what_is_in_right_side
将是 false
,我们只剩下:
false || true
最后还是 true
。
但是如果你已经注意到了这个技巧:我们从最右边的开始 true
那么我们就可以按照前面的推理直接得到true
.
正如迈克尔所说,我们最好使用括号来表达确切的顺序,而不是依赖优先顺序,这实际上很难记住##。
使用||
如果至少一个操作数为真,则条件为真。例如。 false || true
结果为 true
。
所以你的第一个条件为真,因为 第一个为真 (true || ...
。
if (true || (false || true) && false)
......|...............................
第二个条件为真,因为最后一个为真 ... || true)
if (false && (false || true) || true)
...................................|..
看看Truth Table。
OR
P Q P||Q
--------------------
T F T
F T T
T T T
F F F
我无法理解为什么执行以下 if 块。如何评估 if 条件?
public class Test
{
public static void main(String[] args)
{
if (true || (false || true) && false)
{
System.out.println("How does this condition becomes true.");
}
if (false && (false || true) || true)
{
System.out.println("Same with this condition, why is it true.");
}
}
}
&&
的优先级高于 ||
(source)。所以添加括号以强调这些是如何评估的:
if (true || ((false || true) && false))
// -------------^-------------------------^
和
if ((false && (false || true)) || true)
// -----^-------------------------^
如您所见,您最终得到 x || y
,其中 x
在第一个示例中为 true
,而 y
在第二个示例中为真。 true || false
和false || true
自然都是true
.
你可以通过使用输出它正在做什么的方法来看到它的动作(live copy)(记住 ||
和 &&
短路,意思是他们不评估不能改变结果的操作数):
public class Test
{
private static boolean b(String label, boolean value) {
System.out.println(label + ": " + value);
return value;
}
public static void main(String[] args)
{
if (b("1", true) || (b("2", false) || b("3", true)) && b("4", false))
{
System.out.println("How does this condition becomes true.");
}
if (b("5", false) && (b("6", false) || b("7", true)) || b("8", true))
{
System.out.println("Same with this condition, why is it true.");
}
}
}
当运行时,输出:
1: true How does this condition becomes true. 5: false 8: true Same with this condition, why is it true.
好吧,我们来看看第一行:
if (true || (false || true) && false)
根据优先级,将首先评估 ()
之间的所有内容。所以,那将是 false || true
,我们可以将 ||
写成 OR
,这意味着我们得到 true OR false
,在这种情况下它将是 true
,因为一个这两个值中的一个是 true
.
好了我们继续,变成这样:
if (true || true && false)
现在我们来到你困惑的部分。在这种情况下,我们不会从左到右阅读。我们需要先看一下 &&
而不是 ||
。所以我们需要检查 true && false
评估,这将成为 false
:
然后我们留下以下语句:
if (true || false)
当然会变成true
。第二个也是一样。
首先,你必须已经知道false || true
是true
。
除此之外,你还需要知道另外两件事:
- short circuiting.
The && and || operators "short-circuit", meaning they don't evaluate the right hand side if it isn't necessary.
When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
从优先级table,我们知道&&
高于||
。
所以在你的问题中,让我们进行基本推理。
if (true || (false || true) && false)
true || no_matter_what_is_in_right_side
将是 true
,所以我们得到了 true
。
if (false && (false || true) || true)
这个比上一个更棘手,这次我们必须考虑烦人的优先级。我们的推理如下:
false && no_matter_what_is_in_right_side
将是 false
,我们只剩下:
false || true
最后还是 true
。
但是如果你已经注意到了这个技巧:我们从最右边的开始 true
那么我们就可以按照前面的推理直接得到true
.
正如迈克尔所说,我们最好使用括号来表达确切的顺序,而不是依赖优先顺序,这实际上很难记住##。
使用||
如果至少一个操作数为真,则条件为真。例如。 false || true
结果为 true
。
所以你的第一个条件为真,因为 第一个为真 (true || ...
。
if (true || (false || true) && false)
......|...............................
第二个条件为真,因为最后一个为真 ... || true)
if (false && (false || true) || true)
...................................|..
看看Truth Table。
OR
P Q P||Q
--------------------
T F T
F T T
T T T
F F F