C语言 “while”循环和“do while”循环的区别

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

Difference between "while" loop and "do while" loop

cloops

提问by narayanpatra

What is the difference between while loop and do while loop. I used to think both are completely same.Then I came across following piece of code :

while 循环和 do while 循环有什么区别。我曾经认为两者完全相同。然后我遇到了以下代码:

do {
        printf("Word length... ");
        scanf("%d", &wdlen);
    } while(wdlen<2);

This code works perfectly. It prints word length and tascans the input. But when I changed it to

这段代码完美运行。它打印字长并扫描输入。但是当我把它改成

while(wdlen<2){
        printf("Word length... ");
        scanf("%d", &wdlen);
    } 

It gives a blank screen. It do not work. So there is some functional difference between both loops. Can anybody explain it? Thanks in advance.

它给出了一个空白屏幕。它不起作用。因此,两个循环之间存在一些功能差异。有人可以解释一下吗?提前致谢。

EDIT : Is there any other difference in these two ?

编辑:这两者还有其他区别吗?

回答by hydrogen

The do whileloop executes the content of the loop once before checking the condition of the while.

同时做循环一次检查,同时的条件之前执行循环的内容。

Whereas a whileloop will check the condition first before executing the content.

while循环将在执行内容之前先检查条件。

In this case you are waiting for user input with scanf(), which will never execute in the whileloop as wdlen is not initialized and may just contain a garbage value which may be greater than 2.

在这种情况下,您正在使用 scanf() 等待用户输入,它永远不会在while循环中执行,因为 wdlen 未初始化并且可能只包含可能大于 2 的垃圾值。

回答by Guillaume Lebourgeois

While: your condition is at the begin of the loop block, and makes possible to never enter the loop.

While:您的条件位于循环块的开头,并且可以永不进入循环

Do While: your condition is at the end of the loop block, and makes obligatory to enter the loop at least one time.

Do While:您的条件位于循环块的末尾,并且必须至少进入循环一次

回答by Mahmoud

do {
    printf("Word length... ");
    scanf("%d", &wdlen);
} while(wdlen<2);

A do-whileloop guarantees the execution of the loop at least once because it checks the loop condition AFTER the loop iteration. Therefore it'll print the string and call scanf, thus updating the wdlen variable.

do-while环保证至少所述循环的执行一次,因为它会检查循环迭代后循环条件。因此它将打印字符串并调用 scanf,从而更新 wdlen 变量。

while(wdlen<2){
    printf("Word length... ");
    scanf("%d", &wdlen);
} 

As for the whileloop, it evaluates the loop condition BEFORE the loop body is executed. wdlenprobably starts off as more than 2 in your code that's why you never reach the loop body.

至于while循环,它在执行循环体之前评估循环条件。wdlen可能在您的代码中开始时超过 2,这就是为什么您永远不会到达循环体的原因。

回答by sriram

do while in an exit control loop. while is an entry control loop.

在退出控制循环中执行 while。while 是一个入口控制循环。

回答by Unnati shah

While:

尽管:

  1. entry control loop

  2. condition is checked before loop execution

  3. never execute loop if condition is false

  4. there is no semicolon at the end of while statement

  1. 入口控制回路

  2. 在循环执行之前检查条件

  3. 如果条件为假从不执行循环

  4. while语句末尾没有分号

Do-while:

做时:

  1. exit control loop

  2. condition is checked at the end of loop

  3. executes false condition at least once since condition is checked later

  4. there is semicolon at the end of while statement.

  1. 退出控制循环

  2. 在循环结束时检查条件

  3. 由于稍后检查条件,因此至少执行一次错误条件

  4. while 语句末尾有分号。

回答by Stephen

The difference is in when the condition gets evaluated. In a do..whileloop, the condition is not evaluated until the end of each loop. That means that a do..whileloop will alwaysrun at least once. In a whileloop, the condition is evaluated at the start.

不同之处在于条件得到评估的时间。在do..while循环中,直到每个循环结束时才会评估条件。这意味着do..while循环将始终至少运行一次。在while循环中,条件在开始时被评估。

Here I assume that wdlenis evaluating to false (i.e., it's bigger than 1) at the beginning of the whileloop, so the while loop never runs. In the do..whileloop, it isn't checked until the end of the first loop, so you get the result you expect.

在这里,我假设wdlenwhile循环开始时评估为假(即它大于 1),因此 while 循环永远不会运行。在do..while循环中,直到第一个循环结束才对其进行检查,因此您会得到预期的结果。

回答by SPB

Do whileloop will be executed atleast once.......but whileloop will check the condition first and then it may or may not get executed depending on the condition.

Do while循环将至少执行一次......但while循环将首先检查条件,然后根据条件可能会或可能不会执行。

In your example wdlen may assume any garbage value which is > 2 so whileloop will never get executed.

在您的示例中, wdlen 可能会假设任何大于 2 的垃圾值,因此永远不会执行while循环。

whereas do whileloop will be ececuted and will tell u to enter the value and check that value in terminating condition

do while循环将被执行并告诉您输入值并在终止条件下检查该值

回答by s.s

while(wdlen<2){
  ...
 }  

If wdlen (assuming it's a stack variable) is not initialized or assigned a value before the while loop is entered, it will contain whatever was in that space in memory before (i.e. garbage). So if the garbage value is < 2, the loop executes, otherwise it doesn't.

如果 wdlen(假设它是一个堆栈变量)在进入 while 循环之前没有初始化或赋值,它将包含之前内存中该空间中的任何内容(即垃圾)。因此,如果垃圾值 < 2,则循环执行,否则不执行。

do{
 ...
}while(wdlen<2)

will execute once and then checks on condition to run loop again, and this time it might succeed if by chance wdlen which is uninitialized is found to be less than 2.

将执行一次,然后检查条件以再次运行循环,如果偶然发现未初始化的 wdlen 小于 2,它可能会成功。

回答by sth

Probably wdlenstarts with a value >=2, so in the second case the loop condition is initially false and the loop is never entered.

可能wdlen以 >=2 的值开始,因此在第二种情况下,循环条件最初为假,并且永远不会进入循环。

In the second case the loop body is executed before the wdlen<2condition is checked for the first time, so the printf/scanfis executed at least once.

在第二种情况下,循环体wdlen<2在第一次检查条件之前执行,因此printf/scanf至少执行一次。

回答by Prime

whiletest the conditionbefore executing statementswithin the whileloop.

whilewhile循环中执行语句之前测试条件

do whiletest the conditionafter having executed statementwithin the loop.

在循环内执行语句进行 while测试条件

source: let us C

来源:让我们C