Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Postfixed increment operator in expressions

 
Ranch Hand
Posts: 40
1
Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have tried the following expression:

the result surprises me. In fact I expected 21, it seems to me that the postfix hasn't been valued. Any suggestion about it?

Thank you in advance.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pagano monello wrote:the result surprises me. In fact I expected 21, it seems to me that the postfix hasn't been valued. Any suggestion about it?


Because it's postfix, the value of a (10) is used first and then it's incremented with 1 (a=11) and then the result of the sum (20) is assigned to a. It's much more clear if you use another variable to store the sum like in this example

And this topic has a very informative discussion about post-increment (or post-decrement) operators. So if you have some doubts, it's definitely worth reading. And if you still have some doubts after reading that topic, just let us know by hitting the reply button

Hope it helps!
Kind regards,
Roel

PS. You are using the postfix increment operator in your example, but the title suggests the decrement operator. I updated the title accordingly, hope that's ok for you.
 
pagano monello
Ranch Hand
Posts: 40
1
Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,
thank you for your answer, I am gonna read the topic you suggested me; this thing is a little tricky.
Thank you also for having updated the title of the topic which was actually wrong.

pagano
 
I'm still in control here. LOOK at this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic