wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes post/pre-increment/decrement A.K.A. ++ - - Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "post/pre-increment/decrement A.K.A. ++ - -" Watch "post/pre-increment/decrement A.K.A. ++ - -" New topic
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: 30085
    
149

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: 30085
    
149

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: post/pre-increment/decrement A.K.A. ++ - -
 
Similar Threads
media player applet
WA #1.....word association
Posting to bump up my count...
Text box value is not being completely filled with request attribute
The x900 effect ?