• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

page 99 OCA 8 Guide: Review Question 19: post-unary decrement

 
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I thought I was understanding post-unary increment/decrements, but apparently not.

code:


My doubt:
I'd understood from the explanation on the book on page 58 that "if the operator is placed after the operand, [...], then the original value of the expression is returned, with operator applied after the value is returned." The examples of page 59 consubstantiate this. Hence, I thought that in the first iteration while (9 > 1)  m would be 9, and 8 or 7 in the second or third loop, respectively.
But I'm wrong, and 've compiled and checked for each loop the value of m to be 8, 7 and 6, respectively.

What am I not grasping about post-unary operators?

 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please call x++ and x-- postfix operators, not post‑unary.
By the time you reach line 4, the new value of m is visible. That is 8, 7, 6.
 
Pedro Esgueira
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Campbell Ritchie wrote:By the time you reach line 4, the new value of m is visible. That is 8, 7, 6.


Ok, I see, so the post-increment operator becomes visible because the next line is a new expression (so: it's per expression, not per loop, that I need to think about this operator). Thanks
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The new value of the variable incremented by x++ is visible immediately after that expression. But code using more than one ++ or -- operator per statement is bad code which should only be seen in cert exams
reply
    Bookmark Topic Watch Topic
  • New Topic