Swift repeat...while 迴圈
Swift repeat...while 迴圈不像 for 和 while 迴圈在循環體開始執行前先判斷條件語句,而是在迴圈執行結束時判斷條件是否符合。
語法
Swift repeat...while 迴圈的語法格式如下:
repeat { statement(s); }while( condition );
請注意,條件運算式出現在迴圈的尾部,所以迴圈中的 statement(s) 會在條件被測試之前至少執行一次。
如果條件為 true,控制流會跳轉回上面的 repeat,然後重新執行迴圈中的 statement(s)。這個過程會不斷重複,直到給定條件變為 false 為止。
數字 0, 字串 '0' 和 "", 空的 list(), 及未定義的變數都為 false ,其他的則都為 true。true 取反使用 ! 號或 not,取反後返回 false。
流程圖:

實例
import Cocoa var index = 15 repeat{ print( "index 的值為 \(index)") index = index + 1 }while index < 20
以上程式執行輸出結果為:
index 的值為 15 index 的值為 16 index 的值為 17 index 的值為 18 index 的值為 19