• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Confused with Post increment operator

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

I have code like follows:


I thought at the end of for loop i will have the value 10.
But the value of i still 0 at the end of for loop. :s

Can you please explain why i value didnt change?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with the order in which things happen in the i = i++ statement.

1. The current value of i is taken in preparation for carrying out the i = assignment.
2. The i variable is incremented
3. The value from step 1 is assigned to i - replacing the incremented value

Bill

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

Thanks for quick reply

Yes i understood that the value got replaced with old one(i.e. Step 1).
But how as there are no assignment statements after increment..? :s

Let me put my understanding as follows:



first i got assigned (i=0), then its incremented, now its 1 which ideally should be available for next iteration.
But this is not happening in this case. :s

Satish Bejju
 
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, satish,

This question arises quite ofthen, so CodeRanch got tutorial (<- link) for that.

Your example:
The problem why you don't get your expected result is, that each iteration you reset "i" to 0.

If you were try this, you'd get expected result:

Probably have a look at the tutorial above, it is clearer there.
 
satish bejju
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Liutauras Vilda,

Thanks for the link it cleared my confusion.

The answer is operator precedence will come into the picture.
Thanks a lot

Regards,
Satish
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at our FAQ page: Post Increment Operator And Assignment
 
I've never won anything before. Not even a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic