java 如果有多个&&, || java中的条件评估

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34428952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:53:33  来源:igfitidea点击:

If with multiple &&, || conditions Evaluation in java

javaif-statement

提问by Abish R

if(a && b || c || d || e)

whether it check (a && b), if aand bmust be true always, then only allow inside ? or

是否检查(a && b),如果ab必须始终为真,则只允许在里面?或者

((a && b) || a && c || a && d || a && e)), any of the condition is true will it allow inside ?

((a && b) || a && c || a && d || a && e)), 任何一个条件为真会允许进入吗?

回答by dave

If I understand you correctly, in the first part of your question, you are asking whether aand bmust both be true for the entire expression (a && b || c || d || e)to evaluate as true.

如果我理解正确,那么在问题的第一部分,您会问是否ab必须都为真,整个表达式(a && b || c || d || e)才能评估为真。

The answer to that question is no. Java operators are such that &&has higher precedence than ||. So the equivalent expression is:

这个问题的答案是否定的。Java 运算符的&&优先级高于||. 所以等价的表达式是:

(a && b) || c || d || e

Therefore the expression as a whole will evaluate to true if any of a && bor cor dor eis true. For example, if awas false and cwas true, the expression as a whole is true.

因此,如果a && borcdor 中的任何一个为真,则表达式作为一个整体将评估e为真。例如,如果a为假且c为真,则整个表达式为真。

Note that Java conditional operators short-circuit. This means that once the end result of the expression is known, evaluation stops. For example, if aand bwere both true, meaning the overall expression must evaluate to true, then c, dand eare not evaluated.

请注意,Java 条件运算符会短路。这意味着一旦知道表达式的最终结果,计算就会停止。例如,如果ab都是真实的,这意味着整个表达式的值必须为真,那么cd并且e不被评估。

For the second part of your question, we can apply the same logic. The equivalent expression is therefore:

对于您问题的第二部分,我们可以应用相同的逻辑。因此等价的表达式是:

(a && b) || (a && c) || (a && d) || (a && e)

This will evaluate to true if any of the sub-components, eg. a && cevaluates to true. As others have noted, amust be true for the expression to be true as it is part of every sub-component. Then if any of the other variables is true, the expression is true.

如果有任何子组件,这将评估为真,例如。a && c评估为真。正如其他人所指出的,a表达式必须为真,因为它是每个子组件的一部分。然后,如果任何其他变量为真,则表达式为真。

Once you understand that, you can see how the simplification of this expression suggested by @Arc676 is arrived at:

一旦你理解了这一点,你就可以看到@Arc676 建议的这个表达式的简化是如何实现的:

a && (b || c || d || e)

I should also add that the two expressions in your question are different logically. They are equivalent to:

我还应该补充一点,你问题中的两个表达在逻辑上是不同的。它们相当于:

(a && b) || c || d || e

and

a && (b || c || d || e)

The parentheses affect the order of evaluation. In the first, aand bare grouped together by an &&, hence both must be true for that part of the expression (in the parentheses) to be true. In the second, b, c, dand eare grouped together by ||. In this case, only one of b, c, dand eneeds to be true for that part of the expression (in the parentheses) to be true.

括号影响求值顺序。在第一个中,ab由一个 组合在一起&&,因此对于表达式的那部分(在括号中)为真,两者都必须为真。在第二个中bcde由 组合在一起||。在这种情况下,只有b, c,d和 中的一个e需要为真,表达式的那部分(在括号中)才能为真。

回答by Hassaan Ahmad

if this is a part of a big code then you'll have to simplify the condition such that the processor will process fast in order to give you an accurate compilation in lesser time. the most appropriate condition according to me can be " a && (b|| c|| d|| e) " so by using such condition it will just do it in one step rather than going into every condition calling "a" everytime....

如果这是大代码的一部分,那么您必须简化条件,以便处理器能够快速处理,以便在更短的时间内为您提供准确的编译。根据我的说法,最合适的条件可以是“a && (b|| c|| d|| e)”,所以通过使用这样的条件,它只会在一个步骤中完成,而不是每次都进入调用“a”的每个条件。 ...

for more help you can check on this link and also helps me a lot... Hope you'll get your answer : " http://www.homeandlearn.co.uk/java/java_if_else_statements.html" .

