程式設計基礎/迴圈控制結構
外觀
< 程式設計基礎
在迴圈控制結構中,語句或程式碼塊會一直執行,直到程式達到某個狀態,或者對集合中的每個元素都執行了操作。這通常用while、repeat、for或do..until等關鍵字來表示。[1]
迴圈控制結構的基本屬性是可以重複執行某些程式碼行。迴圈的視覺顯示在流程圖中會形成一個圓形迴圈模式,因此“迴圈”一詞與迴圈控制結構相關聯。迴圈可以透過“先測試”迴圈、“後測試”迴圈和計數迴圈來實現。通常,一個使用布林概念的問題控制著迴圈執行的次數。
While 迴圈
count assigned zero
While count < 5
Display "I love computers!"
Increment count
End
Do-While 迴圈
count assigned five
Do
Display "Blast off is soon!"
Decrement count
While count > zero
Repeat-Until 迴圈
count assigned five
Repeat
Display "Blast off is soon!"
Decrement count
Until count < one
For 迴圈
For x starts at 0, x < 5, increment x
Display "Are we having fun?"
End