Swift repeat while循环

Swift repeat while循环

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

语法

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

在这个循环中,repeat while循环的主体会执行一次,然后检查testExpression。

Repeat While循环的流程图

Swift 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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程