while 和 do while C++ 的区别?

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

The difference between while and do while C++?

c++loopswhile-loop

提问by Kahn Kah

I would like someone to explain the difference between a while and a do while in C++

我希望有人解释一下 C++ 中的一会儿和一会儿做一会儿之间的区别

I just started learning C++ and with this code I seem to get the same output:

我刚开始学习 C++,使用这段代码,我似乎得到了相同的输出:

int number =0;

while (number<10)
{
cout << number << endl;
number++
}

and this code:

和这个代码:

int number=0;

do
{
cout << number << endl;
number++
} while (number<10);

The output is both the same in these both calculations. So there seem to be no difference. I tried to look for other examples but they looked way to difficult to understand since it contained mathemetical stuff and other things which I haven't quite learned yet. Also my book gives a sort of psychedelic answer to my question.

在这两种计算中,输出都是相同的。所以好像没什么区别。我试图寻找其他例子,但它们看起来很难理解,因为它包含数学内容和其他我还没有完全学会的东西。我的书也对我的问题给出了一种迷幻的答案。

Is there an easier example to show the difference between these 2 loops?

有没有更简单的例子来说明这两个循环之间的区别?

I was quite curious

我很好奇

回答by tillaert

The whileloop first evaluates number < 10and then executes the body, until number < 10is false.

while循环首先评估number < 10,然后执行体,直到number < 10IS false

The do-whileloop, executes the body, and then evaluates number < 10, until number < 10is false.

do-while循环,执行体,然后评估number < 10,直到number < 10false

For example, this prints nothing:

例如,这不会打印任何内容:

int i = 11;

while( i < 10 )
{
    std::cout << i << std::endl;
    i++;
}

But this prints 11:

但这会打印11

int j = 11;

do
{
    std::cout << j << std::endl;
    j++;
}
while( j < 10 );

回答by ahjashish

The whileloop is an entry control loop, i.e. it first checks the conditionin the while(condition){ ...body... }and then executes the bodyof the loop and keep looping and repeating the procedure until the condition is false.

同时循环是一个进入控制环,即,它首先检查条件while(condition){ ...body... }然后执行体循环,并保持循环并重复该过程,直到条件为假。

The do whileloop is an exit control loop, i.e. it checks the conditionin the do{...body...}while(condition)after the body of the loop has been executed(the body in the do while loop will always be executed at least once) and then loops through the body again until the condition is found to be false.

做而循环是一个出口控制回路,即,它检验状态do{...body...}while(condition)所述循环体已经被执行之后所述主体在while循环将总是被至少执行一次做),然后穿过主体再次循环,直到发现条件为假。

Hope this helps :)

希望这可以帮助 :)

For Example: In case of while loop nothing gets printed in this situation as 1 is not less than 1, condition fails and loop exits

例如:在 while 循环的情况下,在这种情况下不会打印任何内容,因为 1 不小于 1,条件失败并且循环退出

int n=1;
while(n<1)
    cout << "This does not get printed" << endl;

Whereas in case of do while the statement gets printed as it doesn't know anything about the condition right now until it executes the body atleast once and then it stop because condition fails.

而在 do while 语句被打印的情况下,因为它现在对条件一无所知,直到它至少执行一次主体,然后因为条件失败而停止。

int n=1;
do
   cout << "This one gets printed" << endl;
while(n<1);

回答by splrs

If you consider using a different starting value you can more clearly see the difference:

如果您考虑使用不同的起始值,您可以更清楚地看到差异:

int number = 10;

while (number<10)
{
    cout << number << endl;
    number++
}
// no output

In the first example the condition immeditately fails, so the loop won't execute. However, because the condition isn't tested until afterthe loop code in the 2nd example, you'll get a single iteration.

在第一个示例中,条件立即失败,因此循环不会执行。但是,因为在第二个示例中的循环代码之后才会测试条件,所以您将获得一次迭代。

int number = 10;

do
{
    cout << number << endl;
    number++
}
while (number<10);
// output: 10

回答by John Seed

The while loop will only execute of the conditions are met. Whereas the do while loop will execute the first time without verifying the conditions, not until after initial execution.

while 循环只会在满足条件时执行。而 do while 循环将在不验证条件的情况下第一次执行,直到初始执行之后。