This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

i++ vs ++i  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i++ and ++i make difference when combined with =
for example

Does that help?
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

...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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lei,
Why do you want to avoid ?
[ September 29, 2007: Message edited by: john abong ]
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
++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.
 
Marshal
Posts: 4455
284
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I expect Lei has gotten it sorted now since this topic is 10 years old.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!