The pre-increment operator (++ i) and the post-increment operator (i ++) are both operators that assign the result of i + 1 to i, but I will briefly explain the difference.
Returns the value of the expression after incrementing, that is, i + 1.
i = 1;
j = ++i; //j is 2
Returns the value of the expression before incrementing, that is, the value of i.
i = 1;
j = i++; //j is 1
for (int i = 0; i < n; ++i)
for (int i = 0; i < n; i++)
Both are for loops that you often see, but if ++ i and i ++ appear independently like this and the value of the expression is not used, there is no difference in operation. Also, with recent compilers, there is no difference in execution speed depending on the optimization, so you can use the one you like.
Recommended Posts