if(a && b || c || d || e)
whether it check (a && b), if a and b must be true always, then only allow inside ? or
((a && b) || a && c || a && d || a && e)), any of the condition is true will it allow inside ?
If I understand you correctly, in the first part of your question, you are asking whether a and b must both be true for the entire expression (a && b || c || d || e) to evaluate as true.
The answer to that question is no. Java operators are such that && has higher precedence than ||. So the equivalent expression is:
(a && b) || c || d || e
Therefore the expression as a whole will evaluate to true if any of a && b or c or d or e is true. For example, if a was false and c was true, the expression as a whole is true.
Note that Java conditional operators short-circuit. This means that once the end result of the expression is known, evaluation stops. For example, if a and b were both true, meaning the overall expression must evaluate to true, then c, d and e are not evaluated.
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 && c evaluates to true. As others have noted, a must 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.
Once you understand that, you can see how the simplification of this expression suggested by #Arc676 is arrived at:
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, a and b are 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, d and e are grouped together by ||. In this case, only one of b, c, d and e needs to be true for that part of the expression (in the parentheses) to be true.
Dave's answer is pretty good. Look there for an explanation.
If you are still having trouble I'll simplify it.
Case 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
if(z || c || d || e)
This is the same as the expression above. z is only true if a and b are true.
Case 2:
((a && b) || a && c || a && d || a && e))
As Arc676 commented, you can simplify it as
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 y is true if any of b, c, d or e are true. Then you can simplify the expression as
a && y
I'm not saying you should always simplify this but using this technique can make long boolean statements more readable.
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....
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 " .
((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.
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.
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.
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>
if(a && b || c || d || e) can be evaluated as both 1 and 2 below :
1 -> if ( a && ( b || c || d || e) )
2 -> if ( (a && b) || (a && c) || (a && d) || (a && e))
Conclusion will be as below :
statement will be true when --> a is true and anyone of b or c or d or e is true
if(a && b || c || d || e) stands for:
If a is true and b or c or d or e is true then it will go inside. That's it.
So, if a is true and at least any rest other variables(b or c or d or e) then it will go inside. Otherwise it won't.
Related
Is it possible to add a condition in if statement if a certain condition is true?
For example,
I have this variable,
private Boolean doesExist
if doesExist is set to True then I want my if statement like this,
if (A == B && B == C)
if doesExist is set to False then I don't want the second condition in my if condition,
if (A == B)
How about something like this?
if ((doesExist && (A == B && B == C)) || (!doesExist && A == B)) {
// Do stuff
}
When doesExist is true, the predicate depends on (A == B && B == C) (left side of the 'or' operator); when doesExist is false, the predicate depends on A == B (right side of 'or' operator.
Even more sucinct, what about:
if(A == B && (!doesExist || B == C)) {
// do stuff
}
In this case, the value of B == C only affects the overall value of the predicate when doesExist is True.
if (A == B && (!doesExist || B == C))
You can just go with
if(doesExist ? (A == B && B == C) : (A == B))
If the A == B condition will change, then we need update it in two places in suggested solutions, I think better would be to always test A == B and conditionally test second one like:
if(A == B && (!test || B == C))
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);
After the above code is executed, the print result is 2, not 3, why?
I find "i" was 0 not 1 after "b = (t || ((i++) == 0))" executed by debuging.Well,I'm confused why "i++" not changes "i".
Well,I'm confused why "i++" not changes "i".
Because i++ doesn't execute in the code you've provided.
In an expression of the form a || b, first a is evaluated, and if it's true (which it is in this case), the expression b isn't evaluated. This is known as short-circuiting.
This is described in JLS section 15.24.
If you change the code to use the non-short-circuited | operator instead, like this:
b = (t | ((i++) == 0));
... then it will evaluate both operands regardless.
I'm just debugging some code and found some expression:
if (mObject != null && a || b )
{
mObject.someMethod();
}
which had a NullPointerException in the mObject.someMethod() line - so my question is:
how is the expression evaluated?
1. a && b || c == a && (b || c)
or
2. a && b || c == (a && b) || c
If 1. is the case i'd probably have to look at some multithreading issues.
This is probably a dumb question and i'm pretty sure it has been asked before, but i can't find the answer here. Thanks for the answers in advance.
It is equivalent to the following condition:
if ((mObject != null && a) || b )
&& has a higher precedence that ||.
If mObject is null, the condition may pass if b is true.
!= has precedence over && which has precedence over || (Source)
Therefore
if (mObject != null && a || b )
is equivalent to
if (((mObject != null) && a) || b )
To avoid the exception, use parentheses :
if (mObject != null && (a || b) )
According to here, it's:
((( mObject != null ) && a ) || b )
so you should be looking into b, because:
(null != null) is false
false && a is always false
everything depends on b then.
If parentheses are not provided then it will be always evaluated from left to right of the equals expression.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have three if statements:
if (!(a == 0 && b == 0)) {...}
if (!(a == 0 && b != 0)) {...}
if (!(a != 0 && b != 0)) {...}
I would like to combine them in one code block such as a method.
I don't want the other statements to run if one has run. There are workarounds if I want to avoid coming up with some good logic but I'd like to know if there is a beautiful way to write that.
if (!(a == 0 && b b == 0)) {...}
truth table
a b r
z n T
n n T
z z F
n z T
for
if (!(a == 0 && b b != 0)) {...}
truth table
z n F
n n F
z z T
n z T
for
if (!(a != 0 && b b != 0)) {...}
truth table
z n T
n n F
z z T
n z T
common case
n z T
so result that works on all 3 condition is
a != 0 && b == 0
note that: all 3 conditions are totally different, this will only work if you care to execute if
(first && second && third) is true
validate all case by yourself
(z = zero, n = non zero, a, b variables, r = result, T = true, F = false)
Simplest way to express this:
public int foo(boolean a, boolean b) {
int result;
if (a) {
if (b) { result = 1; }
else { result = 2; }
else {
if (b) { result = 3; }
else { result = 4; }
return result;
}
(notice that this evaluates a and b once each, which is the minimum you can expect here)
To keep #DavidWallace happy, here's a translation to the particular form of the original question:
public int foo(int a, int b) {
int result;
if (a==0) {
if (b==0) { result = 1; } // or whatever should happen, not specified in the question
else { result = 2; }
else {
if (b==0) { result = 3; }
else { result = 4; }
return result;
}
Just to re-word your question. You say that if one block runs, the others shouldn't. So when you wrote
if (!(a == 0 && b == 0)) {...}
if (!(a == 0 && b != 0)) {...}
if (!(a != 0 && b != 0)) {...}
You actually meant
if (!(a == 0 && b == 0)) {
// first ...
}
else if (!(a == 0 && b != 0)) {
// second ...
}
else if (!(a != 0 && b != 0)) {
// third ...
}
But this is equivalent to
if (a != 0 || b != 0) {
// first ...
}
else if (b == 0) {
// second ...
}
Notice that the third ... can never run, as I stated in my comment.
please ignore the question - its wrong
I am not sure if my question is issue is related to operator precedence- Just to rule out that I added additional bracket. My understanding is in that case that code in each bracket will be executed. So basically all the OR operation will happen and its output would be AND'ed to condition a.
I have below set of parameters a = true and c = 254 , b is not availble ( b is initialized to 0 -At any given time either b or c only is availble) . So for the above condition I am expecting if condition to result in true but it's resulting in false condition. Any reason why ? Also what is best way to debug such things as in where exactly condition is going wrong - any pointers
if ((a == true) && ((b == 460) || (b == 454) || (b == 455) ||
(c> 13568 && c< 14335) ||
(c> 10640 && c< 10655) ||
(c> 11296 && c< 11311) ||
(c> 25600 && c< 26111) || (c== 7825)))
First a is evaluated, if (a == true) evaluated to true, then only it will execute next && statement
((b == 460) || (b == 454) || (b == 455) ||
(c> 13568 && c< 14335) ||
(c> 10640 && c< 10655) ||
(c> 11296 && c< 11311) ||
(c> 25600 && c< 26111) || (c== 7825))
Inside this, it will check for any one condition which is true, and once it encounter any one statement true, it return from there.
For your condition to be true, a must be true, and in addition, at least one of the conditions on b or c must be true.
Therefore, if a==true and c==254, you will get false, since c is not within any of the ranges you allow, and, as you said, b is not available (which I'm assuming means it doesn't have one of the 3 values you allow).
It would be much simpler if the code is written in a more readable manner;
bool isEqualToAny(int valueToCheck, int[] listToCheckIn){
boolean isMatch = false;
(for item in listToCheckIn){
if (item == valueToCheck){
isMatch = true;
break;
}
}
}
bool isWithinRange(int valueToCheck, int min, int max){
return (valueToCheck > min && valueToCheck < max);
}
if ((a == true)
&& (isEqualToAny(b, int[]{460,454,455})
|| isWithinRange(c,3568,14335)
|| isWithinRange(c,10640,10655)
|| isWithinRange(c,11296,11311)
|| isWithinRange(c,25600,26111)
|| isWithinRange(c,10640,10655)
|| (c== 7825)))
In java8 you can use an array of Tuples to make #isWithinRange more like #isEqualToAny