Swift Loops

A loop statement allows the execution of a statement or group of statements multiple times. There are three types of loop statement in Swift.

  • ‘for’ loop – Useful when it is known in advance the number of times the statements within the loop need to be executed.
  • ‘while’ loop – Repeats a statement or group of statements while an expression evaluates to true. There is a condition to get into the loop, so if the condition evaluates to true at the beginning then the statements inside the loop will never execute.
  • ‘repeat while’ loop – This is similar to the ‘while’ loop except the condition is at the end, so the statements within the loop will always execute once.

‘for’ Loop

A 'for' loop in Swift can work on a range, or a collection of values, such as an array and takes the following form.

for value in range/collection
{
    // Statement(s) to execute.
}

The following example specifies a range of one to five, and displays the value of 'i', which is the current value of the range, each time through the loop.

for i in 1...5
{
    print(i)
}

The resulting output is shown below.

1
2
3
4
5

A 'for' loop can also work on a collection of values, such as an array. An array is a special type of variable, that contains multiple values of the same type.

Here, the 'for' loop is used to print out the names contained in the array.

var fnames = [ "Bob", "George", "Fred", "Alan" ]

for i in fnames
{
    print(i)
}

This displays the array of names as follows.

Bob
George
Fred
Alan

It is also possible to limit the names displayed by adding a 'where' clause to the loop.

var fnames = [ "Bob", "George", "Fred", "Alan" ]

for i in fnames where i != "George"
{
    print(i)
}

Here, the name "George" is omitted from the list displayed.

‘while’ Loop

As mentioned above, the ‘while’ loop has a condition, which must evaluate to true before the statements within it can be executed. The condition within the loop can be enclosed in parenthesis, however, by convention, these are omitted, unless they are absolutely necessary.

while condition
{
    // Statement(s) to execute.
}

In the below example, a variable is declared and initialised to one, then the ‘while’ loop checks to see that the variable is less than or equal to five before outputting it to the screen and incrementing the variable by one.

var i = 1
while i <= 5
{
    print(i)
    i += 1
}

The output will be the same as in the ‘for’ loop example above. Note that if the variable ‘i’ had been declared and initialised to six or above, then the statements within the loop would not be executed at all.

‘repeat while’ Loop

The ‘repeat while’ loop is a variation of the ‘while’ loop, where there is a condition to exit the loop rather than enter it. This means that the statements within the loop will always be executed at least once.

repeat
{
    // Statement(s) to execute.
} while condition

The example below initialises a variable to one, outputs its value and adds one to it, whilst it is less than or equal to five.

var i = 1
repeat
{
    print(i)
    i += 1
} while i <= 5

Again, the output will be the same as in the previous examples. Note that if the variable ‘i’ was initialised to six or above, it would still be output to the screen once because the condition checking whether it is less than or equal to five is at the end of the loop.