后增量和pre-增量的for循环产生相同的输出中 [英] Post-increment and pre-increment within a 'for' loop produce same output

查看:108
本文介绍了后增量和pre-增量的for循环产生相同的输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关循环下产生相同的结果,即使人使用后增量和其他pre-增量。

The following for loops produce identical results even though one uses post increment and the other pre-increment.

下面是code:

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

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

我得到两个'为'循环相同的输出。我缺少的东西吗?

I get the same output for both 'for' loops. Am I missing something?

推荐答案

评估后我++ ++我的新值i 将在这两种情况下是相同的。 pre和后增量之间的差是在评价前pression本身的结果。

After evaluating i++ or ++i, the new value of i will be the same in both cases. The difference between pre- and post-increment is in the result of evaluating the expression itself.

++我递增 I 键,计算结果为的新值i

++i increments i and evaluates to the new value of i.

我++ 计算结果为的旧值i 和增量 I

这不会在for循环中重要的原因是控制流的工作原理大致是这样的:

The reason this doesn't matter in a for loop is that the flow of control works roughly like this:


  1. 测试条件

  2. 如果是假的,终止

  3. 如果这是真的,执行体

  4. 执行增量步

由于(1)和(4)被解耦,可以使用任一pre-或后增

Because (1) and (4) are decoupled, either pre- or post-increment can be used.

这篇关于后增量和pre-增量的for循环产生相同的输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