For faster navigation, this Iframe is preloading the Wikiwand page for While循环.

While循环

此条目没有列出任何参考或来源。 (2014年8月22日)维基百科所有的内容都应该可供查证。请协助补充可靠来源改善这篇条目。无法查证的内容可能会因为异议提出而被移除。

编程语言中,While循环(英语:while loop)是一种控制流程陈述。利用一个返回结果为布尔值(Boolean)的表达式作为循环条件,当这个表达式的返回值为“真”(true)时,则反复执行循环内的代码;若表达式的返回值为“假”(false),则结束执行循环内的代码,继续执行循环下面的代码。

因为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)


另见

{{bottomLinkPreText}} {{bottomLinkText}}
While循环
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?