Swift - Statement Break Statement

Introduction

The following code shows how to use the Break statement:

Demo

var c:Character
var index = 0/*from   www  .jav  a2  s.c  o  m*/
for c in "This is a string" {
     if c == "a" {
        break
     }
     index = index + 1
}
print("Position of 'a' is \(index)")

Result

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