Var 与 Var + + 的区别

在编程方面,尤其是在 Java 中,以下两者的区别是什么:

int var = 0;
var++;

还有

int var = 0;
++var;

这对 for 循环有什么影响?

例如:。

for (int i = 0; i < 10; i++) {}


for (int i = 0; i < 10; ++i) {}
116962 次浏览

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.

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.

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.

++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

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

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;

++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.

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