Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

operator precedence

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

this is my first thread..., please correct me if anything is wrong in this thread.

book- oca java se 8 programmer 1 study guide exam 1zo-808
chapter -2
page number - 59
example-


in book it is given that , first ++x will be calculated. But as per operator precedence table , the order of operator precedence in decreasing order is->  post unary operators(exp++, exp--) then  pre unary oparators(++exp, --exp) ...
So evaluation of expression in line number 2 should be like this-


output =
x is 2
y is 7

please tell me, should i consider orator precedence order or just evaluate from left to right ?


 
Stephan van Hulst
Bartender
Posts: 6587
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Operator precedence has nothing to do with order of evaluation. Operator precedence determines which operands bind to which operators. You can picture this as the compiler making operations explicit by placing parentheses around them:

ExpressionAfter applying operator precedence
++x * 5 / x-- + --x((((++x) * 5) / (x--)) + (--x))

After you have applied operator precedence, you then evaluate the resulting expression from left to right.
 
Campbell Ritchie
Marshal
Posts: 52615
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to yoru post; i think it makes the code look better Notice that the code tags provide their own line numbers.
for all the details you provided, which make your question so much easier to answer and also for providing an exact source complete with page number.

Yes, x++ has the highest precedence, but the left‑to‑right rule takes precedence over operator precedences. I think you know that already. Let's see if we can't get 7 out of your expression without using Stephan's ().
++x * 5 / x-- + --x … and the initial value of x is 3.
At the left we find ++x and ++ has a higher precedence than * so evaluate ++x: 4
Then we find * 5 which has the same precedence as the following / but associates to the left. So it behaves as if the left operator had a higher precedence than the right operator and we evaluate the * first.
x * 5: 20
Then we find / y and the / has a lower precedence than -- so we evaluate the x-- first but as we know that returns the old value: 4
So we now have
20 / 4: 5
Then we have the + operator but the operator to its right has a higher precedence --, so we evaluate
5 + --x
and evalute the --x first. Since x is 3 which you already know from the x--, --x returns 2
5 + 2: 7
As Homer Simpson would say: Woohoo!

Fortunately you will probably only ever see expressions like that in Cert Exams.
 
Campbell Ritchie
Marshal
Posts: 52615
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming x is 6, I have made a tiny change to your calculations:-Notice we managed to calculate 7 by both techniques in the original expression.

Now let's try again with the left‑to‑right method, copying from my previous post:-
++x * 5 / x-- + --x … and the initial value of x is 6.
At the left we find ++x and ++ has a higher precedence than * so evaluate ++x: 7.
Then we find * 5 which has the same precedence as the following / but associates to the left. So it behaves as if the left operator had a higher precedence than the right operator and we evaluate the * first.
7 * 5: 35
Then we find / y and the / has a lower precedence than -- so we evaluate the x-- first but as we know that returns the old value: 7
So we now have
35 / 7: 5
Then we have the + operator but the operator to its right has a higher precedence --, so we evaluate
5 + --x
and evaluate the --x first. Since x is 6 which you already know from the x--, --x returns 5
5 + 5: 10
 
nilesh padmagiriwar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan and Campbell

It helped a lot...
now i understood difference between operator precedence order & evaluation order

And,
x = 6;
System.out.println(++x *5 / x-- + --x); // prints 10

Regards,
Nilesh
 
Campbell Ritchie
Marshal
Posts: 52615
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nilesh padmagiriwar wrote:Thanks Stephan and Campbell . . .
That's a pleasure. I am quite relieved you got 10, because I made lots of mistakes writing that calculation
 
prateek shaw
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes correct , but good explanation
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!