Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
而2.c中c底漆加_C_While Loop - Fatal编程技术网

而2.c中c底漆加

而2.c中c底漆加,c,while-loop,C,While Loop,原代码如下: #include <stdio.h> int main(void) { int n = 0; while (n++ < 3); printf("n is %d\n", n); return 0; } #包括 内部主(空) { int n=0; 而(n++

原代码如下:

#include <stdio.h>
int main(void)
{
    int n = 0;

    while (n++ < 3);
        printf("n is %d\n", n);

    return 0;
}
#包括
内部主(空)
{
int n=0;
而(n++<3);
printf(“n是%d\n”,n);
返回0;
}

我想知道为什么结果是“n是4”而不是“n是3”?

这里发生的是,你将一个好的值与之比较,比如说2<3,然后发生后增量,你在循环中得到3

例如:

// you probably want to remove the ; at the end of the while like this:
while (n++ < 3) { // the posticrement will update the value n
    printf("n is %d\n", n); // here n will have an updated value
}
//您可能想删除;在这段时间的最后,我会这样说:
而(n++<3){//postcrement将更新值n
printf(“n是%d\n”,n);//这里n将有一个更新的值
}

使用{}代替缩进也是一种很好的做法。

这里发生的是,将一个好的值与之比较,比如说2<3,然后发生后增量,在循环中得到3

例如:

// you probably want to remove the ; at the end of the while like this:
while (n++ < 3) { // the posticrement will update the value n
    printf("n is %d\n", n); // here n will have an updated value
}
//您可能想删除;在这段时间的最后,我会这样说:
而(n++<3){//postcrement将更新值n
printf(“n是%d\n”,n);//这里n将有一个更新的值
}

使用{}代替缩进也是一种很好的做法。

缩进错误,printf行不应该缩进-仔细查看上面的行。逐步检查代码。缩进错误,printf行不应该缩进-仔细查看上面的行。逐步检查代码。谢谢帮助。在本例中,作者在while末尾添加了“;”,并在第7行的开头添加了缩进。我猜当n为3时,while循环将被终止,但n++语句仍将向n添加1。因此,n的值最终变成4。@buaaqq,没问题。我很高兴能帮上忙。是的,没错。如果这个答案对你有帮助,你能把它标记为问题的答案吗?谢谢你的帮助。在本例中,作者在while末尾添加了“;”,并在第7行的开头添加了缩进。我猜当n为3时,while循环将被终止,但n++语句仍将向n添加1。因此,n的值最终变成4。@buaaqq,没问题。我很高兴能帮上忙。是的,没错。如果这个答案对你有帮助,你能把它标记为这个问题的答案吗?