• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is the for loop prefix not taken into consideration?

 
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

https://coderanch.com/how-to/java/PostIncrementOperatorAndAssignment might be of interest.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benjamin Scabbia wrote:Great answer thanks for helping me out!


You're most welcome. Glad it helped.

Winston
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic