• Post Reply Bookmark Topic Watch Topic
  • New Topic

for loop - why is i always returning 0  RSS feed

 
A. Aka
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I do not understand this code :



Why i is always returning 0, for me it should return 0, for the first iteration, then 1, etc... because we use post increment

Thanks,


 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you're using the for loop correctly? (Spoiler: you're not)
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Henry has correctly identified the problem.

Your non standard usage of the for loop is a matter of bad form rather than functional failure.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A. Aka wrote:Hi,
Why i is always returning 0, for me it should return 0, for the first iteration, then 1, etc... because we use post increment
Thanks,

Konnichiwa Aka-san. It looks like you have an infinite loop on your hands, always printing out the same thing since i is not changing at all.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:. . . Your non standard usage of the for loop is a matter of bad form rather than functional failure.
I would suggest you shou‍ld always think of a for loop like this:-
for (int i = 0; i < something; i++) ...
That is the basic form, but you will sometimes have to change it into a different form. But start like that. Using non‑standard forms of loops is error‑prone, but as Tim says, there are lots of people who sometimes write loops like this:-That is not incorrect code. Remember to put a comment in the empty space so readers know it is supposed to be empty.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A. Aka wrote:Hi,
I do not understand this code :
Why i is always returning 0, for me it should return 0, for the first iteration, then 1, etc... because we use post increment


But if you really want to get the code to work like that you can try to do the following:


 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:
But if you really want to get the code to work like that you can try to do the following:

Yeah, well... NO, don't do that.

That's like saying "If you really want to drive while you're drunk, just look out the driver's side of the car and make sure you can always see the middle line!"

Our advice is, "NEVER drive when you have been drinking! You can get hurt!"

There is NEVER a good reason to write code for a for-loop in either of those ways; it doesn't matter that the code you suggested appears to work. It's still just as bad as the code that doesn't work.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:... it doesn't matter that the code you suggested appears to work. It's still just as bad as the code that doesn't work.

Actually, I take that back. The bad code that appears to work is worse than the code that doesn't work. That's because in working, it doesn't make the writer seriously think about not doing it that way in the future, just as getting home safely while driving drunk and staying on the correct side of the center line by looking out the driver's side window doesn't really compel the offender to not drive while under the influence ever again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!