预增量与后增量

发布于 2024-11-16 03:39:40 字数 507 浏览 0 评论 0原文

它们有何不同?这就是我的想法,但我不确定......

如果您使用预增量,例如在带有 ++j 的 for 循环中,那么您基本上是在说:“复制 j 的值在循环中使用,然后递增 j,然后使用 j 的副本遍历循环中的语句。”如果您在同一个循环 j++ 中使用后递增,那么您基本上是在说:“复制 j 的值以供在循环中使用,然后使用 j 的副本遍历循环中的语句,然后递增j.”

我不确定的原因是因为我创建了一个 for 循环,将 j 的值乘以 10,然后使用后递增和前递增输出 j=1 到 j=12 的结果。人类可读的输出与增量后和增量前完全相同。我在想,“如果不涉及某种复制操作,输出如何完全相同?”

所以,我猜当我使用引用(在 php 中充当指针)而不是返回值的名称时,在 php 中,预增量和后增量之间的区别确实变得很重要?这是因为没有创建引用的副本,因此预增量将是:“增量 j,然后使用 j 的更改值遍历循环中的语句,然后再次增量 j...”,而后增量增量看起来像:“在循环中的语句中使用 j 的值,然后更改 j 的值,然后使用 j 的新值执行循环...”

How are they different? Here's what I'm thinking, but I'm not sure....

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

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?'

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

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

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

发布评论

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

评论(3

笑红尘 2024-11-23 03:39:40

预增量或后增量不会神奇地将事情延迟到以后。这只是内联简写。

在此处输入图像描述

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

现在让我们看一个循环。

for ($i = 0; $i < 9; $i++) {
    print($i);
}

循环声明的最后一部分($i++)只是每次循环后执行的语句。它将值“传递”到任何地方,然后递增它。当时任何地方都没有使用 $i 。稍后,当执行下一条语句时 (print($i);),$i 的值已经增加。

// 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 在循环内都是相同的。


如果有帮助,您可以将它们视为执行此操作的小例程:

// ++$i
{
    $i = $i + 1;
    return $i;
}

// $i++
{
    return $i;
    $i = $i + 1;
}

当我重读您的问题时,我认为令人困惑的更多是循环如何工作,而不是增量运算符如何工作。请记住,增量是一个简单的一次性操作,以下是循环中第三个表达式的工作原理。

// 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 的值。就是这样。没什么好笑的。

Pre- or post-incrementing do not magically delay things until later. It's simply inline shorthand.

enter image description here

// 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);
}

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

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

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.

幼儿园老大 2024-11-23 03:39:40

当执行 $x++ 时,您是后递增...这意味着只有在对语句求值之后才会发生递增。

因此,给出以下代码:

$x = 10; $y = 0; $z = 5;

$y = $z * $x++;

PHP 这样做:

$x = 10; $y = 0; $z = 5;

$y = $z * $x++;

// Ignore Post-Increment, Evalutate
$y = $z * $x;
$y = 5 * 10;

// Now Increment x - POST-INCREMENT
$x = $x + 1;
$x = 10 + 1;
$x = 11;

// Continue evaluating statement
$y = 5 * 10;
$y = 50;

当执行 ++$x 时,您正在预递增... 这意味着递增将在语句求值之前发生:

$x = 10; $y = 0; $z = 5;

$y = $z * ++$x;

// Do Pre-Increment
$x = $x + 1;
$x = 10 + 1;
$x = 11;

// Evaluate
$y = $z * $x;
$y = 5 * 11;
$y = 55;

在以下情况下PHP 中的 for 循环,PHP 按如下方式计算 for 循环:

for($i = 0; $i < 30; $i++) {
  doSomething();
}

// Is evaluated EXACTLY as such by PHP

$i = 0;
while($i < 30) {
  doSomething();

  $i++;
}

第一个表达式 ($i = 0) 在循环开始时无条件计算(执行)一次。

在每次迭代开始时,$i << 30 被评估。如果计算结果为 TRUE,则循环继续并执行嵌套语句。如果计算结果为 FALSE,则循环执行结束。

在每次迭代结束时,$i++ 作为独立表达式进行计算(执行)。

因此,将变量后递增或预递增作为循环中的第三个表达式不会对其行为产生影响。在这个简单的情况下,两个表达式都会表现得完全相同相同

但是,在如下所示的复杂循环中:

for($i = $j = 0; $i < 30; $i += ++$j) {
  $j = getResult($j);
}

根据上面的示例,后递增或预递增 $j 直接影响 $i 的值。在这种情况下,您需要准确选择您想要做什么。

When doing $x++, you are post-incrementing... This means that the incrementation will only occur after the statement has been evaluated.

So, given the following code:

$x = 10; $y = 0; $z = 5;

$y = $z * $x++;

PHP does this:

$x = 10; $y = 0; $z = 5;

$y = $z * $x++;

// Ignore Post-Increment, Evalutate
$y = $z * $x;
$y = 5 * 10;

// Now Increment x - POST-INCREMENT
$x = $x + 1;
$x = 10 + 1;
$x = 11;

// Continue evaluating statement
$y = 5 * 10;
$y = 50;

When doing ++$x, you are pre-incrementing... This means that the incrementation will occur before the statement is evaluated:

$x = 10; $y = 0; $z = 5;

$y = $z * ++$x;

// Do Pre-Increment
$x = $x + 1;
$x = 10 + 1;
$x = 11;

// Evaluate
$y = $z * $x;
$y = 5 * 11;
$y = 55;

In the case of a for loop in PHP, PHP evaluates a for loop as follows:

for($i = 0; $i < 30; $i++) {
  doSomething();
}

// Is evaluated EXACTLY as such by PHP

$i = 0;
while($i < 30) {
  doSomething();

  $i++;
}

The first expression ($i = 0) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, $i < 30 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

At the end of each iteration, $i++ is evaluated (executed) as an independent expression.

Therefore, post-incrementing or pre-incrementing a variable as the third expression in the loop doesn't have an effect on the behavior of it. In this simple case, both expressions will behave exactly the same.

However, in a complex loop such as the following:

for($i = $j = 0; $i < 30; $i += ++$j) {
  $j = getResult($j);
}

Post-incrementing or pre-incrementing $j directly affects the value of $i according to the examples above. In this case, you need to choose exactly what you want to do.

如梦亦如幻 2024-11-23 03:39:40
$i = 0;
echo $i++;
echo $i;
$j=0;
echo ++$j;
echo $j;

预增量显示增量值。但后增量显示值然后再增量。关于代码会输出01和11

$i = 0;
echo $i++;
echo $i;
$j=0;
echo ++$j;
echo $j;

Pre increment display incremented value. But Post increment display value then increment. About code will output 01 and 11

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