For Loop in Swift


In this tutorial, you are going to learn about for loop in Swift. In Swift, it is also known as the for-in loop.

After following this tutorial, you will learn various types of for-in loops and how to use them with examples.

A loop is used to perform a specific task again and again. It will run a block of code a specific number of times.

For loop is a common and popular one in all other programming languages and there is no exception in the case of Swift.

Why use for-in loop?

A for or for-in loop is used to repeat and run a block of code a specific number of times. it will help to make the program more expressive and elegant.

For example, if you want to print a message 1000 times, then you can easily do it using a for a loop. Well, it is just a simple example. You can do even more with a for-in loop in Swift.

Another example is if you have a list of emails and you want to send messages to each email programmatically, then it will be almost impossible to send emails one by one if there are thousands of emails.
If you try to do it, it will lot of time-consuming work. In that case, you can do this task using for loop. For loop will help you to send email messages to thousands of emails with just a few lines of code.

I hope, you have understood why we use for loops not only in Swift but also in many other programming languages.

Swift for-in loop

In Swift programming, a for-in loop is used to run a block of code for a specific number of times. For example, you can run the same code 100 times or even 1000 times with a for-in loop.

This can be applied over arrays or in ranges for looping the items.

The syntax for the for-in loop is given below:

for item in sequence{
   // Code Block
}

Here the item define each item of the element in the sequence. The loop will continue until it will access the last item of sequence.

Below is another syntax for better understanding:

for student in students{
   // Code Block
}

In the above syntax, we can get each student in the student literate from the sequence students that contains all the students of a class.

Diagram of for loop

Let’s try to understand it with a simple diagram…

Below is the diagram of for loop:

Diagram of For Loop

From the above diagram, you can understand if the loop has not reached the last item, it will go to the body of the loop to run the code again.

If it reaches the end item then it will exit the loop.

Examples of Swift For loop

The for-in loop can be used in Swift in many ways. Here you are going to see some popular uses of the for loop.

You can use this loop over a range, on an array and so on…

So let’s continue and see some examples where you can see some popular ways of using the for-in loop…

For loop with range

You can set a range to specify between which the loop will work. Basically, a range is the values between two numeric intervales.

For example, 1…7 defines the range that is containing values 1, 2, 3, 4, 5, 6, and 7.

Now see the example below:

for i in 0...7 {
    print(i)
}

Run this code online

After we run the above program, we will see the output result given below:

0
1
2
3
4
5
6
7

For-in Loop on an array

We can get each item of an array using the for-in loop. It is a widely used and popular way of getting each array item.

let fruits = ["Apple", "Grape", "Blueberries", "Lychee", "Pineapple", "Strawberry"]

for fruit in fruits {
    print(fruit)
}

Run this code online

Output:

Apple
Grape
Blueberries
Lychee
Pineapple
Strawberry

In the above example, we have an array that contains the names of some fruits. We use a for-in loop to list each fruit.

Using where clause in for-in loop

In Swift programming, we can also use the where clause with the for-in loop. The main purpose of using the where clause in the loop is to implement a filtering feature that will omit the specific items.

Now see the example below:

let fruits = ["Apple", "Grape", "Blueberries", "Strawberry"]

for fruit in fruits where fruit != "Blueberries" {
    print(fruit)
}

Run this code online

Output:

Apple
Grape
Strawberry

In the above example, we have used the where clause and get the items that do not contain Blueberries.

Stride function in the for loop

You can use the stride function with the for-in loop. It is used for increment purposes by a fixed value in each iteration.

Look at the following syntax:

stride(from:to:by)

In the above syntax, from is the starting value, to is the end item value, and by is the fixed value by which the loop will increase by for stepping.

For example, if you want to apply stride function with for loop between 1 to 10 then it will list items like 1, 3, 5, 7, and 9.

Let’s implement the example in the Swift program:

for i in stride(from: 1, to: 10, by: 2) {
    print(i)
}

Run this code online

Output:

1
3
5
7
9