Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Swift Break Statement

Swift Break Statement

Swift loopsSwift Loops

The Swift break statement immediately ends the execution of the entire control flow.

If you are using nested loops (i.e., one loop inside another), the break statement stops executing the innermost loop and then begins executing the next line of code after that block.

Syntax

The syntax of the Swift break statement is as follows:



break

Flow diagram:

Example



import Cocoa



var index = 10



repeat{

    index = index + 1

    

    if( index == 15 ){  // index 等于 15 时终止循环

        break

    }

    print( "index 的值为 \(index)")

}while index < 20

The output of the above program is:



index 的值为 11

index 的值为 12

index 的值为 13

index 的值为 14

Swift loopsSwift Loops