Swift for Loop
In Swift, you can repeat code execution (looping) using a for-in loop, a while loop, or a repeat-while loop.
Here, we'll go over the for-in loop in Swift.
Basics of the for-in Loop in Swift
The for-in loop in Swift lets you iterate over a range of numbers, arrays, dictionaries, strings, and more, executing some code for each element in sequence.
The basic syntax looks like this:
for [constant for the current value] in [range, array, dictionary, etc.] {
Code block to execute during each iteration
}
Let's go through some examples of how to use the for-in loop.
Looping Over a Range of Numbers
First, let's loop over a numeric range like 1 through 5 using a for-in loop.
You specify the range with the range operator, like 1...5.
for i in 1...5 {
print("i = \(i)")
}
The result will look like this, with print executed five times and i taking values from 1 to 5:
i = 1
i = 2
i = 3
i = 4
i = 5
Looping Through an Array
Next, let's loop through an array using for-in.
Here's how you can print all elements in the array:
let names = ["Smith", "Williams", "Miller", "Brown"]
for name in names {
print(name)
}
The result will print each element in order:
Smith
Williams
Miller
Brown
Looping Through a Dictionary
Now let's loop through a dictionary using for-in.
To print all keys and values, you can do this:
By specifying a tuple like (studentID, name), the loop assigns the current dictionary key to studentID and the value to name.
let students = ["S001": "Brown", "S002": "Williams", "S003": "Smith"]
for (studentID, name) in students {
print("\(studentID): \(name)")
}
The result will show all key-value pairs:
S002: Williams
S001: Brown
S003: Smith
Note that arrays in Swift preserve element order, but dictionaries do not. The loop order of dictionary elements does not necessarily match the insertion order.
Using continue in a for-in Loop
Inside a for-in loop, you can use the continue statement to skip the remaining code in the current iteration and move to the next one.
For example, if you want to loop from 1 to 10 but skip numbers divisible by 2, you can do this:
for i in 1...10 {
if i % 2 == 0 {
continue
}
print("i = \(i)")
}
The execution result will be as follows, and when i % 2 is 0, the code after that will not be executed and the next element will be displayed, so only odd numbers will be output.
i = 1
i = 3
i = 5
i = 7
i = 9
Using break in a for-in Loop
You can use the break statement to exit a for-in loop early.
For example, to stop looping once the element "Miller" is found in an array:
let names = ["Smith", "Williams", "Miller", "Brown"]
for name in names {
if name == "Miller" {
break
}
print(name)
}
The result stops looping once "Miller" is reached:
Smith
Williams
Using where with for-in Loops
You can add a where clause to a for-in loop to execute the code block only when a condition is true.
Earlier we used continue to print only odd numbers from 1 to 10. With where, you can write it like this:
for i in 1...10 where i % 2 != 0 {
print("i = \(i)")
}
The output is as follows. The print is executed only when i % 2 != 0 is true.
i = 1
i = 3
i = 5
i = 7
i = 9
That wraps up our explanation of the for-in loop in Swift.