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 迴圈
'這是一個用While迴圈的例子
dim counter as Integer
dim Tick as Integer
counter=5
tick=1
Print "Start"
while counter>0
counter=counter-tick
'迴圈語句
Wend
Print "End"


unsigned int counter = 5;
unsigned long factorial = 1;

while (counter > 0)
{
  factorial *= counter--;    /*當滿足迴圈條件(本例為:counter > 0)時會反覆執行該條語句 */
} 

printf("%lu", factorial);


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++;
    }
}


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?