java 为什么在 for 循环的迭代部分将 pre 更改为 post 增量不会有所作为?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1918196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?
提问by andandandand
Why does this
为什么会这样
int x = 2;
for (int y =2; y>0;y--){
System.out.println(x + " "+ y + " ");
x++;
}
prints the same as this?
打印和这个一样吗?
int x = 2;
for (int y =2; y>0;--y){
System.out.println(x + " "+ y + " ");
x++;
}
As far, as I understand a post-increment is first used "as it is" then incremented. Are pre-increment is first added and then used. Why this doesn't apply to the body of a for loop?
就我所知,后增量首先“按原样”使用,然后再增量。都是先添加再使用预增量。为什么这不适用于 for 循环的主体?
回答by Paul Wagland
The loop is equivalent to:
该循环等效于:
int x = 2;
{
int y = 2;
while (y > 0)
{
System.out.println(x + " "+ y + " ");
x++;
y--; // or --y;
}
}
As you can see from reading that code, it doesn't matter whether you use the post or pre decrement operator in the third section of the for loop.
从阅读该代码可以看出,在 for 循环的第三部分中使用 post 还是 pre 递减运算符并不重要。
More generally, any for loop of the form:
更一般地说,任何形式的 for 循环:
for (ForInit ; Expression ; ForUpdate)
forLoopBody();
is exactly equivalent to the while loop:
完全等同于 while 循环:
{
ForInit;
while (Expression) {
forLoopBody();
ForUpdate;
}
}
The for loop is more compact, and thus easier to parse for such a common idiom.
for 循环更紧凑,因此更容易解析这种常见的习语。
回答by tdammers
To visualize these things, expand the for loop to a while loop:
要可视化这些内容,请将 for 循环扩展为 while 循环:
for (int i = 0; i < 5; ++i) {
do_stuff(i);
}
Expands to:
扩展为:
int i = 0;
while (i < 5) {
do_stuff(i);
++i;
}
Whether you do post-increment or pre-increment on the loop counter doesn't matter, because the result of the increment expression (either the value before or after the increment) isn't used within the same statement.
在循环计数器上执行后增量还是预增量并不重要,因为增量表达式的结果(增量之前或之后的值)不在同一语句中使用。
回答by BalusC
There's no difference in terms of performance, if that's your concern. It can only be used wrongly (and thus sensitive to errors) when you useit duringthe increment.
如果您担心的话,性能方面没有区别。当您在增量期间使用它时,它只能被错误地使用(因此对错误很敏感)。
Consider:
考虑:
for (int i = 0; i < 3;)
System.out.print(++i + ".."); //prints 1..2..3
for (int i = 0; i < 3;)
System.out.print(i++ + ".."); //prints 0..1..2
or
或者
for (int i = 0; i++ < 3;)
System.out.print(i + ".."); //prints 1..2..3
for (int i = 0; ++i < 3;)
System.out.print(i + ".."); //prints 1..2
Interesting detail is however that the normal idiom is to use i++in the increment expression of the forstatement and that the Java compiler will compile it as if ++iis used.
然而,有趣的细节是,通常的习惯用法是i++在for语句的增量表达式中使用,并且 Java 编译器会像++i使用一样编译它。
回答by Sachin Shanbhag
++i and i++ makes a difference when used in combination with the assignment operator such as int num = i++ and int num = ++i or other expressions. In above FOR loop, there is only incrementing condition since it is not used in combination with any other expression, it does not make any difference. In this case it will only mean i = i + 1.
++i 和 i++ 与赋值运算符(例如 int num = i++ 和 int num = ++i 或其他表达式)结合使用时会有所不同。在上面的 FOR 循环中,只有递增条件,因为它没有与任何其他表达式组合使用,它没有任何区别。在这种情况下,它只会意味着 i = i + 1。
回答by Cedric H.
This loop is the same as this whileloop:
此循环与此while循环相同:
int i = 0;
while(i < 5)
{
// LOOP
i++; // Or ++i
}
So yes, it has to be the same.
所以是的,它必须是相同的。
回答by Noon Silk
Because that statement is just on it's own. The order of the increment doesn't matter there.
因为该声明仅凭其自身。增量的顺序在那里无关紧要。
回答by Paul Tomblin
Those two cases are equivalent because the value of i is compared after the increment statement is done. However, if you did
这两种情况是等价的,因为在执行增量语句后比较 i 的值。但是,如果你这样做了
if (i++ < 3)
versus
相对
if (++i < 3)
you'd have to worry about the order of things.
你不得不担心事情的顺序。
And if you did
如果你做了
i = ++i + i++;
then you're just nuts.
那你就是疯了。
回答by Alan Krueger
Because nothing in your examples is usingthe value returned from the pre- or post-increments. Try wrapping a System.out.println()around the ++xand x++to see the difference.
因为您的示例中没有使用从前增量或后增量返回的值。尝试System.out.println()在++x和周围包裹 ax++以查看差异。
回答by ChssPly76
From the Java Language Specification chapter on for loops:
BasicForStatement:
for ( ForInit ; Expression ; ForUpdate ) Statement... if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded.... If the ForUpdate part is not present, no action is taken.
基本For语句:
for ( ForInit ; Expression ; ForUpdate ) Statement...如果存在 ForUpdate 部分,则表达式从左到右依次求值;它们的值(如果有)将被丢弃。... 如果 ForUpdate 部分不存在,则不执行任何操作。
(highlight is mine).
(亮点是我的)。
回答by user207421
The output is the same because the 'increment' item in the 'for (initial; comparison; increment)' doesn't use the resultof the statement, it just relies on the side-effectof the statement, which in this case is incrementing 'i', which is the same in both cases.
输出是一样的,因为 'for (initial; compare; increment)' 中的 'increment' 项不使用语句的结果,它只依赖于语句的副作用,在这种情况下是递增“i”,这在两种情况下都是相同的。

