tags:

views:

148

answers:

3

Possible Duplicates:
Incrementing in C++ - When to use x++ or ++x?

What is the difference between

for (int i = 0; i < MAX; i++)
{
//...do something
}

and

for (int i = 0; i < MAX; ++i)
{
//...do something
}

?

+2  A: 

Nothing at all. The increment is a lone statement, so whether it is pre-incremented or post-incremented doesn't matter.

zdav
A: 

It only matters if the optimizer isn't clever enough to realize that it can do ++i even though you specified i++. (Which is not very likely in modern compilers.)

You can recognize really old programmers because they always use ++i unless they need to use i++, because once apon a time compilers were a lot less clever.

John Knoeller
Not really to do with age, more to do with exposure to non-trivial iterators in C++.
Steve Jessop
I must be a really old programmer then, because I like to write my code so the results are intentional, not accidental.
Jon Reid
A: 

The post- and pre- increment operators matter mainly if you care about the value of some variable in a compound statement. Standalone increment statements, as the third clause of the for loop is, aren't impacted by your choice of pre or post.

int j = i++; and int j = ++i; are very different. Do you want the current value of i or do you want the incremented value of i? In the for loop example, you don't care so long as you increment.

Jonathon