个性化阅读
专注于IT技术分析

Swift repeat-while循环语句用法示例

Repeat While循环与while循环相同, 但不同之处在于repeat … while循环的主体在检查测试表达式之前执行一次。

句法:

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

在此循环中, 执行一次while循环的主体, 并在测试testExpression之后执行一次。

重复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

赞(0)
未经允许不得转载:srcmini » Swift repeat-while循环语句用法示例

评论 抢沙发

评论前必须登录!