• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

operators precedence

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do we evaluate this expression?
If a=3 b=4 c=5 d=6 e=7
Then
What will be the values of?
a++ - --b*c/d+e
It shows me 8 but How unable to understand?
Please help any help will be appreciated...
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seema,
Here it is step by step.
Start: a++ - --b * c / d + e
Post fix and pre fix have highest priorities (left to right):
(3++) - --b * c / d + e // PostFix uses same number
3 - (--4) * c / d + e
3 - 3 * c / d + e
Multiplication and division are next (left to right):
3 - (3 * 5) / d + e
3 - (15 / 6) + e // Integral division because no floats or doubles
3 - 2 + 7
Addition and subtraction are next (left to right):
(3 - 2) + 7
1 + 7
Final result:
8
Regards,
Manfred.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Seema Bhatia:
How do we evaluate this expression?
a=3 b=4 c=5 d=6 e=7
What will be the values of?
a++ - --b*c/d+e
It shows me 8 but How unable to understand?


Seema
The first thing it does is fill in the values of all of the variables, so the expression would look something like this:
3++ - --4*5/6 + 7
then it evaluates each operand of all the operators:
3 - 3*5/6 + 7 (the only change here was the --4)
then it will perform the arithmetic according to the precedence rules and moving from left to right. So the first thing it does is the 3 * 5, then divides that result by 6. The result is 2.5, because the it is going to be used to evaluate another expression the .5 is truncated from it. then the rest of the original expression is finished. the result of 3-2 is added to 7. This gives the answer of 8.
I hope that helped
Dave
the precedence rules are listed here if that was the cause of your question... http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
I don't have a link on the part where the 2.5 is truncated, I'll take a look for it but if someone else has it I'm sure they'll post it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic