控制流 / 控制流中断

var firstMatchValue = -1
let array1 = [123]
let array2 = [234]

firstLoop: for value1 in array1 {
    for value2 in array2 {
        if value1 == value2 {
            firstMatchValue = value2
            break firstLoop
        }
    }
}
//firstMatchValue is 2
print(firstMatchValue)


If you have multiple nested loops, it can be useful to be explicit about which loop the continue or break statement should affect.

To achieve these aims, you can mark a loop statement or conditional statement with a statement label. With a conditional statement, you can use a statement label with the break statement to end the execution of the labeled statement. With a loop statement, you can use a statement label with the break or continue statement to end or continue the execution of the labeled statement.