I want the loop to repeat if the number is not equal to 0 and rest%2 is equal to 1 or -1. But this does not seem to work:
while (number != 0 && rest%2 == 1 || rest%2 == -1)
How do I have to write the code so it works?
While it's good to learn about operator precedence, your expression can be reduced:
while (number != 0 && rest%2 != 0)
Put another way, n % 2 is 0 for positive and negative even numbers and something not even must be odd (which is what you are testing).
This is the correct way:
while (number != 0 && (rest%2 == 1 || rest%2 == -1))
Have a look at Java operator precedence here https://introcs.cs.princeton.edu/java/11precedence/
If I understand correctly you want to enter the loop in two cases:
number != 0 && rest%2 == 1
OR
rest%2 == -1
If that's true consider using parentheses:
while ((number != 0 && rest%2 == 1) || rest%2 == -1)
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.
This question already has answers here:
In Java, what are the boolean "order of operations"?
(6 answers)
Closed 7 years ago.
I know that using And (&&) and OR (||) in a condition statement shouldn't be used without parentheses.
So if you should use both conditions you should do (A && !B) || (C && D)
However, in some code I saw that they are not using Parentheses? What would happen then? I thought that didn't compile:
A && !B || C && D
I guess that it would resolve as with SUMS or MULTIPLICATIONS, I mean, resolve them as they are read:
(((A && !B) || C) && D)
And (&&) has precedence over or (||) in the order of operations. So, this
A && !B || C && D
Is entirely equivalent to
(A && !B) || (C && D)
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.
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