Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
AngelinaJeana Brown
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!
 
João Victor Gomes
Ranch Hand
Posts: 110
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!