Swift For Loop

For Loop in Swift

Tested on: Xcode 12.2, Swift 5.3

A for loop is used to iterate over a sequence of elements. A sequence of elements includes Array, Dictionary and Set which you have learnt in the previous few references. However, you can iterate over a range of number which you be learning soon.

Topics:

  1. How to create a For Loops? (Looping through an Array)
  2. looping through Coutable Ranges
  3. Looping through Dictionary
  4. Looping through Set
  5. forEach





1. How to create a For Loops? (Looping through an Array)

A For Loop contains of for keyword followed by a constant name which you can give any name you want. Then, it also consists of a in keyword followed by an iterable object such as an Array.

Below is the syntax of a for loop:

Swift Code :

for constant in iterableObject {

}

Looping through an Array

i is the constant name that we will be using to access each element while the array of number is used as the iterableObject. For example, the code below. At the first iteration, i = 1 and then i is multiplied by 2 and then the result is printed out. Once finished executing the code inside the for loop, i will then become 2 and then multiply by 2 and so on.

  • Loop 1: i = 1, print( 1 * 2 )
  • Loop 2: i = 2, print( 2 * 2 )
  • Loop 3: i = 3, print( 3 * 2 )
  • Loop 4: i = 4, print( 4 * 2 )

Swift Code :

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

for i in numbers {
   print(i * 2 )
}

Output :

2

4

6

8




2. Looping through Coutable Ranges

Range

Before learning how to loop through a range of value, you need to know what range is. You can learn more about range over here. A range allows you to produce an array of values. However, they do not have a data type of array but their data type is a range that is still a iterableObject. For example, 0..<5 will produce values from 0 to 5 but not including 5.

Swift Code :

let rangeInt = 0..<5

Looping through Coutable Ranges

Since the range is an iterableObject, you can apply the for loop syntax to loop through each value. In this way, you do not need to manually type out the range of value like what we did above.

Swift Code :

for i in 1..<5 {
   print(i * 2 )
}

Output :

2

4

6

8




3. Looping through Dictionary

When looping a dictionary, dictionary is deconstructed into its keys and values. Therefore, when looping it, you expect it to give you back the key and values.

Swift Code :

let numbers = [
   "prime": [2, 3, 5, 7, 11],
   "Fibonacci": [1, 1, 2, 3, 5]
]

for (key, values) in numbers {
   print("key: \(key), values: \(values)")
}

Output :

key: prime, values: [2, 3, 5, 7, 11]

key: Fibonacci, values: [1, 1, 2, 3, 5]




4. Looping through Set

As you know from Set Reference, set does not store the elements in order. Therefore, when looping through the set, do not expect it to follow the order you have created the set.

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])

for value in ingredients {
   print(value)
}

Output :

Pepper

Chicken

Tomato

Garlic

Chicken




5. forEach

forEach is a shortened way of writing a loop. This makes your code cleaner. The { }at the end is called a closure. However, if you don't understand it. You can just apply the forEach code like what you did above. When reading into intermediate of Swift Reference, you will start learning deep into closure.

Swift Code :

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

numbers.forEach { number in
   print(number)
}

Output :

1

2

3

4