增量前与增量后 [英] Pre-incrementation vs. post-incrementation
问题描述
它们有何不同?这就是我的想法,但我不确定....
How are they different? Here's what I'm thinking, but I'm not sure....
如果您使用预增量,例如在++ j的for循环中使用,则基本上是在说:复制j的值以供循环使用,然后递增j,然后遍历与j的副本一起循环的语句."如果在同一循环j ++中使用后递增,那么您基本上是在说:复制j的值以供循环使用,然后在循环中使用j的副本遍历语句,然后递增j."
If you use pre-incrementation, for example in a for loop with ++j, then you are basically saying: "Make a copy of the value of j for use in the loop, then increment j, then go through the statements in the loop with the copy of j." If you are using post-incrementation in the same loop j++, then you are basically saying: "Make a copy of the value of j for use in the loop, then go through the statements in the loop with the copy of j, then increment j."
我不确定的原因是因为我创建了一个for循环,将j的值乘以10,然后使用后增量和前增量将j = 1到j = 12的结果输出.人工可读的输出与增量后和增量前完全相同.我在想,如果不涉及某种复制操作,输出将如何完全相同?"
The reason I'm unsure is because I've created a for loop that multiplies the value of j by 10 and then outputs the result for j=1 through j=12, using both post- and pre-incrementation. The human readable output is exactly the same with post- and pre-incrementation. I'm thinking, 'How are the outputs exactly the same if there isn't some kind of copy operation involved?'
所以,我猜测在PHP中,当我使用引用(在php中充当指针)而不是返回值名称时,前后增量之间的区别确实变得很重要.这是因为未创建引用的副本,因此预递增为:递增j,然后在循环中使用更改后的j值遍历语句,然后再次递增j ...,"而后置-增量看起来像是:将j的值用于循环中的语句,然后更改j的值,然后使用j的新值进行循环..."
So, I'm guessing the difference between pre- and post-incrementation truly becomes important, in php, when I use references (which act as pointers in php) rather than names for return values? This would be because copies of references aren't made, so pre-incrementation would be: "Increment j, then go through the statements in the loop with the changed value of j, then increment j again...," whereas post-incremetation would look like: "Use the value of j for the statements in the loop, then change the value of j, then go through the loop with the new value of j..."
推荐答案
Pre- or post-incrementing do not magically delay things until later. It's simply inline shorthand.
// pre-increment
$var = 5;
print(++$var); // increments first, then passes value (now 6) to print()
// post-increment
$var = 5;
print($var++); // passes value (still 5) to print(), then increments
现在让我们来看一个循环.
Now let's look at a loop.
for ($i = 0; $i < 9; $i++) {
print($i);
}
循环声明的最后一部分($i++
)只是每次循环后要执行的语句.它将"值传递"到任何地方,然后递增.当时在任何地方都没有使用$i
.稍后当执行下一条语句(print($i);
)时,$i
的值已经增加.
The last part of the loop declaration (the $i++
) is simply the statement to execute after each time through the loop. It "passes" the value to nowhere, then increments it. $i
isn't used anywhere at that time. Later when the next statement is executed (print($i);
), the value of $i
has already increased.
// add 1, then do nothing with $i
for ($i = 0; $i < 9; ++$i) {}
// do nothing with $i, then add 1
for ($i = 0; $i < 9; $i++) {}
无论采用哪种方式,循环中的$i
都是相同的.
Whichever way you do it, $i
will be the same within the loop.
如果有帮助,您可以将它们视为可以执行此操作的小型例程:
If it helps, you can think of them as small routines that kind of do this:
// ++$i
{
$i = $i + 1;
return $i;
}
// $i++
{
return $i;
$i = $i + 1;
}
当我重新阅读您的问题时,我认为混乱更多地在于循环的工作方式而不是增量运算符的工作方式.请记住,增量是一次直接的一次性操作,这是循环中第三个表达式的工作方式.
As I reread your question, I think the confusion is more with how the loop works than how increment operators work. Keeping in mind that the increment is a straightforward, all-at-once operation, here's how third expression in the loop works.
// here's a basic loop
for ($i = 0; $i < 9; $i++) {
// do loop stuff
print($i);
}
// this is exactly what happens
for ($i = 0; $i < 9; ) {
// do loop stuff
print($i);
$i++;
}
仅因为可以将最后一行放在循环声明中并没有赋予它任何特殊的权力.幕后没有任何参考或使用任何东西.在循环的内部和外部都可以看到相同的$i
变量.必要时,循环内或循环外的每个语句都直接查找$i
的值.而已.没有可笑的事.
Just because that last line can be put in the loop declaration doesn't give it any special powers. There are no references or anything used behind the scenes. The same $i
variable is seen both inside and outside the loop. Every statement inside or outside the loop directly looks up the value of $i
when necessary. That's it. No funny business.
这篇关于增量前与增量后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!