• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

precedence...

 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 (&&)?
Please explain..
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think u are talking about the "ASSOCIATIVITY". But I am interested in "PRECEDENCE"?
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Annad,
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic