Swift Break Statement

Swift 循环Swift Loops

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

If you are using nested loops (i.e., one loop nested 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 format of the Swift break statement is as follows:

break

Flowchart:

C break 语句

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 execution is:

index 的值为 11
index 的值为 12
index 的值为 13
index 的值为 14

Swift 循环Swift Loops

Other Extensions