++Var 和 Var++ 之间的区别

发布于 2024-11-10 04:21:29 字数 270 浏览 3 评论 0原文

在编程中,特别是在 Java 中,以下之间有什么区别:

int var = 0;
var++;

int var = 0;
++var;

这会对 for 循环产生什么影响?

例如

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

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

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) {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

罪#恶を代价 2024-11-17 04:21:29

太棒了;

尽管 var++++var 都会递增它们所应用的变量,但 var++ 返回的结果是变量递增之前的值,而 ++var 返回的结果是应用增量后变量的值。

进一步说明

++varvar++ 形成完整的语句(如您的示例中)时,两者之间没有区别。例如,以下内容

int x = 6;
++x;
assert x == 7;

与然而

int x = 6;
x++;
assert x == 7;

,当 ++varvar++ 用作较大语句的一部分时,两者可能不等效。例如,以下断言通过

int x = 6;
assert ++x == 7;

,而此断言失败

int x = 6;
assert x++ == 7;

虽然 var++++var 都递增了它们所应用的变量,但 返回的结果var++ 是变量递增之前的值,而 ++var 返回的结果是应用递增之后变量的值。

当在 < code>for 循环,两者没有区别,因为增量变量的值不构成较大语句的一部分。可能不会这样出现,因为源文件的同一行上还有其他代码。但如果仔细观察,您会发现增量之前有一个 ; ,后面没有任何内容,因此增量运算符不构成较大语句的一部分。

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.

风蛊 2024-11-17 04:21:29

int a = 5, b;

后增量b = a++;a 首先传输到 b,然后 a 递增,所以现在 b5a 是 < code>6 效果是b = a; a = a + 1;

预自增: b = ++a; :首先将a自增,然后结果为转移到b,所以现在a7b也是7。效果是a = a + 1; b = a

a++++a 保持独立,以类似的方式起作用。在您提供的循环示例中,增量运算符在任何表达式中都不关联,并且是独立的。因此,在这个特定的实现中,这两者是相同的。

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.

小情绪 2024-11-17 04:21:29

++var 是预自增运算符;它在计算表达式之前增加 var 的值。同样,var++是后置自增运算符;在计算表达式之后,它会增加 var 的值。

在简单循环的情况下,两者之间没有区别,因为表达式 ++var;var++; 都会产生相同的结果。

有关详细信息,请参阅例如 http://www. article.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/operators.html#IncDecOps

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

你穿错了嫁妆 2024-11-17 04:21:29

var++ 在递增之前返回其值。
++var 在递增后返回其值。

int var = 0;
System.out.println(var++); // returns 0;

var = 0;
System.out.println(++var); // returns 1

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
苦妄 2024-11-17 04:21:29

在您的示例中,没有区别,但是以下之间有区别:

int var = 0;
int var2 = ++var;

和:

int var = 0;
int var2 = var++;

在第一种情况下,var2 的值为 1,而在第二种情况下,它为 0。

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.

彩扇题诗 2024-11-17 04:21:29

当单独出现在表达式中时,++varvar++ 是相同的。
这适用于您的问题,因为您只有 ++i, i++

当您内联它们时才会发生差异:

int x = 0;
printf( "%d %d\n", ++x, x ); // 1 1
printf( "%d %d\n", x++, x ); // 1 2

如何记住?

只有 您会看到首先是运算符,然后是增量,最后是取值。

当您首先看到变量,然后获取值并随后递增时。

因此,在第一个示例中,您会看到相同的值,因为:

you increment `x`, then access `x`, then access `x` again  

因此,在第二个示例中,您会看到差异,因为:

you access `x`, then increment `x`, then access `x` again

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
假装不在乎 2024-11-17 04:21:29

++i 是预自增,它在出现的表达式中的任何内容之前完成。

i++ 是后递减,它是在它出现的表达式中的任何内容之后完成的。

在第一个循环中,您将从 0 运行到 9。在第二个循环中,它将从 1 运行到 9。

我的建议:避免这两个循环(例外可能是 i++;或第一个循环)。在表达式中寻找太棘手了。

我花了一整天的时间调试

myArray[(i++)%16] = data

并试图找出为什么它有时会尝试写入 data[16]

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

失退 2024-11-17 04:21:29

事实上,这很简单。语句中首先执行预增量 ++i。

j = ++i +1;

相当于

i = i+1;
j = i+1;

最后执行后置增量 i++;

j = i++ +1;

相当于

j = i+1;
i = i+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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文