-1

I've been reading quite a few posts/questions about the micro-optimization of ++i and i++ in C++. And from what I've learnt is that ++i "can", not always, but can be faster than i++.

So this makes me ask this question, whats the point of i++ then? I thought that ++i is that you increment the value first then return it. Where as i++ you return the value then increment it. But I have made a very simple test on this:

for(int i = 0; i < 10; ++i)
{
    std::cout << i << std::endl;
}

is the same as:

for(int i = 0; i < 10; i++)
{
    std::cout << i << std::endl;
}

Both prints out the same results. So my real question is that is there a situation where you MUST use i++ rather than ++i? If there is, please explain. Thanks

Danny
  • 9,199
  • 16
  • 53
  • 75
  • the way you have used it in your example is arbitrary, it doesnt matter, where you have to choose one over the other is when you need to pre increment/decrement or post increment/decrement while(--len) { something }. and of course that has nothing to do with speed, speed is a completely separate topic. You have not really addressed speed in your question. – old_timer Aug 31 '13 at 14:08
  • In your examples you are ignoring (discarding) the results of `++i` and `i++` and only using their side effects. If you are discarding the result, then of course the two are equivalent. However, we don't always discard the results of expressions. The results of `++i` and `i++` are different, so in cases when you care about these results, you use whichever fits your purpose. – AnT stands with Russia Aug 31 '13 at 15:51

4 Answers4

1

Here's an example:

int i = 0;
while (i++ < 10) {
    std::cout << i << std::endl;
}

This will print the numbers 1 to 10. If you used ++i, it would print 1 to 9.

Of course, this is a trivial example since there are other ways to do this, but the point is that i++ will return the old value. For further clarification:

int i = 5;
std::cout << i++ << std::endl; // 5
std::cout << i << std::endl;   // 6

std::cout << ++i << std::endl; // 7
std::cout << i << std::endl;   // 7

In a for loop, there's no difference, since the increment is executed and then the condition is checked. The only difference between the two is the value of the actual expression.

tckmn
  • 57,719
  • 27
  • 114
  • 156
0

Sometimes you want to use the value first and then increment, e.g.

while (array[i++] != sentinel)
jev
  • 2,023
  • 1
  • 17
  • 26
0

Use i++ if you want to get (for example print) i before it is incremented, for example:

for(int i = 0; i < 10; i++)
{
    std::cout << "old i = " << i++ << std::endl;
    std::cout << "new i = " << i << std::endl;
}

You use ++i if you want to print already incremented i.

cpp
  • 3,743
  • 3
  • 24
  • 38
0

There shouldn't be a viable normal situation where you must use postfix or prefix version. Each version can yield the same functional effect.

The prefix operator can be faster but the compiler will perform the optimization anyway and will do it better than your manual attempt.

What differs mainly is the readability and simplicity of your code - see examples in other answers - there are situations when prefix/postfix is more convenient than the other version.

All of this is relevant to both C and C++. Since C++ allows you to overload operators, you can provide different functionality for each of these operators and in this case the above claims do not hold. Should be noted, however, that it's probably a very bad idea to write such code.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85