Loop Reference Guide

A loop statement allow a block of code to be executed repeatedly depending on the condition specified in the loop. Swift has 4 different types of Loops and on this page we will explain each type and give examples of when you might use them.

Swift loop types

  • for-statement
  • for-in-statement
  • while-statement
  • do-while-statement

For-Loop

The for statement loop allows a block of code to be executed while incrementing a counter.  The loop will continue as long as the condition remains true.

Structure of a for-loop

for initialization; condition; increment {

//Block of code to execute when condition is true.

}

Example of a for-loop

for var i = 0; i < 500; i++ {
    println(i)
}

How does it work?

  1. The variable "i" is created and given an initial value of 0 (loop initialization).
  2. The loop checks whether the variable "i" is less than the number 500 (loop condition).
  3. If i < 500, the condition is true and the block of code is executed.
  4. Inside the block of code, we increment the variable "i" by 1 (increment)

Once the loop cycles 500 times (i = 500) the loop condition is no longer true, so the loop terminates.

For-In Loop

Structure of a for-in Loop

for item in collection {
   //Code to execute
}

Example 1 of a for-in loop

This example loops through each value in an array. This array reads the value of an array item, in this case the item type is a String.

var arrayOfApps = ["TripList", "Station App", "RoadCam Oregon"]
for value in arrayOfApps
{
    println(value)
}

Example 2 of a for-in loop
In Swift, you can add multiple object types in your Arrays and you can just as easily cycle through them.

var arrayOfDifferentTypes = [1, 2, 3, "Jeep","VW","Porsche"]
for value in arrayOfDifferentTypes
{
    println(value)
}

How does it work?

  1. In these examples, the arrays contain a number of Strings and/or Integers.
  2. The loop cycles through each item in the array.
  3. The loop executes the block of code when an item exists.
  4. The loop terminates on completion of the array.

 

 

While Loop

The while statement allows a block of code to be executed repeatedly as long as the condition remains true. This type of loop statement is similar to a for-loop. Generally for statements are used when there is a known number of iterations and while statements when the iteration count is not known beforehand.

Structure of a while-loop

while condition {

//Block of code to execute when condition is true.

}

Example of a while-loop

var i = 0
while i < 500 {
    println(i)
    i++
}

How does it work?

  1. The variable "i" is created outside the loop and given an initial value of 0.
  2. The loop checks whether the variable "i" is less than the number 500 (loop condition).
  3. If i < 500, the condition is true and the block of code is executed.
  4. Inside the block of code, we increment the variable "i" by 1 (increment)

Once the loop cycles 500 times (i = 500) the loop condition is no longer true, so the loop terminates.

Do-While Loop
The do-while statement is similar to while-loop except that instead of evaluating the condition before code execution, this loop evaluates it afterward. This means the while-loop code block is guaranteed to be called at least once. After the code block is completed, the loop checks the condition and decides whether to carry on with further increments.

Structure of a do-while-loop

do {

//Block of code to execute when condition is true.

}while condition

Example of a while-loop

var i = 0
do {
    //Block of code to execute when condition is true.
    println(i)
    i++
}while i < 500

How does it work?

  1. The variable "i" is created outside the loop and given an initial value of 0.
  2. The loop executes the statement (code block).
  3. The loop checks whether the variable "i" is less than the number 500 (loop condition).
  4. If i < 500, the loop continues on until the condition is false.
  5. Inside the block of code, we increment the variable "i" by 1 (increment)