控制流 / 控制流中断

let numbers = [ 235711131719 ]
var str = ""
for number in numbers {
    if number > 10 {
        break
    }
    str += (str == "" ? "" : "-") + "\(number)"
}
//str is "2-3-5-7"

print("str is \(str)")


The break statement ends execution of an entire control flow statement immediately. The break statement can be used inside a switch or loop statement when you want to terminate the execution of the switch or loop statement earlier than would otherwise be the case.

When used inside a loop statement, break ends the loop’s execution immediately and transfers control to the code after the loop’s closing brace (}). No further code from the current iteration of the loop is executed, and no further iterations of the loop are started.