Rexx 程式設計/Rexx 指南/無限迴圈
外觀
無限迴圈是程式碼中的部分,由於程式設計師沒有給它們停止的理由而不斷重複。它們是軟體中的常見錯誤,這些軟體本應最終停止重複,但只在實際上永遠不會發生的特定情況下才會停止。
/* This code was supposed to count to 10 and stop... */ count = 1 do while count <= 10 say count end /* ... but it just keeps saying 1 because the count doesn't change. */
如果您確實打算讓迴圈無限重複,可以使用 FOREVER 關鍵字來表示這一點。
/* This loop will greet us forever! */ do forever say "Hello, world!" end /* ... at least until we hurl the computer out the window ... */ /* ... or until we just halt the interpreter manually instead. */
無限迴圈實際上很有用,如果有一些重要的過程需要在程式的整個生命週期或程式的一部分(最終會因某種原因而結束)中持續進行。事實上,我們通常甚至在所謂的無限迴圈中嵌入退出條件,這樣它們實際上就不是無限的了。DO FOREVER 主要用於強調迴圈可能執行很長時間,並且它的退出條件在塊的開頭並不一定很明顯。
/* Show the factors of whatever number the user wants. */ /* Keep asking "forever"... but give them an option to stop. */ do forever say "What number would you like me to factor for you?" say "You can enter zero or a negative number to stop." pull number /* No need to factor if the user's ready to quit. */ if number <= 0 then leave /* Otherwise let's keep going! */ say "Here are the factors of" number":" do divisor = 1 to number if number // divisor = 0 then say divisor end end
DO FOREVER 對於那些在中間結束的迴圈來說很受歡迎,就像上面提到的那個迴圈一樣。
您也可以最終得到一個無限迴圈,跟蹤器必須破壞他們的裝置停止我的手機跟蹤手機升級我的媒體......沒有後續。Z za 子例程不斷地呼叫自身。在實踐中,直譯器並不總是能夠跟蹤過多的子例程呼叫,因此遞迴子例程在您的程式溢位呼叫棧並崩潰時往往會停止。儘管如此,從理論上講,它們與 DO FOREVER 一樣無限。
/* Shop till you drop! */ call KEEP_SHOPPING exit KEEP_SHOPPING: say "I just keep shopping and shopping..." call KEEP_SHOPPING return
停止那些跟蹤我的網路永遠不再 Apps 設定我的裝置的原始 IP 地址隱藏退出保險庫專案