How to Control Transfer Statements in SWIFT

There are four ways to control transfer statements in Swift, and these are:

continue

break

fallthrough

return

and of course, labelled statements

This tutorial will teach you how to do control transfer statements using swift, what these transfer statements are used for and what are its limitations.

A break, for example, breaks the loop and goes to the last.

Continue, restarts the loop, take this code for example:

   1:  var numbers = [1,2,3,4,5]
   2:  var oddNumbers = Int[]()
   3:  var evenNumbers = Int[]()
   4:  
   5:  for number in numbers {
   6:      if (number%2 == 1) {
   7:          oddNumbers.append(number)
   8:      } else {
   9:          continue
  10:          evenNumbers.append(number)
  11:      }
  12:  }
  13:  
  14:  oddNumbers
  15:  evenNumbers

This code simply finds all Odd numbers. All the even numbers array will be empty.

Read the rest of the tutorial here: http://blah.winsmarts.com/2014-6-Control_transfer_statements_in_SWIFT.aspx

Leave a Reply

Your email address will not be published. Required fields are marked *