Swift - Statement Break Statement


The following code shows how to use the Break statement:


var c:Character
var index = 0//from  ww w .j a v a  2 s . c  om
for c in "This is a string" {
     if c == "a" {
     index = index + 1
print("Position of 'a' is \(index)")


Here, you use the Break statement to end the loop the moment the character you are looking for is found.

When you use the Break statement within a loop such as a For, While, or Do-While loop, the control is transferred to the first line of code after the closing brace }.

Related Topics