• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is the for loop prefix not taken into consideration?  RSS feed

 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I was looking at this for loop



It prints 0..9 - fine.

I then looked at this loop



It prints 0..9 - why? From what I understand ++i is a prefix, therefore by the time the for loop condition is checked, shoudln't i start with 1? So 1..9?

If I did this test in a print statement so:

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between ++i and i++ is only within that statement. The print statement is a separate statement, so it has no effect on it.

http://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment might be of interest.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Scabbia wrote:It prints 0..9 - why?

Because:is precisely equivalent to:apart from the fact that, in the second case, i is defined outside the loop.

Now, looking at that second loop, would it make any difference if it was ++i instead of i++?

HIH

Winston
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Benjamin Scabbia wrote:It prints 0..9 - why?

Because:is precisely equivalent to:apart from the fact that, in the second case, i is defined outside the loop.

Now, looking at that second loop, would it make any difference if it was ++i instead of i++?

HIH

Winston


Great answer thanks for helping me out!

Makes sense and of course, in the second loop using a prefix or postfix would make no difference
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Scabbia wrote:Great answer thanks for helping me out!

You're most welcome. Glad it helped.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!