While循环
在编程语言中,While循环(英语:)是一种控制流程的陈述。利用一个返回结果为布尔值(Boolean)的表达式作为循环条件,当这个表达式的返回值为“真”(true)时,则反复运行循环内的代码;若表达式的返回值为“假”(false),则结束运行循环内的代码,继续运行循环下面的代码。
结构 |
---|
do-while |
while |
for |
foreach |
因为While循环在区块内代码被运行之前,先检查陈述是否成立,因此这种控制流程通常被称为是一种前测试循环(pre-test loop)。相对而言Do While循环,是在循环区块运行结束之后,再去检查陈述是否成立,被称为是后测试循环。
程序范例

while 循环
VB
'这是一个用While循环的例子
dim counter as Integer
dim Tick as Integer
counter=5
tick=1
Print "Start"
while counter>0
counter=counter-tick
'循环语句
Wend
Print "End"
C/C++
unsigned int counter = 5;
unsigned long factorial = 1;
while (counter > 0)
{
factorial *= counter--; /*当满足循环条件(本例为:counter > 0)时会反复运行该条语句 */
}
printf("%lu", factorial);
Java
public static void main(str args[]){
while true{
System.out.println("Hello World!") //因为条件已经固定为常量true,所以就会不断运行循环内的语句
}
int counter = 0 ;
while counter<5{
System.out.println("已经运行了"+counter+"次") //因为条件限定为counter不大于5,所以在counter不大于5的情况下会不断重复循环中的内容
counter++;
}
}
Python语言
a = 0
while a <= 10 : #如果a没有大于10就运行
a = a+1
print(a)
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.