# operator precedence

nilesh padmagiriwar

Greenhorn

Posts: 4

posted 7 months ago

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 ?

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

posted 7 months ago

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:

After you have applied operator precedence, you then evaluate the resulting expression from left to right.

Expression | After 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.

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

Campbell Ritchie

Marshal

Posts: 52615

119

posted 7 months ago

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,

At the left we find

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.

Then we find

So we now have

Then we have the + operator but the operator to its right has a higher precedence --, so we evaluate

and evalute the --x first. Since x is 3 which you already know from the

As Homer Simpson would say: Woohoo!

Fortunately you will probably only ever see expressions like that in Cert Exams.

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`: 4Then 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`: 20Then 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: 4So we now have

`20 / 4`: 5Then 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`: 7As Homer Simpson would say: Woohoo!

Fortunately you will probably only ever see expressions like that in Cert Exams.

Campbell Ritchie

Marshal

Posts: 52615

119

posted 7 months ago

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

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

Campbell Ritchie

Marshal

Posts: 52615

119