Swift while 和 repeat...while 循環(huán)

在本文中,您將學(xué)習(xí)在Swift編程中創(chuàng)建while和repeat... while循環(huán)。

在上一篇文章中,我們了解了for-in循環(huán)以運(yùn)行一定次數(shù)的一組任務(wù)。在本文中,您將學(xué)習(xí)在迭代次數(shù)未知時(shí)使用while和repeat..while作為for-in循環(huán)的代替方法。

while循環(huán)執(zhí)行一組語句,直到條件變?yōu)閒alse為止。當(dāng)?shù)谝淮蔚_始之前未知迭代次數(shù)時(shí),最好使用此類循環(huán)。Swift提供了兩種while循環(huán):

1. Swift While循環(huán)

該循環(huán)在每次通過循環(huán)的開始時(shí)評(píng)估其條件。

while循環(huán)的語法為:

while (TestExpression) {
    // 語句
}

while 循環(huán)如何工作?

TestExpression是一個(gè)布爾表達(dá)式。

如果 TestExpression 被計(jì)算為true,

  • while 循環(huán)內(nèi)的語句將被執(zhí)行。

  • 并且 TestExpression 被再次計(jì)算。

繼續(xù)進(jìn)行此過程,直到將 TestExpression 計(jì)算為 false。如果 TestExpression 計(jì)算結(jié)果為false,則while循環(huán)終止。

While循環(huán)流程圖

Swift While循環(huán)流程圖

示例1:While循環(huán)

var currentLevel:Int = 0, finalLevel:Int = 5
let gameCompleted = true
while (currentLevel <= finalLevel) {
    //玩游戲
    if gameCompleted {
        print("你已經(jīng)通過了關(guān)卡 \(currentLevel)")
        currentLevel += 1
    }
}
print("在while循環(huán)之外")

運(yùn)行該程序時(shí),輸出為:

你已經(jīng)通過了關(guān)卡 0
你已經(jīng)通過了關(guān)卡 1
你已經(jīng)通過了關(guān)卡 2
你已經(jīng)通過了關(guān)卡 3
你已經(jīng)通過了關(guān)卡 4
你已經(jīng)通過了關(guān)卡 5
在while循環(huán)之外

在上面的程序中,變量currentLevel和finalLevel被初始化為0,常量gameCompleted被初始化為true。

在while循環(huán)的每次迭代中,它都會(huì)檢查判斷條件 currentLevel <= finalLevel 。如果條件返回 true,則繼續(xù)執(zhí)行while循環(huán)內(nèi)的語句,否則終止循環(huán)。

執(zhí)行步驟
迭代條件(currentLevel <= finalLevel)輸出
10 <= 5 (true)你已經(jīng)通過了關(guān)卡 0
21 <= 5 (true)你已經(jīng)通過了關(guān)卡 1
32 <= 5 (true)你已經(jīng)通過了關(guān)卡 2
43 <= 5 (true)你已經(jīng)通過了關(guān)卡 3
54 <= 5 (true)你已經(jīng)通過了關(guān)卡 4
65 <= 5 (true)你已經(jīng)通過了關(guān)卡 5
76 <= 5 (false)在while循環(huán)之外

2.重復(fù)while循環(huán)

該循環(huán)在每次循環(huán)結(jié)束時(shí)評(píng)估其條件。repeat ... while循環(huán) 與 while循環(huán)相似,但有一個(gè)關(guān)鍵區(qū)別。在檢查計(jì)算 測(cè)試表達(dá)式(testExpression) 之前,執(zhí)行一次 repeat ... while 循環(huán)主體。

repeat..while循環(huán)的語法為:

repeat {
    // statements
    ...
} while (testExpression)

repeat ... while循環(huán)如何工作?

repeat ... while 循環(huán)的主體執(zhí)行一次(在檢查測(cè)試表達(dá)式之前)。只有這樣,testExpression才被檢查。

如果 testExpression 被計(jì)算為 true,則將執(zhí)行循環(huán)體內(nèi)的語句,然后再次對(duì) testExpression 進(jìn)行計(jì)算。一直進(jìn)行到 testExpression 被計(jì)算為 false 為止。

當(dāng)testExpression是false時(shí),repeat..while循環(huán)終止。

repeat ... while循環(huán)的流程圖

快速重復(fù)..while循環(huán)流程圖

示例2:repeat ... while 循環(huán)

var currentLevel:Int = 0, finalLevel:Int = 5
let gameCompleted = true
repeat {
    //玩游戲
    if gameCompleted {
        print("你已經(jīng)通過了關(guān)卡 \(currentLevel)")
        currentLevel += 1
    }
} while (currentLevel <= finalLevel)
print("在重復(fù)while循環(huán)之外")

運(yùn)行該程序時(shí),輸出為:

你已經(jīng)通過了關(guān)卡 0
你已經(jīng)通過了關(guān)卡 1
你已經(jīng)通過了關(guān)卡 2
你已經(jīng)通過了關(guān)卡 3
你已經(jīng)通過了關(guān)卡 4
你已經(jīng)通過了關(guān)卡 5
outside of repeat while loop

在上面的示例中,循環(huán)內(nèi)的語句首次執(zhí)行。對(duì)于下一次迭代,它檢查條件currentLevel <= finalLevel。

如果條件返回true,則執(zhí)行while循環(huán)內(nèi)的語句,否則循環(huán)終止。

執(zhí)行步驟
IterationCondition (currentLevel <= finalLevel)Output
10 <= 5 (true)你已經(jīng)通過了關(guān)卡 0
21 <= 5 (true)你已經(jīng)通過了關(guān)卡 1
32 <= 5 (true)你已經(jīng)通過了關(guān)卡 2
43 <= 5 (true)你已經(jīng)通過了關(guān)卡 3
54 <= 5 (true)你已經(jīng)通過了關(guān)卡 4
65 <= 5 (true)你已經(jīng)通過了關(guān)卡 5
76 <= 5 (false)

在重復(fù)while循環(huán)之外

盡管repeat 和 repeat while循環(huán)具有相同的執(zhí)行步驟,但條件 currentLevel <= finalLevel 在 repeat... while循環(huán)中,僅在執(zhí)行其中的語句之后才執(zhí)行該條件的計(jì)算。

但是在中while,currentLevel <= finalLevel 條件是在開始執(zhí)行條件之前先檢查的。

3.無限while循環(huán)

如果測(cè)試表達(dá)式永遠(yuǎn)不會(huì)計(jì)算為false,則while的主體 和 repeat..while循環(huán)被執(zhí)行無數(shù)次。

示例3:無限while循環(huán)

while (true) {
   print("Hello, World!")
}

 

repeat {
   print("Hello, World!")

} while (true)

運(yùn)行該程序時(shí),輸出為:

Hello, World!
Hello, World!
.
.
.

當(dāng)您運(yùn)行程序時(shí),兩個(gè)循環(huán)都將無限次執(zhí)行語句 print("Hello, World!") 。因此,您可以看到在控制臺(tái)中,連續(xù)的輸出“Hello, World!”。

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清