Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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

 
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
 
author & internet detective
Posts: 40245
819
Eclipse IDE VI Editor Java
  • 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.
 
author
Posts: 4226
33
jQuery Eclipse IDE Java
  • 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:
https://coderanch.com/t/411365/java/java/Post-increment-Pre-Increment-Precedence
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
 
Their achilles heel is the noogie! Give them noogies tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic