Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Swift – For-in Loop

  • Last Updated : 05 Dec, 2021

The for-in loop in Swift is very similar to for loop in Python. In Python, we write for iteration in range(). Here iteration is just a variable for iteration. In swift, also we write for iteration in range. We have collections like sets, arrays, dictionaries, so we can iterate their items using a for-in loop, which means it is used to iterate over any sequence. for-in loop and if statement is quite similar. In an if statement, the condition is used to execute a block of statements only when the condition is satisfied. Otherwise, it will not be executed. Similarly, the for-in loop is also is used to execute a block of statements if the condition satisfied. But we can repeat the block of code n number of times. Using the if condition we can’t repeat the execution of code. Generally, the for-in loop has two keywords for and in.

Syntax:

for variable_name in range{

    // body of for-in loop

}    

We can also ignore the values from the sequence using the underscore(_) in place of variable_name in the for-in loop. It doesn’t give access to the current value during the iteration of the loop. 

Syntax:

for _ in range{

    // body of for-in loop

}    

Flow diagram of the for-in loop:

Example:

Swift




// Swift program to illustrate the use of for-in loop
  
// Creating an array which contain
// the name of the courses
let course = [ "DSA", "ReactJS", "Java", "C++" ]
  
print("Courses are: ")
  
// Iterating the elements
// of course array
// Using for-in loop
for i in course{ print(i) }

Output:

Courses are: 
DSA
ReactJS
Java
C++

for-in loop with arrays 

As we know that array is used to store multiple elements of the same type. So with the help of the for-in loop, we can easily iterate all the elements present in the given array one by one.

Syntax:

for i in array_name{ print(i) }

Example:

Swift




// Swift program to illustrate
// the use of for-in loop with array
  
// Creating an array which contain
// the name of the students
let arr = [ "Karthik", "Chandu", "Nandu"
            "Komal", "Sohan", "Suresh" ]
  
print("Student names are: ")
  
// Iterating the elements
// of arr array
// Using for-in loop
for i in arr{ 
    print(i) 
}

Output:

Student names are: 
Karthik
Chandu
Nandu
Komal
Sohan
Suresh

for – in loop with arrays using where clause: We can also use where clause with for-in loop. where clause is used to filter the elements from the given sequence according to the condition specified in the where clause.

Syntax:

for i in array_name where condition {

    // Statements 

}

Example: 

Swift




// Swift program to illustrate the use
// of for-in loop with where clause
  
// Creating an array which contain
// the name of the students
let arr = [ "Karthik", "Chandu", "Nandu"
            "Komal", "Sohan", "Suresh" ]
  
print("Student names are: ")
  
// Iterating the elements of the array
// Here we apply filter that is 
// "i != nandu" using where
// clause in for-in loop
for i in arr where i != "Nandu"{
    print(i)
}

Output :

Student names are: 
Karthik
Chandu
Komal
Sohan
Suresh

Explanation: In the above example, we first create an array named “arr”, which contains the name of the student. Now using the for-in loop we are accessing the elements from the array. Here we are using the where clause also with the for-in loop to filter the elements according to the condition that is “i != Nandu” which means Nandu will not display in the output.

for – in loop with range

Using range with for loop we can repeat the statements inside the loop many numbers of times. For example, if we want to print 1 to 100 numbers simply we can specify a range in for loop and print it instead of using the print() function 100 times. 

Syntax:

for i in range{

    print(i)

}

 or 

for i in start…end{

    print(i)

}

Here the range is not an identifier. Range specifies the starting and ending of a range. For example, if we want to print 1 to 10 values have to write 1…10 in place of range. Here “…” operator act like – in general. Generally, we use 1-10 for specifying a range. Here in Swift, we use three dots “…”. 

Example:

Swift




// Swift program to illustrate the use
// of for-in loop with range
  
// Display the numbers starting from 1 to 15
print("Numbers are:")
for i in 1...15{
    print(i)
}

Output:

Numbers are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Explanation: In the above example, we will display the numbers starting from 1 to 15. So we specify a range using range operator (…) in the for-in loop. This will print the numbers starting from 1 to 15.

Sometimes we do not want a close range(contains both starting and end points). So we use a half-open range operator (..<) with a for-in loop. It includes only the lower bound but not the upper bound. Let us discuss with the help of an example:

Swift




// Swift program to illustrate the use
// of for-in loop with range
  
// Display the numbers starting from 1 to 9
print("Numbers are:")
for i in 1..<10{
    print(i)
}

Output:

Numbers are:
1
2
3
4
5
6
7
8
9

for-in loop with stride function 

The stride function is very similar to the range() function in Python programming. In python we pass start, stop, step into range function. Similarly, in Swift, we use from, to, by. The working is similar to the range() function in Python. Generally, in Python, we specify maximum value instead of specifying from and to. As for i in range(100). And the third parameter step is not mandatory to pass. But here in swift all the parameters from, to, by are mandatory. If we skip any parameter we may get an error. So we need to pass all the parameters into stride function. Like the range function in python, the stride function in swift also prints numbers upto a range of n-1. If we want to print 1 to 20 numbers using the range function, it will print up to 19 only. Likewise, stride also prints n-1 only. 

Syntax:

for i in stride(from:to:by){

    // Statements

}

Example: 

Swift




// Swift program to illustrate the use
// of for-in loop with stride function
  
// Display the numbers starting from 1 to 9
// Using stride() function
for i in stride(from:1, to:10, by:1)
{
    print(i)
}

Output: 

1
2
3
4
5
6
7
8
9

Explanation: In the above example, we use the for-in loop to print values in a specified range using the stride() function. Here, we are printing 1 to 10 values. As stride function works similarly to the range function in Python. Similar to range() function, stride() function also printed n-1 values . Here we have given a 1-10 range. We got n-1 values. That is 1-9 values as output.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!