This question already has an answer here:
In programming, particularly in Java, what is the difference between:
int var = 0;
var++;
and
int var = 0;
++var;
What repercussions would this have on a for loop?
e.g.
for (int i = 0; i < 10; i++) {}
for (int i = 0; i < 10; ++i) {}
tldr;
Although both var++
and ++var
increment the variable they are applied to, the result returned by var++
is the value of the variable before incrementing, whereas the result returned by ++var
is the value of the variable after the increment is applied.
Further Explanation
When ++var
or var++
form a complete statement (as in your examples) there is no difference between the two. For example the following
int x = 6;
++x;
assert x == 7;
is identical to
int x = 6;
x++;
assert x == 7;
However, when ++var
or var++
are used as part of a larger statement, the two may not be equivalent. For example, the following assertion passes
int x = 6;
assert ++x == 7;
whereas this one fails
int x = 6;
assert x++ == 7;
Although both var++
and ++var
increment the variable they are applied to, the result returned by var++
is the value of the variable before incrementing, whereas the result returned by ++var
is the value of the variable after the increment is applied.
When used in a for
loop, there is no difference between the two because the incrementation of the variable does not form part of a larger statement. It may not appear this way, because there is other code on the same line of the source file. But if you look closely, you'll see there is a ;
immediately before the increment and nothing afterwards, so the increment operator does not form part of a larger statement.
++var
is the pre-increment operator; it increments the value of var
before evaluating the expression. Similarly, var++
is the post-increment operator; it increments the value of var
after evaluating the expression.
In the case of a simple loop, there is no difference between two, because the expressions ++var;
and var++;
both yield to the same result.
For more info, see for example http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/operators.html#IncDecOps.
int a = 5, b;
post increment : b = a++;
: a
is first transferred to b
and then a
is incremented, so now b
is 5
, and a
is 6
The effect is b = a; a = a + 1;
pre increment: b = ++a;
: first a
is incremented and then the result is transferred into b
, so now a
is 7
and also b
is 7
. The effect is a = a + 1; b = a
a++
and ++a
staying independently act in the similar way. In the loop examples you have presented, the increment operators is not associated in any expression, and are independent. Therefore these two in this particular implementation is identical.
In your examples, there is no difference, there is however a difference between:
int var = 0;
int var2 = ++var;
and:
int var = 0;
int var2 = var++;
In the first case, the value of var2 is 1 while in the second, it's 0.
var++ returns its value before incrementing.
++var returns its value after incrementing.
int var = 0;
System.out.println(var++); // returns 0;
var = 0;
System.out.println(++var); // returns 1
++i is preincrement, it is done before anything in the expression it appears.
i++ is postdecrement, it is done after anything in the expression it appears.
in the first loop, you'll run from 0 to 9. In the second, it will run from 1 to 9.
My advice: avoid both of them (exceptions may be i++; or the first loop). Too tricky to look for in the expression.
I passed a long day debugging
myArray[(i++)%16] = data
and trying to find why it did try to write to data[16] sometimes
In fact, this is rather simple. The preincrement ++i is executed first in the statement.
j = ++i +1;
is equivalent to
i = i+1;
j = i+1;
whereas the post increment i++ is executed at the end;
j = i++ +1;
is equivalent to
j = i+1;
i = i+1;
Both ++var
and var++
are identical when appear in expression alone.
This applies to your question because you have alone ++i
, i++
It difference only take place when you inline them:
int x = 0;
printf( "%d %d\n", ++x, x ); // 1 1
printf( "%d %d\n", x++, x ); // 1 2
How to remember?
When you see first the operator, then increment and later take value.
When you see first the variable, then take value and later increment.
So in first example you see equal values because:
you increment `x`, then access `x`, then access `x` again
So in second example you see differences because:
you access `x`, then increment `x`, then access `x` again
来源:https://stackoverflow.com/questions/6175316/the-difference-between-var-and-var