0

I have a question about writing loops. I always begin with

(int i=0; i<10; i++)

But I see many experts begin with

(int i=0; i<10; ++i)

Is there there any real difference, or they are same?

Of course, I know the difference between pre-increment and post-increment. I mean which one I should use when writing the loop? or it depends.

Thanks!

Nonesuch
  • 23
  • 4

4 Answers4

5

There is no difference among the two. Pre increment and post increment are the only difference.

The difference between i++ and ++i is the value of the expression.

The value i++ is the value of i before the increment. The value of ++i is the value of i after the increment.

However in your loop it does not make any difference.

int i = 0;
00000088  xor         edx,edx 
0000008a  mov         dword ptr [ebp-40h],edx 
            i++;
0000008d  inc         dword ptr [ebp-40h] 
            ++i;
00000090  inc         dword ptr [ebp-40h] 

As you can see it does not make any difference in performance but in certain situations you might want to increment a number right after a sequence point or before it.

In C++ ++i is an l-value, but i++ is not.

Sadique
  • 22,572
  • 7
  • 65
  • 91
2

In C++ there is a difference. While the prefix operator will return a reference to the value, the postfix operator will return a copy and therefore is slower than the prefix operator.

Sven Painer
  • 194
  • 1
  • 10
  • Only if the type of `i` is user-defined and its `operator++` is not inlined. –  Nov 02 '13 at 21:32
  • For many primitive types any decent compiler will optimize away this perceived performance hit. For more complex types this could be true. – Joe Nov 02 '13 at 21:33
1

The main difference between pre- and post-incrementing lies in the return value.
Post-incrementing returns the value which has been present before the variable has been increased, essentially creating a copy of the old value. Pre-incrementing returns a reference to the new value, thereby avoiding the creation of a temporary variable.
Generally speaking it does not make a difference and is mostly a matter of taste, but in certain cases where memory is short the pre-increment option might be more useful. I also read that the post-increment version is supposed to be slower (as it creates a new temporary variable) but I have not found any conclusive proof of that

UnholySheep
  • 3,967
  • 4
  • 19
  • 24
0

Preincrement is generally preferred because while it makes no difference for primitive types it may be faster for iterators as preincrement doesn't have to return a copy of its old value.

Since preincrement is sometimes faster (with non primitive types like iterators) it is a good habit to get into even where it makes no performance difference which is why many people recommend it. This is item 28 in C++ Coding Standards for example.

mattnewport
  • 13,728
  • 2
  • 35
  • 39
  • In *this case*, what semantic difference is there? – user2864740 Nov 02 '13 at 21:25
  • There is no difference in this case but the question was why 'many experts' prefer preincrement and the answer is because it makes a difference in some cases and so it is a good habit to get into even for the cases where it doesn't. – mattnewport Nov 02 '13 at 21:29