Cprogramming 简明教程
Do-While Loop in C
do-while 循环是最常用的 loops in C 类型之一。 do 和 while 关键字一起使用来形成循环。 do-while 是一个退出验证的循环,其中会在执行循环主体后检查测试条件。而 while loop 是一个 entry-verified 。另一方面, for loop 是一个自动循环。
The do-while loop is one of the most frequently used types of loops in C. The do and while keywords are used together to form a loop. The do-while is an exit-verified loop where the test condition is checked after executing the loop’s body. Whereas the while loop is an entry-verified. The for loop, on the other hand, is an automatic loop.
Syntax of do while Loop
C 中 do-while 循环的语法为:
The syntax of do-while loop in C is −
do {
statement(s);
} while(condition);
How do while Loop Works?
循环结构以关键词 do 开始。然后是大括号内的语句块。 while 关键词紧随右大括号之后。 while 前面有一个括号,其中应该有一个布尔表达式。
The loop construct starts with the keword do. It is then followed by a block of statements inside the curly brackets. The while keyword follows the right curly bracket. There is a parenthesis in front of while, in which there should be a Boolean expression.
现在让我们了解 while 循环是如何工作的。当 C compiler 遇到 do 关键词时,程序控制进入并执行由大括号标记的代码块。当代码块结束时,将评估 while 关键词前面的表达式。
Now let’s understand how the while loop works. As the C compiler encounters the do keyword, the program control enters and executes the code block marked by the curly brackets. As the end of the code block is reached, the expression in front of the while keyword is evaluated.
如果表达式为真,则程序控制返回到循环的顶部。如果表达式为假,则编译器停止回到循环块的顶部,并继续执行该块之后的紧邻语句。请注意,while 语句的末尾有一个分号。
If the expression is true, the program control returns back to the top of loop. If the expression is false, the compiler stops going back to the top of loop block, and proceeds to the immediately next statement after the block. Note that there is a semicolon at the end of while statement.
Flowchart of do while Loop
以下流程图表示 do-while 循环的工作原理:
The following flowchart represents how the do-while loop works −
由于控制循环的表达式是在程序第一次运行循环块之后才进行测试的,因此 do-while 循环被称为“退出验证循环”。此处的关键点是,do-while 循环确保循环至少执行一次。
Since the expression that controls the loop is tested after the program runs the looping block for the first time, the do-while loop is called an "exit-verified loop". Here, the key point to note is that a do-while loop makes sure that the loop gets executed at least once.
while 关键词意味着只要表达式为真,编译器就会继续执行随后的块。但是,由于条件位于循环结构的末尾,因此它是在每次迭代之后才进行检查的(而不是像在 while 循环中那样在每次迭代之前进行检查)。
The while keyword implies that the compiler continues to execute the ensuing block as long as the expression is true. However, since the condition sits at the end of the looping construct, it is checked after each iteration (rather than before each iteration as in the case of a while loop).
程序无条件地执行其第一次迭代,然后测试条件。如果发现为真,则编译器执行下一次迭代。只要发现表达式为假,循环主体就会被跳过,并且 while 循环之后的第一个语句将被执行。
The program performs its first iteration unconditionally, and then tests the condition. If found to be true, the compiler performs the next iteration. As soon as the expression is found to be false, the loop body will be skipped and the first statement after the while loop will be executed.
让我们尝试通过几个示例了解 while 循环的行为。
Let us try to understand the behaviour of the while loop with a few examples.
Example of do while Loop
以下程序打印 Hello world 消息五次。
The following program prints the Hello world message five times.
#include <stdio.h>
int main(){
// local variable definition
int a = 1;
// while loop execution
do{
printf("Hello World\n");
a++;
} while(a <= 5);
printf("End of loop");
return 0;
}
Output
此处,do-while 循环充当一个计数循环。运行代码并检查其输出:
Here, the do-while loop acts as a counted loop. Run the code and check its output −
Hello World
Hello World
Hello World
Hello World
Hello World
End of loop
控制重复次数的变量“a”初始化为 1。程序无条件地进入循环,打印消息,将“a”增加 1。
The variable "a" that controls the number of repetitions is initialized to 1. The program enters the loop unconditionally, prints the message, increments "a" by 1.
当它到达循环的末尾时,将在 while 语句中测试条件。由于条件“a ⇐ 5”为真,因此程序返回到循环的顶部并重新进入循环。
As it reaches the end of the loop, the condition in the while statement is tested. Since the condition "a ⇐ 5" is true, the program goes back to the top of the loop and re-enters the loop.
现在“a”为 2,因此条件仍然为真,因此循环再次重复,并持续到条件变为假。该循环停止重复,并且程序控制转到该块之后的步骤。
Now "a" is 2, hence the condition is still true, hence the loop repeats again, and continues till the condition turns false. The loop stops repeating, and the program control goes to the step after the block.
现在,将“a”的初始值更改为 10,然后再次运行该代码。它将产生以下输出:
Now, change the initial value of "a" to 10 and run the code again. It will produce the following output −
Hello World
End of loop
这是因为程序无条件地进入了循环块。由于 while 关键词之前的条件为假,因此该块不会在下次重复。因此,do-while 循环至少执行一次迭代,因为测试条件位于循环的末尾。出于此原因,do-while 循环被称为“退出验证循环”。
This is because the program enters the looping block unconditionally. Since the condition before the while keyword is false, hence the block is not repeated for the next time. Hence, the do-while loop takes at least one iteration as the test condition is at the end of the loop. For this reason, do-while loop is called an "exit-verified loop".
Difference Between while and do while Loops
使用 @ {s0} 和 @ {s1} 构建的循环看起来相似。你可以轻松地将 @ {s2} 循环转换成 @ {s3} 循环,反之亦然。但是,两者之间存在一定的关键差异。
The loops constructed with while and do-while appear similar. You can easily convert a while loop into a do-while loop and vice versa. However, there are certain key differences between the two.
显而易见的语法差异是 do-while 结构以 @ {s4} 关键字开头,并以 @ {s5} 关键字结尾。@ {s6} 循环不需要 @ {s7} 关键字。其次,在 do-while 循环中,你会在 @ {s8} 前面找到分号。while 循环中没有分号。
The obvious syntactic difference is that the do-while construct starts with the do keyword and ends with the while keyword. The while loop doesn’t need the do keyword. Secondly, you find a semicolon in front of while in case of a do-while loop. There is no semicolon in while loops.
Example
控制循环的测试条件的位置是两者之间的主要差异。测试条件在 while 循环的开头,而在 do-while 循环中它在末尾。它如何影响循环行为?看看以下代码:
The location of the test condition that controls the loop is the major difference between the two. The test condition is at the beginning of a while loop, whereas it is at the end in case of a do-while loop. How does it affect the looping behaviour? Look at the following code −
#include <stdio.h>
int main(){
// local variable definition
int a = 0, b = 0;
// while loop execution
printf("Output of while loop: \n");
while(a < 5){
a++;
printf("a: %d\n", a);
}
printf("Output of do-while loop: \n");
do{
b++;
printf("b: %d\n",b);
} while(b < 5);
return 0;
}
最初,“a”和“b”初始化为“0”,并且两个循环的输出相同。
Initially, "a" and "b" are initialized to "0" and the output of both the loops is same.
Output of while loop:
a: 1
a: 2
a: 3
a: 4
a: 5
Output of do-while loop:
b: 1
b: 2
b: 3
b: 4
b: 5
现在将 @ {s9} 的初始值都更改为 3,然后再次运行该代码。两个循环的输出没有变化。
Now change the initial value of both the variables to 3 and run the code again. There’s no change in the output of both the loops.
Output of while loop:
a: 4
a: 5
Output of do-while loop:
b: 4
b: 5
现在将两个变量的初始值都更改为 10,然后再次运行该代码。在这里,你可以观察到两个循环之间的差异:
Now change the initial value of both the variables to 10 and run the code again. Here, you can observe the difference between the two loops −
Output of while loop:
Output of do-while loop:
b: 11
请注意,@ {s10} 循环没有任何迭代,但 @ {s11} 执行其主体一次。这是因为在 @ {s12} 的情况下,循环条件在循环块的顶部进行验证,并且由于条件为假,所以程序不会进入循环。
Note that the while loop doesn’t take any iterations, but the do-while executes its body once. This is because the looping condition is verified at the top of the loop block in case of while, and since the condition is false, the program doesn’t enter the loop.
在 @ {s13} 的情况下,程序无条件地进入循环,将“b”递增到 11,然后不重复,因为条件为假。它表明 @ {s14} 肯定会进行至少一次重复,无论循环变量的初始值如何。
In case of do-while, the program unconditionally enters the loop, increments "b" to 11 and then doesn’t repeat as the condition is false. It shows that the do-while is guaranteed to take at least one repetition irrespective of the initial value of the looping variable.
do-while 循环还可以用于构建条件循环。你也可以在 @ {s17} 中使用 @ {s18} 和 @ {s19} 语句。