Rexx 程式設計/Rexx 教程/for 迴圈
外觀
“for” 迴圈也被稱為“定值迴圈”。它們會重複執行同一程式碼塊多次後停止。在很多其他語言中,它們使用單詞 “for” 進行編碼,但在 Rexx 中,它們出現在關鍵字 DO 和 END 之間,就像其他程式碼塊一樣。
/* Greet the world 10 times: */ do 10 say "Hello, world!" end
如果我們希望程式碼每次執行時執行略有不同的操作,我們可以使用一個變數來跟蹤計數。
/* Count from 1 to 10: */ do count = 1 to 10 say count end
Rexx 還可以跳過 2、3、4 等數量進行計數。
/* Count by 2s */ do count = 0 to 8 by 2 say count end
如果你不想指定最後一個數字,而只想要指定目標計數次數,Rexx 確實有一個 FOR 關鍵字可供你使用。
/* Count out 5 numbers starting at -2 */ do count = -2 for 5 say count end
你還可以將定值迴圈巢狀在彼此內部,以實現兩層或更多層的重複。
/* Make a nicely formatted multiplication table. */ do row# = 1 to 10 output = "" do col# = 1 to 10 product = row# * col# output = output || right(product, 4) end say output end