C语言 在 C 中的 while 循环中发布增量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25538672/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:18:20  来源:igfitidea点击:

Post Increment in while loop in C

cwhile-looppost-incrementpre-increment

提问by algo

Here is a very simple C program:

这是一个非常简单的 C 程序:

int main()
{
    int i = 0;
    while(i++ < 10)
         printf("%d\n", i);

    return 0;
}

The result is:

结果是:

1
2
3
4
5
6
7
8
9
10

Why 0is not the first number to print? And if I replace the i++with ++iI'll get this:

为什么0不是第一个打印的数字?如果我替换为i++++i我会得到这个:

1
2
3
4
5
6
7
8
9

For both i++and ++ithe first number is 1.
I expected to see the 0for post increment in while loop while().
Thanks.

对于两者i++++i第一个数字是1
我希望0在 while 循环中看到for post 增量while()
谢谢。

回答by paxdiablo

The i++(and ++i) is done as part of the whileexpression evaluation, which happens beforethe printing. So that means it will always print 1initially.

i++(和++i)作为的一部分进行while表达的评价,这恰好之前打印。所以这意味着它总是会在1最初打印。

The only difference between the i++and ++ivariants is when the increment happens inside the expression itself, and this affects the final value printed. The equivalent pseudo-code for each is:

i++++i变体之间的唯一区别是增量发生在表达式本身内部时,这会影响打印的最终值。每个的等效伪代码是:

while(i++ < 10)            while i < 10:
                               i = i + 1
    printf("%d\n", i);         print i
                           i = i + 1

and:

和:

                           i = i + 1
while(++i < 10)            while i < 10:
    printf("%d\n", i);         print i
                               i = i + 1

Let's say igets up to 9. With i++ < 10, it uses 9 < 10for the whileexpression thenincrements ito 10 before printing. So the check uses 9 then prints 10.

让我们说i起床9。使用i++ < 10,它9 < 10用于while表达式然后i在打印前递增到 10。所以支票使用 9 然后打印 10。

With ++i < 10, it first increments ithenuses 10 < 10for the whileexpression. So the check uses 10 and doesn'tprint anything, because the loop has exited because of that check.

++i < 10时,它首先递增i然后使用10 < 10用于while表达。因此该检查使用 10 并且打印任何内容,因为循环已因该检查而退出。

回答by 0x499602D2

i++is post-incrementand ++iis pre-increment. Post-increment means that the previous value is returned after incrementing the object. Pre-increment means that the object is incremented and then returned. Either way, the object is incremented when its expression is evaluated and that's why you don't get 0as the first output.

i++后增++i预递增。后自增是指对象自增后返回前一个值。预增是指对象自增然后返回。无论哪种方式,对象在其表达式被评估时都会增加,这就是为什么你不会0作为第一个输出。

回答by Michael Slevin

You increment iafter checking it and before printing it. Either increment after checking and printing, or use a do whileloop:

i在检查之后和打印之前增加。检查和打印后递增,或使用do while循环:

int main()
{
    int i = 0;
    do {
        printf("%d\n", i);
    } while(i++ < 10);
    return 0;
}

回答by lzam

When the while loop while(i++ < 10)is evaluated it is checking the value of i, adding one to it, and comparing the oldvalue to 10. When you change it to while(++i < 10)it increments the value of ibefore comparing the new value to 10.

当 while 循环while(i++ < 10)被评估时,它会检查 i 的值,给它加 1,并将值与 10进行比较。当你改变它时,while(++i < 10)它会i在将新值与 10 进行比较之前增加 的值。

Either way however, by the time you get to the next statement ihas already been incremented.

然而,无论哪种方式,当您到达下一个语句时i,已经增加了。

If you wanted to print from 0 to 9 you could try

如果你想从 0 打印到 9 你可以试试

    while(i < 10)
     printf("%d\n", i++);

instead. In this case the value of iis incremented afterproviding its original value to the printfstatement.

反而。在这种情况下, 的值向语句提供其原始值i递增。printf

回答by R.M.VIVEK ARNI

The whie loops is check the conditions is true executed while block and test the

wie 循环检查条件是否为真,同时阻塞并测试

conditions is false terminate the while loop.

条件为 false 终止 while 循环。

see about post increment within while loop ++

参见 while 循环 ++ 内的后增量

void main()

无效主()

{

{

int rmvivek;

国际 rmvivek;

clrscr();

clrscr();

printf("?nter the integer values 1 to 10");

printf("?输入整数值 1 到 10");

scanf("%d",&rmvivek);

scanf("%d",&rmvivek);

while(10>= rmvivek++)

而(10>= rmvivek++)

{

{

printf("the value is=%d\n", rmvivek);

printf("值为=%d\n", rmvivek);

}

}

getch();

获取();

}

}