下面的for循环产生相同的结果,尽管一个使用后增量,另一个使用前增量。

代码如下:

for(i=0; i<5; i++) {
    printf("%d", i);
}

for(i=0; i<5; ++i) {
    printf("%d", i);
}

我得到了两个for循环相同的输出。我遗漏了什么吗?


当前回答

代码的结果将是相同的。原因是这两个递增操作可以看作是两个不同的函数调用。两个函数都会导致变量的递增,只有它们的返回值不同。在本例中,返回值被丢弃,这意味着输出中没有可区分的差异。

However, under the hood there's a difference: The post-incrementation i++ needs to create a temporary variable to store the original value of i, then performs the incrementation and returns the temporary variable. The pre-incrementation ++i doesn't create a temporary variable. Sure, any decent optimization setting should be able to optimize this away when the object is something simple like an int, but remember that the ++-operators are overloaded in more complicated classes like iterators. Since the two overloaded methods might have different operations (one might want to output "Hey, I'm pre-incremented!" to stdout for example) the compiler can't tell whether the methods are equivalent when the return value isn't used (basically because such a compiler would solve the unsolvable halting problem), it needs to use the more expensive post-incrementation version if you write myiterator++.

你应该进行预增量的三个原因:

你不必考虑变量/对象是否有重载的增量后方法(例如在模板函数中),并区别对待它(或忘记区别对待它)。 一致的代码看起来更好。 当有人问你“为什么要预增量?”时,你就有机会教他们关于停止问题和编译器优化的理论限制。:)

其他回答

是的,你会得到完全相同的输出。为什么你认为他们应该给你不同的产出?

在这样的情况下,增量后或增量前很重要:

int j = ++i;
int k = i++;
f(i++);
g(++i);

你可以通过赋值或传递参数来提供一些值。你在for循环中两者都不做。它只会递增。后-和前-在那里没有意义!

在计算i++或++i之后,i的新值在这两种情况下是相同的。前增量和后增量之间的区别在于表达式本身的求值结果。

++i增加i并计算为i的新值。

i++计算为I的旧值,并增加I。

这在for循环中无关紧要的原因是控制流大致是这样工作的:

测试条件 如果为假,终止 如果为真,则执行body 执行增量步骤

由于(1)和(4)是解耦的,可以使用前增量或后增量。

如果你这样写,那就很重要了:

for(i=0; i<5; i=j++) {
    printf("%d",i);
}

将比这样写的情况多迭代一次:

for(i=0; i<5; i=++j) {
    printf("%d",i);
}

代码的结果将是相同的。原因是这两个递增操作可以看作是两个不同的函数调用。两个函数都会导致变量的递增,只有它们的返回值不同。在本例中,返回值被丢弃,这意味着输出中没有可区分的差异。

However, under the hood there's a difference: The post-incrementation i++ needs to create a temporary variable to store the original value of i, then performs the incrementation and returns the temporary variable. The pre-incrementation ++i doesn't create a temporary variable. Sure, any decent optimization setting should be able to optimize this away when the object is something simple like an int, but remember that the ++-operators are overloaded in more complicated classes like iterators. Since the two overloaded methods might have different operations (one might want to output "Hey, I'm pre-incremented!" to stdout for example) the compiler can't tell whether the methods are equivalent when the return value isn't used (basically because such a compiler would solve the unsolvable halting problem), it needs to use the more expensive post-incrementation version if you write myiterator++.

你应该进行预增量的三个原因:

你不必考虑变量/对象是否有重载的增量后方法(例如在模板函数中),并区别对待它(或忘记区别对待它)。 一致的代码看起来更好。 当有人问你“为什么要预增量?”时,你就有机会教他们关于停止问题和编译器优化的理论限制。:)

因为在任何一种情况下,增量都是在循环体之后完成的,因此不会影响循环的任何计算。如果编译器是愚蠢的,使用后增量可能会稍微降低效率(因为通常它需要保留一个预值的副本以供以后使用),但我希望在这种情况下优化消除任何差异。

考虑for循环是如何实现的可能会很方便,本质上是转换为一组赋值、测试和分支指令。在伪代码中,预增量看起来像这样:

      set i = 0
test: if i >= 5 goto done
      call printf,"%d",i
      set i = i + 1
      goto test
done: nop

后增量至少会有另一个步骤,但它将是微不足道的优化

      set i = 0
test: if i >= 5 goto done
      call printf,"%d",i
      set j = i   // store value of i for later increment
      set i = j + 1  // oops, we're incrementing right-away
      goto test
done: nop