fbpx
维基百科

While迴圈

程式語言中,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) 


另见

while迴圈, 此條目没有列出任何参考或来源, 2014年8月22日, 維基百科所有的內容都應該可供查證, 请协助補充可靠来源以改善这篇条目, 无法查证的內容可能會因為異議提出而移除, 在程式語言中, 英語, while, loop, 是一種控制流程的陳述, 利用一個返回結果為布林值, boolean, 的表達式作為循環條件, 當這個表達式的返回值為, true, 則反覆執行迴圈內的程式碼, 若表達式的返回值為, false, 則結束執行迴圈內的代碼, 繼續執行迴圈下面的代碼, 因為在區塊內代碼被執行之前, 先檢. 此條目没有列出任何参考或来源 2014年8月22日 維基百科所有的內容都應該可供查證 请协助補充可靠来源以改善这篇条目 无法查证的內容可能會因為異議提出而移除 在程式語言中 While迴圈 英語 while loop 是一種控制流程的陳述 利用一個返回結果為布林值 Boolean 的表達式作為循環條件 當這個表達式的返回值為 真 true 時 則反覆執行迴圈內的程式碼 若表達式的返回值為 假 false 則結束執行迴圈內的代碼 繼續執行迴圈下面的代碼 因為While迴圈在區塊內代碼被執行之前 先檢查陳述是否成立 因此這種控制流程通常被稱為是一種前測試迴圈 pre test loop 相對而言Do While迴圈 是在迴圈區塊執行結束之後 再去檢查陳述是否成立 被稱為是後測試迴圈 目录 1 程式範例 1 1 VB 1 2 C C 1 3 Java 1 4 Python語言 2 另见程式範例 编辑 while 迴圈 VB 编辑 這是一個用While迴圈的例子 dim counter as Integer dim Tick as Integer counter 5 tick 1 Print Start while counter gt 0 counter counter tick 迴圈語句 Wend Print End C C 编辑 unsigned int counter 5 unsigned long factorial 1 while counter gt 0 factorial counter 當滿足迴圈條件 本例為 counter gt 0 時會反覆執行該條語句 printf lu factorial Java 编辑 public static void main str args while true System out println Hello World 因為條件已經固定為常量true 所以就會不斷執行迴圈內的語句 int counter 0 while counter lt 5 System out println 已經運行了 counter 次 因為條件限定為counter不大於5 所以在counter不大於5的情況下會不斷重複迴圈中的内容 counter Python語言 编辑 a 0 while a lt 10 如果a沒有大於10就執行 a a 1 print a 另见 编辑for循环 迭代 取自 https zh wikipedia org w index php title While迴圈 amp oldid 66073828, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。