Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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
C++ 这两个项目有什么不同?_C++ - Fatal编程技术网

C++ 这两个项目有什么不同?

C++ 这两个项目有什么不同?,c++,C++,这两个项目之间有什么区别?第一个程序的输出是9,第二个程序的输出是10 #include <iostream> using namespace std; // So we can see cout and endl int main() { int x = 0; // Don't forget to declare variables while ( x < 10 ) { // While x is less than 10 cout &l

这两个项目之间有什么区别?第一个程序的输出是9,第二个程序的输出是10

#include <iostream>

using namespace std; // So we can see cout and endl

int main()
{
    int x = 0; // Don't forget to declare variables

    while ( x < 10 ) { // While x is less than 10
        cout << x << endl;
        x++; // Update x so the condition can be met eventually
    }
    cin.get();
}
#包括
使用命名空间std;//所以我们可以看到cout和endl
int main()
{
int x=0;//不要忘记声明变量
while(x<10){//whilex小于10

cout在第一个代码块中,您输出x,然后添加到其中,这样它将输出0-9。在第二个代码块中,您将在输出之前将1添加到x,这样它将给您输出1-10。它基于您将
x++
放在与输出语句相关的位置。第一个的输出是0 1 2 3 5 7 8 9。第二个的输出是12 3 4 5 6 7 8 9 10。

在第一个示例中,您写出数字,然后增加变量,而在第二个示例中,您首先增加值。

这是由于顺序(显然)。在第一个while循环中,当
x
为9时,它会打印它,增加它,不通过条件,并且不会再次进入循环

在第二种情况下,当
x
为9时,它将其增加到10,然后打印,然后离开循环。这只是常识。因此,第二个循环跳过数字0,一直打印到10

第一个循环:0,1,2,3,4,5,6,7,8,9


第二个循环:1,2,3,4,5,6,7,8,9,10

第一个代码段打印变量的值,然后将其递增。这意味着将不打印上次递增后的变量值


第二个代码段递增变量,然后打印它。这意味着它不会打印0。它初始化为的值。

第10行和第11行被交换。@Fozi hahaha,可爱的回答,
001110101
jkfbvafdsf
是代码,但
int main(void){}
是代码。使用调试器逐步执行,并观察x@SuperKhew:我看不出佛子评论中的幽默。这只是答案。该死的。你打破了我的讽刺检测器。回答问题做得很好,但不是OP真正想要的。值只在打印后检查条件?为什么第二个代码不会在9停止如果条件是xBeacuse 9<10,它将再次执行循环,对吗?值只会在打印出来后与条件一起检查?您的意思是,值只会在打印出来后与条件一起检查?@SuperKhew a
循环会在每个循环迭代开始之前对条件进行一次评估。它将OSSN在循环体中执行语句时不连续地评估条件,所以它不会在循环体的中间中断。所以,只有在打印完循环体之后,才检查该值,并且检查它是否应该再次循环。
#include <iostream>

using namespace std; // So we can see cout and endl

int main()
{
    int x = 0; // Don't forget to declare variables

    while ( x < 10 ) { // While x is less than 10
        x++; // Update x so the condition can be met eventually
        cout << x << endl;
    }
    cin.get();
}