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
}
?
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
}
?
Nothing at all. The increment is a lone statement, so whether it is pre-incremented or post-incremented doesn't matter.
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.
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.