Cprogramming 简明教程
C - While Loop
在 C 中, while 是我们可以用于构造循环的关键字之一。 while 循环是最常用的 loops in C 之一。C 中的其他循环关键字是 for 和 do-while 。
In C, while is one of the keywords with which we can form loops. The while loop is one of the most frequently used types of loops in C. The other looping keywords in C are for and do-while.
while 循环通常称为 entry verified loop ,而 do-while loop 是 exit verified loop 。而 for loop 是一个 automatic loop 。
The while loop is often called the entry verified loop, whereas the do-while loop is an exit verified loop. The for loop, on the other hand, is an automatic loop.
Syntax of C while Loop
构造 while 循环的语法如下 −
The syntax of constructing a while loop is as follows −
while(expression){
statement(s);
}
while 关键字后跟一个括号,其中应该有一个布尔表达式。在括号后,大括号内有一组语句。
The while keyword is followed by a parenthesis, in which there should be a Boolean expression. Followed by the parenthesis, there is a block of statements inside the curly brackets.
Flowchart of C while Loop
以下流程图表示 while 循环的工作方式 −
The following flowchart represents how the while loop works −
How while Loop Works in C?
C compiler 会计算表达式。如果表达式为真,就会执行后跟的代码块。如果表达式为假,编译器会忽略 while 关键字旁边的块,并继续执行块后的下一个语句。
The C compiler evaluates the expression. If the expression is true, the code block that follows, will be executed. If the expression is false, the compiler ignores the block next to the while keyword, and proceeds to the immediately next statement after the block.
由于在程序进入循环之前会对控制循环的表达式进行测试,因此 while 循环称为 entry verified loop 。此处,需要知道的一点是,如果条件在第一个实例中被发现不为真, while 可能根本不执行。
Since the expression that controls the loop is tested before the program enters the loop, the while loop is called the entry verified loop. Here, the key point to note is that a while loop might not execute at all if the condition is found to be not true at the very first instance itself.
while 关键字表示只要表达式为真,编译器就会继续执行之后的块。该条件位于循环结构的顶部。在每次迭代之后,都会测试该条件。如果发现为真,编译器会执行下一次迭代。一旦发现表达式为假,就会跳过循环体,并执行 while 循环后的第一个语句。
The while keyword implies that the compiler continues to execute the ensuing block as long as the expression is true. The condition sits at the top of the looping construct. After each iteration, the condition is tested. 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.
Example of while Loop in C
下列程序将会显示五次 prints the "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
while(a <= 5){
printf("Hello World \n");
a++;
}
printf("End of loop");
return 0;
}
Output
此处, while 循环充当计数循环。运行代码并查看其输出 −
Here, the 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
Example Explanation
控制重复次数的变量“a”在 while 语句之前被初始化为 1。由于条件“a ⇐ 5”为真,程序会进入循环,打印消息,将“a”递增 1,并返回到循环顶部。
The variable "a" that controls the number of repetitions is initialized to 1, before the while statement. Since the condition "a ⇐ 5" is true, the program enters the loop, prints the message, increments "a" by 1, and goes back to the top of the loop.
在下一迭代中,“a”为 2,因此条件仍然为真,因此循环会再次重复,直到条件变为假。循环会停止重复,程序控制会转到块后的步骤。
In the next iteration, "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. Now the output will show the following −
End of loop
这是因为 while 关键字之前的条件在第一个迭代中本身就为假,因此不会重复该块。
This is because the condition before the while keyword is false in the very first iteration itself, hence the block is not repeated.
“char”变量表示对应于其 ASCII 值的字符。因此,可以对其进行递增。因此,从“a”开始递增变量值,直到达到“z”。
A "char" variable represents a character corresponding to its ASCII value. Hence, it can be incremented. Hence, we increment the value of the variable from "a" till it reaches "z".
Using while as Conditional Loop
你可以在给定条件得到满足之前,将 while 循环用作执行条件循环的地方。
You can use a while loop as a conditional loop where the loop will be executed till the given condition is satisfied.
Example
在此示例中, while 被用作 conditional loop 。在所接收的输入为非负值之前,该循环将继续重复。
In this example, the while loop is used as a conditional loop. The loop continues to repeat till the input received is non-negative.
#include <stdio.h>
int main(){
// local variable definition
char choice = 'a';
int x = 0;
// while loop execution
while(x >= 0){
(x % 2 == 0) ? printf("%d is Even \n", x) : printf("%d is Odd \n", x);
printf("\n Enter a positive number: ");
scanf("%d", &x);
}
printf("\n End of loop");
return 0;
}
While Loop with break and continue
In all the examples above, the while loop is designed to repeat for a number of times, or till a certain condition is found. C has break and continue statements to control the loop. These keywords can be used inside the while loop.
More Examples of C while Loop
Example: Printing Lowercase Alphabets
以下程序借助 while 循环打印所有小写字母。
The following program prints all the lowercase alphabets with the help of a while loop.
#include <stdio.h>
int main(){
// local variable definition
char a = 'a';
// while loop execution
while(a <= 'z') {
printf("%c", a);
a++;
}
printf("\n End of loop");
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
abcdefghijklmnopqrstuvwxyz
End of loop
Example: Equate Two Variables
在给定的代码中,我们有两个 variables “a”和“b”,分别初始化为 10 和 0。在循环内,“b”递减,“a”递增,每次迭代。该循环被设计为在“a”和“b”不等于之前重复。当两者都达到 5 时,循环结束。
In the code given below, we have two variables "a" and "b" initialized to 10 and 0, respectively. Inside the loop, "b" is decremented and "a" is incremented on each iteration. The loop is designed to repeat till "a" and "b" are not equal. The loop ends when both reach 5.
#include <stdio.h>
int main(){
// local variable definition
int a = 10, b = 0;
// while loop execution
while(a != b){
a--;
b++;
printf("a: %d b: %d\n", a,b);
}
printf("\n End of loop");
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
a: 9 b: 1
a: 8 b: 2
a: 7 b: 3
a: 6 b: 4
a: 5 b: 5
End of loop
while Vs. do while Loops
在大多数情况下, do-while 循环看起来类似于 while 循环,但其语法不同。 do-while 被称为 exit verified loop 。在某些情况下,它们的行为不同。 while 与 do-while 循环之间的区别在本教程的 do-while 章节中进行了说明。
The do-while loop appears similar to the while loop in most cases, although there is a difference in its syntax. The do-while is called the exit verified loop. In some cases, their behaviour is different. Difference between while and do-while loop is explained in the do-while chapter of this tutorial.