• Post Reply Bookmark Topic Watch Topic
  • New Topic

Chapter 2 - Increment & Decrements Operators - Error in Example (OCA 8, Sybex)  RSS feed

 
Vinoth Kumar Thirunavukkarasu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Increment & Decrements Operators

int x= 3;
int y = ++x * 5 / x-- + --x

The first place, variable x need to be modified should be x-- (Post-unary) as this is the first in sequence of operators instead of ++x, though the result is same 7

So it should be

int y = ++x * 5 / 3 + --x
int y = 3 * 5 / 3 + 2
int y = 15 / 3 + 2
int y = 5 + 2
int y = 7

Please confirm.

Thanks
Vinoth Kumar
 
Vinoth Kumar Thirunavukkarasu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did figure out there are couple of posts having nice conversation about this issue.

Will do it practically and correct myself
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36656
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth,
Welcome to CodeRanch!

Glad you found the other posts. When you make more posts about the book, please indicate which book you are referring to and the page # (page 59 in this case). Helps in answering questions.
 
Scott Selikoff
author
Bartender
Posts: 4085
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OCA book (p59) includes a line by line derivation of this. The pre- and post- operators should be evaluated from left to right in this example, as they are separated by lower precedent operators.

Perhaps this discussion will help you understand this:
http://www.coderanch.com/t/411365/java/java/Post-increment-Pre-Increment-Precedence
 
Roel De Nijs
Sheriff
Posts: 11082
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinoth Kumar Thirunavukkarasu,

First of all, a warm welcome to CodeRanch!

Vinoth Kumar Thirunavukkarasu wrote:I did figure out there are couple of posts having nice conversation about this issue.

You are probably referring to this topic and this one. If not, you should definitely read them as well as both topics provide excellent explanations (with illustrative code snippets) about the same code snippet. And if you want to learn more about the order of evaluation of (post-)increment (& decrement) operators, this topic is also definitely worth reading.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!