Difference between pre-increment and post-increment in a loop?
Is there a difference in ++i
and i++
in a for
loop? Is it simply a syntax thing?
a++ is known as postfix.
add 1 to a, returns the old value.
++a is known as prefix.
add 1 to a, returns the new value.
C#:
string[] items = {"a","b","c","d"};
int i = 0;
foreach (string item in items)
{
Console.WriteLine(++i);
}
Console.WriteLine("");
i = 0;
foreach (string item in items)
{
Console.WriteLine(i++);
}
Output:
1
2
3
4
0
1
2
3
foreach
and while
loops depend on which increment type you use. With for loops like below it makes no difference as you're not using the return value of i:
for (int i = 0; i < 5; i++) { Console.Write(i);}
Console.WriteLine("");
for (int i = 0; i < 5; ++i) { Console.Write(i); }
0 1 2 3 4
0 1 2 3 4
If the value as evaluated is used then the type of increment becomes significant:
int n = 0;
for (int i = 0; n < 5; n = i++) { }
Pre-increment ++i increments the value of i and evaluates to the new incremented value.
int i = 3;
int preIncrementResult = ++i;
Assert( preIncrementResult == 4 );
Assert( i == 4 );
Post-increment i++ increments the value of i and evaluates to the original non-incremented value.
int i = 3;
int postIncrementResult = i++;
Assert( postIncrementtResult == 3 );
Assert( i == 4 );
In C++, the pre-increment is usually preferred where you can use either.
This is because if you use post-increment, it can require the compiler to have to generate code that creates an extra temporary variable. This is because both the previous and new values of the variable being incremented need to be held somewhere because they may be needed elsewhere in the expression being evaluated.
So, in C++ at least, there can be a performance difference which guides your choice of which to use.
This is mainly only a problem when the variable being incremented is a user defined type with an overridden ++ operator. For primitive types (int, etc) there's no performance difference. But, it's worth sticking to the pre-increment operator as a guideline unless the post-increment operator is definitely what's required.
There's some more discussion here.
In C++ if you're using STL, then you may be using for loops with iterators. These mainly have overridden ++ operators, so sticking to pre-increment is a good idea. Compilers get smarter all the time though, and newer ones may be able to perform optimizations that mean there's no performance difference - especially if the type being incremented is defined inline in header file (as STL implementations often are) so that the compiler can see how the method is implemented and can then know what optimizations are safe to perform. Even so, it's probably still worth sticking to pre-increment because loops get executed lots of times and this means a small performance penalty could soon get amplified.
In other languages such as C# where the ++ operator can't be overloaded there is no performance difference. Used in a loop to advance the loop variable, the pre and post increment operators are equivalent.
Correction: overloading ++ in C# is allowed. It seems though, that compared to C++, in C# you cannot overload the pre and post versions independently. So, I would assume that if the result of calling ++ in C# is not assigned to a variable or used as part of a complex expression, then the compiler would reduce the pre and post versions of ++ down to code that performs equivalently.
In C# there is no difference when used in a for loop.
for (int i = 0; i < 10; i++) { Console.WriteLine(i); }
outputs the same thing as
for (int i = 0; i < 10; ++i) { Console.WriteLine(i); }
As others have pointed out, when used in general i++ and ++i have a subtle yet significant difference:
int i = 0;
Console.WriteLine(i++); // Prints 0
int j = 0;
Console.WriteLine(++j); // Prints 1
i++ reads the value of i then increments it.
++i increments the value of i then reads it.
The question is:
Is there a difference in ++i and i++ in a for loop?
The answer is: No.
Why does each and every other answer have to go into detailed explanations about pre and post incrementing when this is not even asked?
This for-loop:
for (int i = 0; // Initialization
i < 5; // Condition
i++) // Increment
{
Output(i);
}
Would translate to this code without using loops:
int i = 0; // Initialization
loopStart:
if (i < 5) // Condition
{
Output(i);
i++ or ++i; // Increment
goto loopStart;
}
Now does it matter if you put i++
or ++i
as increment here? No it does not as the return value of the increment operation is insignificant. i
will be incremented AFTER the code's execution that is inside the for loop body.
Since you ask about the difference in a loop, i guess you mean
for(int i=0; i<10; i++)
...;
In that case, you have no difference in most languages: The loop behaves the same regardless of whether you write i++
and ++i
. In C++, you can write your own versions of the ++ operators, and you can define separate meanings for them, if the i
is of a user defined type (your own class, for example).
The reason why it doesn't matter above is because you don't use the value of i++
. Another thing is when you do
for(int i=0, a = 0; i<10; a = i++)
...;
Now, there is a difference, because as others point out, i++
means increment, but evaluate to the previous value, but ++i
means increment, but evaluate to i
(thus it would evaluate to the new value). In the above case, a
is assigned the previous value of i, while i is incremented.