在本文中,您將學習在Swift編程中創(chuàng)建while和repeat... while循環(huán)。
在上一篇文章中,我們了解了for-in循環(huán)以運行一定次數(shù)的一組任務。在本文中,您將學習在迭代次數(shù)未知時使用while和repeat..while作為for-in循環(huán)的代替方法。
while循環(huán)執(zhí)行一組語句,直到條件變?yōu)閒alse為止。當?shù)谝淮蔚_始之前未知迭代次數(shù)時,最好使用此類循環(huán)。Swift提供了兩種while循環(huán):
該循環(huán)在每次通過循環(huán)的開始時評估其條件。
while循環(huán)的語法為:
while (TestExpression) { // 語句 }
TestExpression是一個布爾表達式。
如果 TestExpression 被計算為true,
while 循環(huán)內(nèi)的語句將被執(zhí)行。
并且 TestExpression 被再次計算。
繼續(xù)進行此過程,直到將 TestExpression 計算為 false。如果 TestExpression 計算結(jié)果為false,則while循環(huán)終止。
var currentLevel:Int = 0, finalLevel:Int = 5 let gameCompleted = true while (currentLevel <= finalLevel) { //玩游戲 if gameCompleted { print("你已經(jīng)通過了關卡 \(currentLevel)") currentLevel += 1 } } print("在while循環(huán)之外")
運行該程序時,輸出為:
你已經(jīng)通過了關卡 0 你已經(jīng)通過了關卡 1 你已經(jīng)通過了關卡 2 你已經(jīng)通過了關卡 3 你已經(jīng)通過了關卡 4 你已經(jīng)通過了關卡 5 在while循環(huán)之外
在上面的程序中,變量currentLevel和finalLevel被初始化為0,常量gameCompleted被初始化為true。
在while循環(huán)的每次迭代中,它都會檢查判斷條件 currentLevel <= finalLevel 。如果條件返回 true,則繼續(xù)執(zhí)行while循環(huán)內(nèi)的語句,否則終止循環(huán)。
迭代 | 條件(currentLevel <= finalLevel) | 輸出 |
---|---|---|
1 | 0 <= 5 (true) | 你已經(jīng)通過了關卡 0 |
2 | 1 <= 5 (true) | 你已經(jīng)通過了關卡 1 |
3 | 2 <= 5 (true) | 你已經(jīng)通過了關卡 2 |
4 | 3 <= 5 (true) | 你已經(jīng)通過了關卡 3 |
5 | 4 <= 5 (true) | 你已經(jīng)通過了關卡 4 |
6 | 5 <= 5 (true) | 你已經(jīng)通過了關卡 5 |
7 | 6 <= 5 (false) | 在while循環(huán)之外 |
該循環(huán)在每次循環(huán)結(jié)束時評估其條件。repeat ... while循環(huán) 與 while循環(huán)相似,但有一個關鍵區(qū)別。在檢查計算 測試表達式(testExpression) 之前,執(zhí)行一次 repeat ... while 循環(huán)主體。
repeat..while循環(huán)的語法為:
repeat { // statements ... } while (testExpression)
repeat ... while 循環(huán)的主體執(zhí)行一次(在檢查測試表達式之前)。只有這樣,testExpression才被檢查。
如果 testExpression 被計算為 true,則將執(zhí)行循環(huán)體內(nèi)的語句,然后再次對 testExpression 進行計算。一直進行到 testExpression 被計算為 false 為止。
當testExpression是false時,repeat..while循環(huán)終止。
var currentLevel:Int = 0, finalLevel:Int = 5 let gameCompleted = true repeat { //玩游戲 if gameCompleted { print("你已經(jīng)通過了關卡 \(currentLevel)") currentLevel += 1 } } while (currentLevel <= finalLevel) print("在重復while循環(huán)之外")
運行該程序時,輸出為:
你已經(jīng)通過了關卡 0 你已經(jīng)通過了關卡 1 你已經(jīng)通過了關卡 2 你已經(jīng)通過了關卡 3 你已經(jīng)通過了關卡 4 你已經(jīng)通過了關卡 5 outside of repeat while loop
在上面的示例中,循環(huán)內(nèi)的語句首次執(zhí)行。對于下一次迭代,它檢查條件currentLevel <= finalLevel。
如果條件返回true,則執(zhí)行while循環(huán)內(nèi)的語句,否則循環(huán)終止。
Iteration | Condition (currentLevel <= finalLevel) | Output |
---|---|---|
1 | 0 <= 5 (true) | 你已經(jīng)通過了關卡 0 |
2 | 1 <= 5 (true) | 你已經(jīng)通過了關卡 1 |
3 | 2 <= 5 (true) | 你已經(jīng)通過了關卡 2 |
4 | 3 <= 5 (true) | 你已經(jīng)通過了關卡 3 |
5 | 4 <= 5 (true) | 你已經(jīng)通過了關卡 4 |
6 | 5 <= 5 (true) | 你已經(jīng)通過了關卡 5 |
7 | 6 <= 5 (false) | 在重復while循環(huán)之外 |
盡管repeat 和 repeat while循環(huán)具有相同的執(zhí)行步驟,但條件 currentLevel <= finalLevel 在 repeat... while循環(huán)中,僅在執(zhí)行其中的語句之后才執(zhí)行該條件的計算。
但是在中while,currentLevel <= finalLevel 條件是在開始執(zhí)行條件之前先檢查的。
如果測試表達式永遠不會計算為false,則while的主體 和 repeat..while循環(huán)被執(zhí)行無數(shù)次。
while (true) { print("Hello, World!") }
repeat { print("Hello, World!") } while (true)
運行該程序時,輸出為:
Hello, World! Hello, World! . . .
當您運行程序時,兩個循環(huán)都將無限次執(zhí)行語句 print("Hello, World!") 。因此,您可以看到在控制臺中,連續(xù)的輸出