Swift Break Statement
The Swift break statement immediately terminates the execution of the entire control flow.
If you are using nested loops (i.e., one loop inside another), the break statement will stop the execution of the innermost loop and start executing the next line of code after that block.
Syntax
The syntax for the Swift break statement is as follows:
break
Flowchart:
Example
import Cocoa
var index = 10
repeat {
index = index + 1
if index == 15 { // Terminate the loop when index equals 15
break
}
print("The value of index is \(index)")
} while index < 20
The output of the above program is:
The value of index is 11
The value of index is 12
The value of index is 13
The value of index is 14