• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

possible mistake about increment/decrement (Java OCA 8 Programmer I Study Guide, Sybex)

 
Rubens Torquette
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I think I've found a possible mistake through the explanation about increment/decrement operators, covered in OCA SE 8 book – Chapter 2.

In the next example, the book states to begin to evaluate from left to right. The problem here is that we have a precedent statement (x--) and we should start by this one, and after that from left to the rigth.

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

If the initial value of x is 3 the result is the same, but, if you change this value to 5 it is possible to see the difference.

Am I right or wrong?

Regards.

Rubens.

 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rubens Torquette,

First of all, a warm welcome to CodeRanch!

Rubens Torquette wrote:I think I've found a possible mistake through the explanation about increment/decrement operators, covered in OCA SE 8 book – Chapter 2.

No, the study guide is spot-on! You should definitely read this topic and this one 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. And if you want to learn more about evaluating boolean expressions with short-circuit operators this topic and this one are must-read topics.

Hope it helps!
Kind regards,
Roel
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubens Torquette wrote:Am I right or wrong?

One way of finding out would be to change the types from int to double, and then check if the result is what you expect it to be. The trouble with ints in this case is that you also have integer division to deal with.

Winston
 
Rubens Torquette
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys by the help.

Indeed, the correct, in this case, is to use the left to right rule.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic