The difference between i ++ and ++ i in the for loop

Possible duplicates:
Increment in C ++ - When to use x ++ or ++ x?

What's the difference between

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

and

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

?

+5
source share
3 answers

Nothing. Increment is a single operator, so it does not matter whether it is pre-incremental or post-incremental.

+5
source

The post-and pre-increment statements are important mainly if you care about the value of a variable in a compound statement. Standalone increment instructions, since the third clause of the for loop is independent of your choice of pre or post.

int j = i++; int j = ++i; . i i? for , .

+2

This only matters if the optimizer is not smart enough to understand what it can do ++, even if you specified me ++. (Which is unlikely for modern compilers.)

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

+1
source

All Articles