轉至內容

Rexx 程式設計/如何使用 Rexx/while

取自 Wikibooks,面向開放世界的開放書籍

"while" 迴圈是一種不確定的迴圈。只要某個條件為真,它們就會重複同一部分程式碼。它們包含可能被重複任意次數的程式碼。在甚至開始執行這一程式碼之前,while 迴圈會檢查條件,然後在程式碼完成之後再次檢查條件,以檢視是否應該再次執行該程式碼。

/* Let's start our counting at 1. */
count = 1
/* The first loop will be skipped over entirely, since the condition is false. */
do while count < 0
 say "You'll never see this!"
end
/* The following loop will count to 10 and then stop because count will be 11. */
do while count <= 10
 say count
 count = count + 1
end
/* The message about apples will only be said once, while count is still 11. */
do while count < 12
 say "An apple a day keeps the doctor away."
 count = count + 1
end

請注意,如果迴圈條件永遠不會變為 false,while 迴圈將不會自行停止。它將是一個無限迴圈。這是一個常見的錯誤。

/* Just keeps on saying hello. */
do while 2 < 3
 say "Hello, world!"
end
華夏公益教科書