Control Transfer Statements

Continue, Break and Fallthrough in Swift

Tested on: Xcode 12.3, Swift 5.3

Continue, Break and Fallthrough allows you to change the order in of your code in a For-Loop or/and Switch. They are also know as control transfer statement. However, break and throw are also part of control transfer statement which are not cover in this reference. Return will be cover in functions while throw will be cover in Error Handling.

  1. Break
  2. Continue
  3. Fallthrough





1. Break

Swift Code :

let numbers = [1, 2, 3, 4]

for i in numbers {
   if i > 2{
       break
   }

   print(i)
}

Output :

1

2




2. Continue

Swift Code :

let numbers = [1, 2, 3, 4]

for i in numbers {
   if i == 2{
       continue
   }

   print(i)
}

Output :

1

3

4




3. Fallthrough

Swift Code :

let postiveNumber = 10

switch postiveNumber {
case 0...99:
   print("Less than 100")
   fallthrough
case 100...201:
   print("100 to 200")
default:
   print("Is a number")
}

Output :

Less than 100

Is a number


Swift Code :

let postiveNumber = 190

switch postiveNumber {
case 0...99:
   print("Less than 100")
   fallthrough
case 100...201:
   print("100 to 200")
default:
   print("Is a number")
}

Output :

100 to 200

Is a number