4

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
}

?

Community
  • 1
  • 1
WannaBeGeek
  • 3,906
  • 3
  • 17
  • 9

3 Answers3

5

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

zdav
  • 2,752
  • 17
  • 15
1

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
  • 33,512
  • 4
  • 61
  • 92
1

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 Faust
  • 12,396
  • 4
  • 50
  • 63