• 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
Just answered this post in Intermediate ...
Please don't post into multiple forums!!
Manfred.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per Bruce Eckel
Unary operators ++ -- += have higher precedence over arithmetic
Arithmetic operators go in
* / % + -
now let us take it step by step
Since unary operators has precedence over arithmetic the first to execute will be --b(note a++ will not execute as value of a will be incremented only after the expression is evaluated)
so b = 3
next b*c will be executed which is 3*5 = 15
after that division 15/d = 15/6 = 2.5. Since it is integer it truncates and hence it gets a value of 2
at this point expression is a++ - 2 + e
which is easy to solve. Mind it a will be 3 while calculating and then it will be incremented.


[This message has been edited by Anshul Manisha (edited June 15, 2001).]
[This message has been edited by Anshul Manisha (edited June 15, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic