Swift repeat while循環
重復而循環與while循環相似,但不同之處在于,repeat…while循環的主體在檢查測試表達式之前執行一次。愛掏網 - it200.com
語法
repeat {
// statements
...
} while (testExpression)
在這個循環中,repeat while循環的主體會執行一次,然后檢查testExpression。愛掏網 - it200.com
Repeat While循環的流程圖
示例
var currentLevel:Int = 0, finalLevel:Int = 5
let gameCompleted = true
repeat {
//play game
if gameCompleted {
print("You have successfully completed level \(currentLevel)")
currentLevel += 1
}
} while (currentLevel <= finalLevel)
print("Terminated! outside of repeat while loop")
輸出:
You have successfully completed level 0
You have successfully completed level 1
You have successfully completed level 2
You have successfully completed level 3
You have successfully completed level 4
You have successfully completed level 5
Terminated! outside of repeat while loop
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。