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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Junilu Lacar
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Knute Snortum
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Frits Walraven
• Ganesh Patekar

# i++ vs ++i

Greenhorn
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.
[ August 29, 2007: Message edited by: Lei Zhang ]

Ranch Hand
Posts: 434
i++ and ++i make difference when combined with =
for example

Does that help?

Sheriff
Posts: 4012
6
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 )

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...

...like, 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!
Pauline

Sheriff
Posts: 1367
18
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
Greenhorn
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

Ranch Hand
Posts: 79
Lei,
Why do you want to avoid ?
[ September 29, 2007: Message edited by: john abong ]

Katrina Owen
Sheriff
Posts: 1367
18
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.

Greenhorn
Posts: 5
++i and i++ will not effect the program. post and prefix will end up the same except for one variable with be incremented by 1.

Sheriff
Posts: 4674
308
I expect Lei has gotten it sorted now since this topic is 10 years old.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?