如需更多帮助,您可以查看此链接,也对我有很大帮助...希望您能得到答案:“ http://www.homeandlearn.co.uk/java/java_if_else_statements.html”。

回答by learningbyexample

((a && b) || (a && c) || (a && d) || (a && e))

((a && b) || (a && c) || (a && d) || (a && e))

in this one, a has to always be true and either b, c, d, or e has to be true to go in.

在这一点中,a 必须始终为真,并且 b、c、d 或 e 必须为真才能进入。

if(a && b || c || d || e)

if(a && b || c || d || e)

in this one, a and b have to be true, other wise only c or only d or only e have to be true.

在这一点中,a 和 b 必须是真的,否则只有 c 或只有 d 或只有 e 必须是真的。

in the snippet it shows that a and b have to be true. if a and b are not true then it can go check if c or d or e are true then it will still be true.

在代码片段中,它显示 a 和 b 必须为真。如果 a 和 b 不为真,那么它可以去检查 c 或 d 或 e 是否为真,那么它仍然是真的。

function test(a, b, c, d, e) {

  if ((a && b || c || d || e)) {

    return a;

  }
}
document.getElementById("demo1").innerHTML = " a = 1, b = 0, c = 0, d = 0, e = 0 answer: " + test(1, 0, 0, 0, 0);
document.getElementById("demo2").innerHTML = " a = 0, b = 1, c = 0, d = 0, e = 0 answer: " + test(0, 1, 0, 0, 0);
document.getElementById("demo3").innerHTML = " a = 1, b = 1, c = 0, d = 0, e = 0 answer: " + test(1, 1, 0, 0, 0);
document.getElementById("demo4").innerHTML = " a = 0, b = 0, c = 1, d = 0, e = 0 answer: " + test(0, 0, 1, 0, 0);
<h1>JavaScript Variables</h1>

<p>In this example, a, b, c, d and e are variables</p>

<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>

回答by miva2

Dave's answer is pretty good. Look there for an explanation. If you are still having trouble I'll simplify it.

戴夫的回答很好。看看那里的解释。如果您仍然遇到问题,我会简化它。

Case 1:

情况1:

if(a && b || c || d || e)

In this case you can simplify it by saying z = a && b
This means that the expression becomes

在这种情况下,您可以通过说z = a && b
这来简化它这意味着表达式变为

if(z || c || d || e)  

This is the same as the expression above. zis only true if aand bare true.

这与上面的表达式相同。z仅当ab为真时才为真。

Case 2:

案例2:

((a && b) || a && c || a && d || a && e))

As Arc676 commented, you can simplify it as

正如 Arc676 评论的那样,您可以将其简化为

a && (b || c || d || e)

Here you can use the same technique as in case 1 to further simplify it. If you take y = b || c || d || e. Meaning yis true if any of b, c, dor eare true. Then you can simplify the expression as

在这里,您可以使用与案例 1 相同的技术来进一步简化它。如果你拿y = b || c || d || e. 含义y如有的是真实的bcd或者e是真实的。然后你可以将表达式简化为

a && y

I'm not saying you should always simplify this but using this technique can make long boolean statements more readable.

我并不是说你应该总是简化它,但使用这种技术可以使长布尔语句更具可读性。

回答by Shiva

Well, I go with @dave answer with good explanation.

好吧,我和@dave 一起回答并给出了很好的解释。

I would like to add few points. Here, the catch is how or in which order you add parentheses in the order of evaluation.

我想补充几点。在这里,问题在于您如何或以何种顺序按照计算顺序添加括号。

And a && bacts as a single element.

a && b充当单个元素。

For more info on if-else logic you can refer thisresource.

有关 if-else 逻辑的更多信息,您可以参考资源。

回答by Imran

if(a && b || c || d || e)stands for:

if(a && b || c || d || e)代表:

If ais trueand bor cor dor eis true then it will go inside. That's it. So, if ais trueand at least any rest other variables(bor cor dor e) then it will go inside. Otherwise it won't.

如果ais trueand bor cor dore是真的,那么它会去inside。而已。因此,如果atrue,并且至少有任何其他变量(bcde),那么它将进入内部。否则不会。