Two Laptop Bag*
The moose likes Cattle Drive and the fly likes i++ vs ++i Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "i++ vs ++i" Watch "i++ vs ++i" New topic

i++ vs ++i

Lei Zhang

Joined: Jul 10, 2007
Posts: 29
while i was working on java1b, i had a for loop, something like this

for(int i = 0; i < 100; ++i)

I tried to make the first i equal to 1 to avoid int i = 1; But it seems i get the same result as if i do

for(int i = 0; i < 100; i++)

Katrina explained to me, it's because it is as if ++i or i++ are on their own line...
don't quite get it

Sorry guys, I can't put more code to make it more clear.
Thanks in advanced.
[ August 29, 2007: Message edited by: Lei Zhang ]

Hare & Tortoise
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 430
i++ and ++i make difference when combined with =
for example

Does that help?

To be or not to be. It's a question.
Pauline McNamara

Joined: Jan 19, 2001
Posts: 4012
Hi Lei, welcome along on the Cattle Drive.

I'm trying to understand your question a little better. It sounds like you wanted i to have the value of 1 at the start of your loop, so you used

for ( i = 0; i < 100; ++i )

instead of

for ( i = 0; i < 100; i++ ).

Do I understand that part correctly?

Like Louis said, the difference between i++ and ++i only shows up if you're combining that increment with another operation. In his example the other operation is an assignment.

I think what Katrina was saying is that in a for loop, you're not combining the increment with another operation, so you don't see the difference between i++ and ++i. (Please correct me, Katrina, if I'm off here!)

Each time through the loop, your code does something like this (here in pseudocode):

// first it checks if it should go through the loop again
if ( i < 100 )

// if so, it does whatever you want to do in the loop

// then it increments i
i++ ;

// after that, it would check again whether i is less than 100 and so on

The increment is "on it's own line" as Katrina said. It's not compounded with another operation, so it wouldn't make a difference if it was written i++ or ++i.

Remember what you read in the assignment page about loops? How the equivalent of a for loop can be written with a while loop like this:

Take a look at a simple "while loop":

The "for loop" shorthand for this is:

Maybe that makes sense?

You might want to play around with some code, since that's the most fun..., what happens when you write this?

int someNumber = 1 ;
System.out.println( "someNumber is " + someNumber ) ;
System.out.println( "someNumber is " + someNumber++ ) ;
System.out.println( "someNumber is " + ++someNumber ) ;

In the last two examples, the increment is compounded with printing out the value of someNumber.

Maybe take another look at the assignment page on loops, plus what the Style Guide says about compounding increments.

Enjoy the Cattle Drive ride!
Katrina Owen

Joined: Nov 03, 2006
Posts: 1357
Pauline, that was exactly the question being asked!

Thanks for the detailed explanation and great examples (taking note for next time the question comes up)
Lei Zhang

Joined: Jul 10, 2007
Posts: 29
Thanks for such detailed explaination, Louis and Pauline. Now I fully understand what's mean "in it's own" and differnce between i++ and ++i.

Thanks Katrina to clear up my first post
John Abong
Ranch Hand

Joined: May 14, 2007
Posts: 79
Why do you want to avoid ?
[ September 29, 2007: Message edited by: john abong ]
Katrina Owen

Joined: Nov 03, 2006
Posts: 1357
Actually, I think Lei was trying to avoid
<code>i = 0</code> by skipping directly to <code>i = 1</code>.

If I remember correctly, this turned out to be unnecessary for the assignment.
I agree. Here's the link:
subject: i++ vs ++i
Similar Threads
Multi dimensional array
operator precedence
Ternary Operator?
Having trouble with Math.random
a real noob