• Post Reply Bookmark Topic Watch Topic
  • New Topic

operator precedence  RSS feed

 
Kiruthika Dhanajeyan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


post increment(++) or pre increment operators have highest precedence..

for eg:
int x = 4;
int y = x * 2 + ++x;

now according to the precedence x should increment to 5 first and then x is reassigned so the answer would be y = 5 * 2 + 5(y=15)..

But the right solution is y = 4* 2 + 5(y=13)..


Why and How??

Thanks in advance...
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are confusing precedence with evaluation order. Just because you (and actually, the majority of people) instinctively evaluate based on precedence, that doesn't mean the computer does that. The JLS specifies how expressions are evaluated, and for the most part, it is done left to right.

Henry
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:The JLS specifies how expressions are evaluated, and for the most part, it is done left to right.Henry

Of course you are correct, Henry, but let's go further. In terms of evaluation order and precedence, how would you explain that this code outputs "7 7," rather than "9 7?"
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is evaluated from left to right. You take 1 and add something to it. Then you find that the operator to the right of the two has a higher precedence, so you add the result of that expression evaluated from left to right to 1 and Bob's your uncle: 7.
There is no inconsistency between precedences and left‑to‑right.

Kiruthika Dhanajeyan wrote:post increment(++) or pre increment operators have highest precedence..
Well, actually something like () behaves as if it had a higher precedence than the increment operators. The postfix operators have a higher precedence than the prefix operators, and prefix operators higher than multiplicative (* / %).
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

  • evaluates from left to right
  • x *
    4 *
  • Now checks is there any higher precedence operator than * then answer is yes i.e ++ so we get

  • 4 * 2 +   ( ++4 ) // ++ is evaluated first but x * 2 where value of x is already stored i.e 4

    4 * 2  +  ( 5 ) then

  • 4 * again checks is there any higher precedence operator than * then answer is no then

  • ( 4 * 2 ) + 5
    8 + 5

  • 8 + again checks is there any higher precedence operator than + then answer is no operator available then

  • 8 + 5  = 13 is the answer.

     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Same with
    Evaluates from left
  • 1 + checks any higher precedence operator than + so answer is yes i.e. * but we have in memory 1 + already so

  • 1 + kept as it is and * is evaluated so

  • 1 + ( 2 * 3 ) we get  1 + 6 then again checks any  higher precedence operator than + ? then answer is no, so

  • (1 + 6) executed y = 7

  • Same with  2 * 3 + 1


  • 2 *  any higher precedence operator to right side than *  ? answer is no then

  • ( 2 * 3) + 1 so we get 6 + 1 again check higher than +? answer is no then

    ( 6 + 1 ) = 7 is the answer.



     
    Kiruthika Dhanajeyan
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks all...especially patil....Crystal clear explanation..Thanks for your time......
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are welcome 
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!