How does Java evaluate this statement? - java

I have this expression in the if else statement
if (row % 2 == 0 && col % 2 == 0 || row % 2 == 1 && col % 2 == 1) {
return 0;
}
else {
return 1;
}
which behaves as intended, returning 0 when the row and column are either both even or both odd. What perplexes me is how Java didn't read it as
(row % 2 == 0 && (col % 2 == 0 || row % 2 == 1 && col % 2 == 1))
How exactly does Java evaluate a statement without parenthesis?

How exactly does Java read a statement without parenthesis?
By applying the operator precedence rules. The Java language specification says that && has a higher precedence than ||, and that means that:
row % 2 == 0 && col % 2 == 0 || row % 2 == 1 && col % 2 == 1
is equivalent to:
(row % 2 == 0 && col % 2 == 0) || (row % 2 == 1 && col % 2 == 1)
And in fact, == has a higher precedence than &&, so that is equivalent to:
((row % 2 == 0) && (col % 2 == 0)) || ((row % 2 == 1) && (col % 2 == 1))
This page from the Java Tutorial provides more information on operator precedence and how it affects expression evaluation.
For what it is worth, there is a natural parallel between operator precedence in a programming language and the meaning of simple arithmetical expressions ... as you were taught in primary school. For example, you were taught that 1 + 2 x 3 means the same thing as 1 + (2 x 3); i.e. the answer is 7 and not 9. In technical terms, the x (multiplication) operator has higher precedence than the + (addition) operator.
The idea is the same in a typical programming language, except that there is a larger range of operators to deal with ... and the rules are (generally speaking) more precisely specified1.
1 - For instance, since the evaluation of primaries and operators can in some cases have side effects, it can be important to know the precise order in which the sub expressions get evaluated.

That is because of the precedence order. && has precedence over ||.
Look here.

Related

Condition for while loop doesn't work in java

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)

Java difference between || and && in this example

I am making a counter between number ranges and not sure the correct way to do this. I have always used the || operator but reading some examples, I feel I should be using the && command. Here is my example problem...
if(value >= 1 || value <=10){
count1++;
}
else if(value >= 11 || value <= 20){
count2++;
// AND SO ON........
Or should I be using the && operator like
if(value >= 1 && value <= 10){
count1++;
}
else if value >= 11 && value <= 20){
count2++;
}
|| means "or".
&& means "and".
value >= 1 || value <= 10 makes no sense because it's always true. All numbers are 1 or more, or 10 or less. Some numbers are both, but that doesn't matter.
value >= 1 && value <= 10 makes far more sense. There's a limited range of numbers ([1..10]) for which both the first condition and the second condition are true.
|| is the or operator, so the condition value >= 1 || value <=10 is true for all values if you think about it. So, unless you want your counts to be meaningless, use && which is the and operator.

Simplify multiple similar statements on (while)

For example let's say i have this:
while( i1 % 1!=0 || i1 % 2!=0 || i1 % 11!=0 || i1 % 16!=0 ||
i1 % 7!=0 ||i1 % 3!=0 ||i1 % 12!=0 ||i1 % 17!=0 ||
i1 % 8!=0 ||i1 % 4!=0 ||i1 % 13!=0 ||i1 % 18!=0 ||
i1 % 9!=0 ||i1 % 5!=0 ||i1 % 14!=0 ||i1 % 19!=0 ||
i1 % 10!=0 ||i1 % 6!=0 ||i1 % 15!=0 ||i1 % 20!=0 ) {}
How can i simplify that?
Like while the module of i1 for 1-20 is different to 0. without all that mess.
Pardon if its a dumb question, I'm fairly new to this.
You can create a method that will generate the statement for you, and using a forloop to iterate 20 numbers and check if the modulo of each number is not equal to zero.
sample:
public boolean checkModulos(int toCheck)
{
for(int i = 0; i < 20; i++)
{
if(toCheck % i != 0)
return true;
}
return false;
}
to use it is:
while(checkModulos(i1))
You can work with the statement a bit. With DeMorgan's Law (!A || !B == !(A &&B)), you can turn it into !(i1 % 1 == 0 && i1 % 2 == 0 ... && i1 % 20 == 0)
Now think about the statement a bit. A number mod another equaling zero means the first number is divisible by the second. That means the statement really reads: not (i1 is divisible by all the numbers 1-20). So find a number n that is divisible by all the numbers 1-20, and have the loop become while( !(i1 % n == 0) ), or just while(i1 % n != 0)

Explain if (++value % 2 == 0 && ++count < limit) in java

public class AndOperator {
public static void main(String[] arg) {
int value = 8;
int count = 10;
int limit = 11;
if (++value % 2 == 0 && ++count < limit) {
System.out.println("here");
System.out.println(value);
System.out.println(count);
} else{
System.out.println("there");
System.out.println(value);
System.out.println(count);
}
}
}
i am getting output as
there
9
10
explain how count is 10....?
&& is short-circuit operator. It will only evaluate the 2nd expression if the 1st expression evaluates to true.
Since ++value % 2 == 0 is false, hence it doesn't evaluate the 2nd expression, and thus doesn't increment count.
++value = 9 so ++value % 2 == 0 is false so ++count < limit is not evaluated.
This is called Short circuit evaluation. See the wikipedia page : http://en.wikipedia.org/wiki/Short-circuit_evaluation
Since you are using &&(logical and) operator.
logical and evaluate second condition only if first condition is evaluated to true
Here in your code first condition ++value % 2 == 0 is evaluated to false,so second condition ++count < limit won't be evaluated.
If you want to execute ++count < limit also use &.
more information read Difference between & and &&
Because ++value % 2 == 0 is false, thus it won't return the first statement. The reason ++value % 2 is false is because value is incremented by one before the mod operator is evaluated. So ++value % 2 is 9 % 2 which != 0.

simple boolean question

What am I doing wrong here?
I am wanting to display integers from 1-100 who are divisible by either 6 or 7. That's done and working. The next step is to not display any that are divisible by both...that isn't working in my loop (those integers are still displaying)
for (int i = 1; i < 100; i++)
if (i % 6 == 0 || i % 7 == 0 && i % (6 * 7) != 0){
println(i);
}
Thanks!
Joel
try making your condition more explicit by adding (...), like so:
if (((i % 6 == 0 || i % 7 == 0) && (i % (6 * 7) != 0)) {
}
by default && takes precedence over ||
Missing parentheses:
for (int i = 1; i < 100; i++) {
if ((i % 6 == 0 || i % 7 == 0) && i % (6 * 7) != 0) {
println(i);
}
}
You could also use the exclusive or
for (int i = 1; i < 100; i++) {
if ((i % 6 == 0) ^ (i % 7 == 0)) {
println(i);
}
}
or just an unequal if ((i % 6 == 0) != (i % 7 == 0))
Since exclusive or is not used that often, I doubt this will increase the readability of the code...
I would simply stop worrying about how to evaluate precedence, and use something like:
for (int i = 1; i <= 100; i++) {
if ((i % 42) == 0) continue;
if ((i % 6) == 0) println (i);
if ((i % 7) == 0) println (i);
}
I'm assuming here that 1-100 was an inclusive range in which case you should use <= rather than <. It won't matter for your specific case since 100 is divisible by neither 6 nor 7.
Guess what? Any decent optimising compiler (including JIT ones) will probably end up generating the same code as for all the other possibilities. And, even if it didn't, it wouldn't matter unless you were calling this function a great many times.
I think that's a tiny bit more readable than:
if (i % 6 == 0 || i % 7 == 0 && i % (6 * 7) != 0) ...
or, worse yet, the Lisp-like thing you'll have to turn it into to get it working properly :-)
Keep in mind one possibility - you can change your loop to make it more efficient (sevenfold), for the specific case with 6 and 7, thus:
for (int i = 7; i <= 100; i += 7)
if ((i % 6) != 0)
println (i);
This uses the for loop itself to only check multiples of 7 and print them if they're not also multiples of 6.
for (int i = 1; i < 100; i++)
if ((i % 6 == 0 || i % 7 == 0) && !(i % 6 == 0 && i % 7 == 0)){
println(i);
}

Categories

Resources