# i++ vs ++i

This topic is 4914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

whats the difference between i++ and ++i? i see it used alot in loops. what is the advantage of either method?

##### Share on other sites
++i is pre-incrementation, and i++ is post-incrementation. They basically mean, increment before the value is evaluated, or increment after the value is evaluated, respectively.

If you did:

int i = 1;
std::cout << ++i; // <-- This outputs 2
std::cout << i; // <-- This outputs 2

But if you did:

int i = 1;
std::cout << i++; // <-- This outputs 1
std::cout << i; // <-- This outputs 2

Generally, its more efficient to do ++i, because i++ requires storing an intermediate value, incrementing, and then returning. For good practice, you should do ++i whenever you can, unless your real intended meaning is i++. However, modern compilers will do optimizations that will filter-out useless i++ statements and turn them into the equivalent of ++i.

In the case of loops, more specifically for loops, it makes no difference, because the incrementation is always done after each cycle of the loop. So whether you post-increment or pre-increment. Its done at the same time and the variable takes on the same value.

##### Share on other sites
i++ increments the value and returns the old value. ++i increments the value and returns the new value. For integers there is generally no performance difference, for more complex classes that implement operator++(), such as many iterators, generally you should use the ++i form unless you need the old value.

##### Share on other sites
Quote:
 Original post by Max_Payne++i is pre-incrementation, while i++ is post-incrementation. They basically mean, increment before the value is evaluated, or increment after the value is evaluated, respectively.

To put it in more simple terms:

i = 3;x = i++;  //x is equal to 3, but i is now 4.j = 3;y = ++i;  //both y and j are equal to 4.

##### Share on other sites
If you can go either way, my vote goes to i++ because the gain in readability far outweights the infinitesimal gain in performance.

But SiCrane's note about ++i for iterators is good.

##### Share on other sites
that was quick! thanks alot for clearing that up for me.

damn i love this forum

##### Share on other sites
The part no one has mentioned is this ... the most common use of i++ is in situations like this:

for(int i = 0; i < length; i++)

this is just plain silly. when executed as a full stament (not inside a bigger expression or statement), the results of pre and post increment are identical ... the value gets incremented by the end of the stament ... but the theory of the i++ version says that an intermediate is created to return the old value. While it is totally true that this gets optimized away for all integral types on all modern compilers ... it DOES NOT get optimized away if i is a custom class ...

for if you have a template written like this

template <typename IteratorType>
void DoSomething(IteratorType begin, IteratorType end)
{
while(begin != end)
{
// Do Something Here

begin++; // this is just bad, it should be ++begin
}
}

// all three of these are for incrementing i
i = i + 1;
i += 1;
++i;

// this is for post incrementing i, ONLY used for cases where you need to cacluate based on the OLD value of i
i++

##### Share on other sites
++i; is supposed to give you a small speed improvement.

##### Share on other sites
If you don't use the intermediate value of 'var++', the compiler removes all the unnecessary code... even in the case of complex stuff like iterators.

This assumes you have a 'good' optimizer.

Yes, compilers ARE this smart. Assembly-level dependency graphing is freaking awesome.

Just remember, when you're looking at assembly, if you're using a debug build, you're probably not seeing optimized results.

##### Share on other sites
Quote:
 Original post by tgraupmann++i; is supposed to give you a small speed improvement.

When i is an iterator, i++ may be slower than ++i. Key words: iterator, may. It should provide no noticable difference when i is an integer, nor is it guaranteed to be faster even when i is an iterator (although it should not be slower).

As such, it is good to get in the habit of writing the increment of loops as ++i rather than i++:

for ( ... ; ... ; ++i )
rather than:
for ( ... ; ... ; i++ )
(the two statements are equivilant otherwise)

However, do not avoid i++ on the basis that "it will be slower" when it will make your code clearer.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015742
×