| Author |
post/pre-increment/decrement A.K.A. ++ - -
|
colton peterson
Ranch Hand
Joined: Nov 18, 2007
Posts: 97
|
|
what is the difference between i++ and ++i? if i = 1; then wouldn't the both make I equal 2?
|
www.mormon.org
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26496
|
|
Yes and if you have them as a command by themselves, they work the same. If you have them in a command that also does something else, you get different results because they increment/decrement at different times. Try running the following code: They give different results because they increment at different times.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
colton peterson
Ranch Hand
Joined: Nov 18, 2007
Posts: 97
|
|
it said 1 2 process complete so i++ returns the value and then increments and ++i increments the value then returns right?
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26496
|
|
Originally posted by colton peterson: so i++ returns the value and then increments and ++i increments the value then returns
Right!
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by colton peterson: so i++ returns the value and then increments and ++i increments the value then returns
Not exactly. Both actually increment the value of i *first*, and then return. The difference is that i++ returns the value i had before the increment, ++i returns the value i has after the increment. That is why i = i++; doesn't change the value of i at all. Or more precisely, it first increments the value of i, and then sets it back to the old value.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
colton peterson
Ranch Hand
Joined: Nov 18, 2007
Posts: 97
|
|
|
Thanks!
|
 |
 |
|
|
subject: post/pre-increment/decrement A.K.A. ++ - -
|
|
|