Is there a difference in ++i
and i++
in a for
loop? Is it simply a syntax thing?
相关问题
- Why isn't mySet.erase(it++) undefined behavior
- Operator Precedence.. () and ++
- Operator Precedence.. () and ++
- Bash Post Increment
- Pre increment and post increment
相关文章
- C/C++ post-increment/-decrement and function call
- i++ vs. ++i in a JavaScript for loop
- Is the behavior of return x++; defined?
- What are the historical reasons C languages have p
- Difference between *ptr += 1 and *ptr++ in C
- Post Increment with respect to Sequence Points
- Why doesn't changing the pre to the post incre
- Post increment operator not incrementing in for lo
In javascript due to the following i++ may be better to use:
While arrays (I think all) and some other functions and calls use 0 as a starting point you would have to set i to -1 to make the loop work with the array when using ++i.
When using i++ the following value will use the increased value. You could say i++ is the way humans count, cause you can start with a 0.
Pre-increment ++i increments the value of i and evaluates to the new incremented value.
Post-increment i++ increments the value of i and evaluates to the original non-incremented value.
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:
https://web.archive.org/web/20170405054235/http://en.allexperts.com/q/C-1040/Increment-operators.htm
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.
As this code shows (see the dissambled MSIL in the comments), the C# 3 compiler makes no distinction between i++ and ++i in a for loop. If the value of i++ or ++i were being taken, there would definitely be a difference (this was compiled in Visutal Studio 2008 / Release Build):
The question is:
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:
Would translate to this code without using loops:
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.i++ ; ++i ; both are similar as they are not used in an expression.
There is no difference if you are not using the value after increment in the loop.
Both the loops will print 0123.
But the difference comes when you uses the value after increment/decrement in your loop as below:
Pre Increment Loop:
Output: 0 0 1 1 2 2 3 3
Post Increment Loop:
Output: 0 0 1 0 2 1 3 2
I hope the difference is clear by comparing the output. Point to note here is the increment/decrement is always performed at the end of the for loop and hence the results can be explained.