• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do you use ++i or i++ in for loops?  RSS feed

 
Pete Letkeman
Ranch Foreman
Posts: 917
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These two lines are effectively the same:

I know this, however I usually use option A. It just seems more correct to me.
I'm sure that everyone has their own little quirks about the code they write, which don't really change the ending result.

How do you like to increment your simple for loop counter?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option A because it's the convention. That is, when I used that type of for statement. 99% of the time I end up using the non-indexed form of for.
 
O Shea
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Option B
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Option A because it's the convention.


Absolutely. If I was given code and it used Option B, I would have to stop and think for a few seconds about why Option A wasn't used. And when I'm looking at somebody else's code, there's enough real things to think about. Don't make me think about irrelevant trivia!
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O Shea wrote:I think Option B

Any reasoning?
 
O Shea
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Any reasoning?

Yes, just for fun to try something different.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer to use option A because I'm used to it and it somehow looks more visually pleasing to me. However, I will use option B if that's consistent with the rest of the project.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O Shea wrote:[. . . to try something different.
As long as you don't expect anybody else to read it, that is. There are reasons for following conventions which Paul C has already told you about.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Convention is my main consideration in using post-increment. However, I do seem to recall reading somewhere that in JavaScript, the pre-increment can result in slightly better performance. Don't use this as an excuse to do premature optimization though. Code for clarity first, then optimize if there are problems. And always base optimization decisions on your own quantified performance testing results, not on gut feeling or word of mouth.
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first form is more commonly preferred, but aside from that the two statements are only identical in effect for this particular case. One is a post-increment, the other is a pre-increment.

Consider the following:That will assign q[0] to z[0], q[2] to z[1], q[4] to z[2]...

Contrast with:

That will assign q[2] to z[0], q[4] to z[1], q[6] to z[2]...

Or, more commonly, a countdown loop:
That would count 5, 4, 3, 2, 1.

Versus:
Which would be 4, 3, 2, 1.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:

I believe that should be and also the other for loop.
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Tim Holloway wrote:

I believe that should be and also the other for loop.


I spent so much time trying not to screw up my calculations, I missed the fact that I got the order wrong. Typical.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:I spent so much time trying not to screw up my calculations...


This is why I (personally) try to avoid using either i++ or ++i. As I said, I'll use it in the canonical for-loop because that's a well-known idiom, but otherwise I'll avoid it. Even to the point of writing



instead. Fortunately I never have to write code like Tim's examples, and even if I did I wouldn't be looking for code like Tim's. No doubt people brought up on C coding would have no problem at all with that code, but I wasn't ever a C programmer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!