Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Post and pre-increment operaor question on page 59 (Sybex)

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am working through the post and pre-increments/decrements and I got a different result than the book is showing.
I then entered the code into Eclipse and the output also is different from the example.

Code on page 59:
int x = 3;
int y = ++x * 5 / x-- + --x;
x is 2
y is 7

My results using pencil and paper are:
x is 3
y is 9

Eclipse's results are:
x is 4
y is 9

My questions are: How is x equal to 4? Does it take the result of ++x? I have been updating my OCA book based on the table of errors located here: https://www.selikoff.net/java-oca-8-programmer-i-study-guide/. Will you continue to update this table or should I continue to post questions here to receive an answer?

Thanks!!!

 
AngelinaJeana Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, please forgive me. I looked at the code again and noticed that I entered it incorrectly! I fully understand why the results are x = 2, y = 7. Thanks! So sorry!
 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you still have doubts about operators precedence, this post may help you. It contains links to great topics.
 
AngelinaJeana Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic