Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# precedence...

Anand Ko
Ranch Hand
Posts: 79
int i=0;boolean b;
b = (4 == 2 && ++i <= 1);
result : b is false.
here the first expn evaluates to false & so the second expn won't even be evaluated.
i have read earlier that all the operands are evaluated from left to right except for &&, ||, ?:.
Since, the relation operator which has higher precedence must be evaluated than conditional operator (&&)?

Gian Franco
blacksmith
Ranch Hand
Posts: 979
Hi,
I've read that the operator && is
evaluated from left to right.
The only operators that are evaluated
from right to left are:
++ -- (prefix increment, prefix decrement)
++ -- (postfix increment, postfix decrement)
? : (conditional)
= += -= *= /= %= (assignment)

Anand Ko
Ranch Hand
Posts: 79
I think u are talking about the "ASSOCIATIVITY". But I am interested in "PRECEDENCE"?

Vicken Karaoghlanian
Ranch Hand
Posts: 522
The '&&' operator is evaluated from Left-to-Right, see the code below.

int i=0;boolean b;
b = (4 == 2 && ++i <= 1);
result : b is false.

The order of procedence is as follows:
1) ++i = 1 --> b = (4 == 2 && 1 <= 1)
2) <= --> b = (4 == 2 && true)
3) == --> b = (false && true)
4) && --> b = (false)
Hope this helps.

venkatesh rajmendram
Ranch Hand
Posts: 130

int i=0;boolean b;
b = (4 == 2 && ++i <= 1);
result : b is false.
The order of procedence is as follows:
1) ++i = 1 --> b = (4 == 2 && 1 <= 1)
2) <= --> b = (4 == 2 && true)
3) == --> b = (false && true)
4) && --> b = (false)

Hi, The above procedure might not be right...
b = (4 == 2 && ++i <= 1);
Here we have short circuit && , so the right side is not even evaluated i.e i never gets incremented....so
b = (false && ++i <=1);
b = false;
You can display the value of i before and after the execution it will remain unchanged...
- Venkatesh

Vicken Karaoghlanian
Ranch Hand
Posts: 522
Originally posted by venkatesh rajmendram:

Hi, The above procedure might not be right...
b = (4 == 2 && ++i <= 1);
Here we have short circuit && , so the right side is not even evaluated i.e i never gets incremented....so
b = (false && ++i <=1);
b = false;
You can display the value of i before and after the execution it will remain unchanged...
- Venkatesh

hmmm, good catch venkatesh, you are correct with your assumption, the RHS is never evaluated, i should've seen that . But anyways thanks for correcting me.
Ranchers, please disregard my previous post.