while循环c语言_C – While循环

while循环c语言

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<10a=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.

在下面的示例中,我们将一些消息打印到控制台,然后检查号码。

LEARN MORE  Php - While and Do While Loops
了解更多PHP-While和Do While循环

[rps-include post=6557]

[rps-include post = 6557]

翻译自: https://www.poftut.com/c-while-loops/

while循环c语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值