
while循环c语言
[rps-include post=6557]
[rps-include post = 6557]
Loops are used to loop and iterate through sequential data or similar serial output. Sequential data will be generally an array. For example to search in a array for a specific value while loop can be used. We will look 3 main loop mechanism provided by C programming language.
循环用于循环和遍历顺序数据或类似的串行输出。 顺序数据通常是一个数组。 例如,可以使用循环在数组中搜索特定值。 我们将看一下C编程语言提供的3个主要循环机制。
而 (While)
While runs repeatedly if given condition is true. Here is the syntax of while .
如果给定条件为真,则重复运行。 这是while的语法。
while(condition)
{
statemens;
}
In while syntax condition can be a boolean value like true
or false
.
在while语法条件可以是像一个布尔值true
或f alse
。
In this example we will count up to 10. For each step the a<10
will be evaluated. a=a+1
will run in each step and a
will increment one by one. When a
reached 10 the a<10
will be false and the while loop end. In each step the value of the a
will be printed with printf
function.
在此示例中,我们最多可以计数10。对于每个步骤,将评估a<10
。 a=a+1
将在每个步骤中运行,而a
将一一递增。 当a
达到10时, a<10
将为false,而while循环结束。 在每个步骤中,将使用printf
函数打印a
的值。
#include <stdio.h>
int main() {
int a=0;
while(a<10){
a = a + 1;
printf("%d\n",a);
}
return 0;
}

Here our condition is a<10
which is met for the first 10 steps but in each step we increment a
and print it to the console with printf("%d\n",a)
这里我们的条件是a<10
在前10个步骤中可以满足要求,但在每个步骤中我们都会增加a
并使用printf("%d\n",a)
将其打印到控制台
无限While循环(Infinite While Loop)
While loop is the simplest and easiest way to create infinite loop. Infinite loop will run forever if the application is not stopped externally. We will put 1
into condition of the while which is equivalent of true
boolean in C.
While循环是创建无限循环的最简单,最简单的方法。 如果应用程序未在外部停止,则无限循环将永远运行。 我们将1
为while的条件,它等于C中的true
布尔值。
while(1){
a = a + 1;
printf("%d\n",a);
}
OR we will use 1=1
which will evaluate boolean true
否则我们将使用1=1
来评估布尔值true
while(1=1){
a = a + 1;
printf("%d\n",a);
}
OR we will use 1>0
which is always true
否则我们将使用1>0
总是true
while(1>0){
a = a + 1;
printf("%d\n",a);
}
做一会儿 (Do While)
Do While mechanism is very similar to While but the difference is that While code block is executed after condition check. In Do While mechanism code block is executed first and then condition is checked. If condition is not met which is not true the Do While mechanism ends. Here is the syntax.
Do While机制与While类似,但区别在于条件检查后执行While代码块。 在“ Do While机制”中,首先执行代码块,然后检查条件。 如果不满足条件(不正确),则“ Do While”机制结束。 这是语法。
do{
statement;
}while(condition);
In the example below we print some message to the console and then check the number.
在下面的示例中,我们将一些消息打印到控制台,然后检查号码。
[rps-include post=6557]
[rps-include post = 6557]
while循环c语言