Java - Having AND and OR in the same condition, without parentheses [duplicate] - java

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)

Related

Or operand not working in while loop [duplicate]

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 4 years ago.
I have this loop statement, which I'll express using C-like syntax (C, C++, Java, JavaScript, PHP, etc. all use similar syntax):
while (c != 'o' || c != 'x') {
c = getANewValue();
}
I want it to run until I get a 'o' or 'x', but it never exits, even when c is 'o' or 'x'. Why not?
I've also tried using if:
if (c != 'o' || c != 'x') {
// Show an error saying it must be either 'o' or 'x'
}
but that also always shows the error message, even when c is 'o' or 'x'. Why?
The condition (c != 'o' || c != 'x') can never be false. If c is 'o', then c != 'x' will be true and the OR condition is satisfied. If c is 'x', then c != 'o' will be true and the OR condition is satisfied.
You want && (AND), not || (OR):
while (c != 'o' && c != 'x') {
// ...
}
"While c is NOT 'o' AND c is NOT `'x'..." (e.g., it's neither of them).
Or use De Morgan's law, covered here:
while (!(c == 'o' || c == 'x')) {
// ...
}
"While it's NOT true that (c is 'o' or c is 'x')..."
It must be if(c!='o' && c!='x') instead of if(c!='o' || c!='x'). If you use the or operator the boolean expression will be always true.
Why is my c != 'o' || c != 'x' condition always true?
The expression combines two sub-expressions using the logical OR operator (||). The expression is true if at least one of the sub-expressions is true. In order to be false, both sub-expressions it connects must be false.
The sub-expressions are c != 'o' and c != 'x'.
The first sub-expression c != 'o' is false when c == 'o'. The same for the second one; it is false when c == 'x'.
Please note that they cannot be false on the same time because c cannot be 'o' and 'x' on the same time.
The condition should be if(!(c=='o' || c=='x')) or if(c!='o' && c!='x')
even when you enter x or you enter o in that case if condition evaluates to true and hence game_start remains false.
it should be if(c!='o' && c!='x')
or use a more straight forward way
if(c == 'o' || c == 'x')
game_start=true;
else
System.out.println("you can only enter o or x!");

Finding # of combinations of truth values

There is a question I have had a problem doing.
How many combinations of truth values for p, q, and r make this expression true?
(p && !q) || (q || !r)
I know that the answer is 7, but I don't know how they got the answer. I could simply test every combination (8 is max, 2^3), but is there a faster way I can do this? Can the expression be simplified?
This certainly does not need an exhaustive search. You can reason as follows:
Due to the || !r you know that the 4 combinations with r == false satisfy the expression
Due to the || q you know that of the remaining 4 combinations, the 2 with q == true satisfy the expression
Due to the (p && !q) you know that of the remaining 2 combinations both have q == false (because you've already considered the cases where q == true above) and so the 1 with p == true satisfies the expression
Adding those up you have 7 combinations satisfying the expression.
As for simplifying the expression, (p && !q) || q is equivalent to p || q. So the expression can be simplified to p || q || !r. That can also be expressed as !(!p && !q && r) which makes it obvious why there are 7 combinations: there's only one combination that does not satisfy the expression.
To answer the second question, "can it be simplified?": In Boolean algebra, the expression
a || (!a && b)
is equivalent to
a || b
Looking at the original expression:
(p && !q) || (q || !r)
note that || is associative (and so is &&), and therefore you can rearrange the parentheses:
((p && !q) || q) || !r
and the first part is a case of the equivalence at the top of my answer, thus
((p && !q) || q)
is equivalent to
p || q
(note that || and && are both commutative), so the whole expression is equivalent to
p || q || !r

Logic evaluation with && and || in a statement

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.

Boolean Logic in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have come across a problem in Boolean logic in which you must assign Boolean values to each of the three variables, therefore making each equation unequal to each other.
The problem specifies:
!b && (c || !d) != !(b || !c && d)
I've tried solving the problem with a guess and check method, but I haven't had any luck quite yet. Is there a way to solve the problem algebraically? I'm working in Java.
This is a bit of a trick question. Consider applying De Morgan's law to the expression on the left, after applying a double negation:
!!(!b && (c || !d))
!(b || !(c || !d))
!(b || !c && d)
In other words, there is no such combination because the two are logically equivalent.
A very simple solution to this kind of problem can often be checking the formular for what seems to be the most "powerful" value. Here I'd say that is b.
So let's go through the examples with b == true and b == false.
First though, let's add some more parenthesis so it's clear what is what:
!b && (c || !d) != !(b || !c && d)
= (!b && (c || !d)) != !(b || (!c && d))
If b == true then:
(!true && (c || !d)) != !(true || (!c && d))
=> (false && (c || !d)) != !(true || (!c && d))
=> false != !true
=> false != false
=> false
And if b == false then:
(!false && (c || !d)) != !(false || (!c && d))
=> (true && (c || !d)) != !(!c && d)
=> (c || !d) != (c || !d)
=> false
So this can never be fulfilled.
You can use some rules of boolean logic:
!(a || b) = !a && !b
!(a && b) = !a || !b
Apply them to the right hand side:
!(b || !c && d) = !b && !(!c && d) = !b && (c || !d)

operator precedence && and ||

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

Categories

Resources