Swift Program to Iterate Over an Array


An array is used to store elements of same data type in an order. In this article, we will learn how to write a swift program to iterate over an array.

Here we use the following methods to iterate through each element of the array −

  • Using for-in loop

  • Using forEach() function

  • Using while loop

  • Using enumerated() function

Method 1: Using for-in loop

To iterate over an array we can use for-in loop.

Syntax

for value in Sequence {
   // statement
}

Here, the value parameter contains an element of the array during the iteration and the Sequence represent the array.

Example

Following Swift program to iterate over an array.

import Foundation
import Glibc

// Creating an array of string type
let names = ["Lovely", "Mukta", "Mohini", "Norma", "Eli"]

// Using for loop to iterate through each element of the array
print("Array elements are:")
for str in names {
   print(str)
}

Output

Array elements are:
Lovely
Mukta
Mohini
Norma
Eli

Here in the above code, we have an array of string type. Then use for-in loop to iterates over each element of the given array and print the elements.

Method 2: Using forEach(_:) function

We can also use forEach(_:) function to iterate through each element of the given array. forEach(_:) function calls the given closure on each element of the given array. Its working is same as for-in loop.

Syntax

func forEach(_C: (Self.Element)throws->Void)rethrows

Here, C represent a closure which takes an element of the given array as a parameter.

Example

Following Swift program to iterate over an array.

import Foundation
import Glibc

// Creating an array of integer type
let MyValues = [20, 3, 3, 4, 21, 4, 7, 10, 8, 4, 2]

// Iterate over the elements of the array
// Using forEach() function
print("Array:")
MyValues.forEach { (ele) in
   print(ele)
}

Output

Array:
20
3
3
4
21
4
7
10
8
4
2

Here in the above code, we have an array of integer type. Then we use forEach{(ele) in print(ele)} function along with closure to iterate over the elements of the given array and print them on the screen.

Method 3: Using while loop

We can also use while loop to iterate through each element of the given array.

Syntax

while(condition){
   // Statement
}

Here, condition is any expression and statement is the single or multiple block of statements. Here the while loop iterates only when the condition is true. If the condition is false, then the control passes to the statement present after the while loop.

Example

Following Swift program to iterate over an array.

import Foundation
import Glibc

// Creating an array of integer type
let MyValues = [2.0, 3.3, 3.1, 2.4, 0.21, 0.42, 1.7, 1.203, 8.9]

print("Array:")

// Iterate over the elements of the array
// Using while loop
var x = 0
while x < MyValues.count {
   print(MyValues[x])
   x += 1
}

Output

Array:
2.0
3.3
3.1
2.4
0.21
0.42
1.7
1.203
8.9

Here in the above code, we have an array of double. Then run a while loop to iterate over each elements of the MyValues array and display the m. Here, x represents the index of the elements so in each iteration the value of x increases by 1 to move to the next element. This loop continues till the end of the array.

Method 4: Using enumerated() function

We can also use enumerated() function to iterate over each element of the given array. This function return a sequence of pair(a, b), where ‘a’ represent a consecutive integer starting from zero and ‘b’ represent an element of the given array

Syntax

func enumerated()

Here, the function does not take any argument.

Example

Following Swift program to iterate over an array.

import Foundation
import Glibc

// Creating an array of integer type
let MyString = ["oo", "pp", "ee", "ww", "ss", "qq", "ll", "rr", "tt"]

print("Array elements along with their index:")

// Iterate over the elements of the array
// Using enumerated() function 
for (indexVal, ele) in MyString.enumerated() 
{
   print("Element [\(indexVal)]: \(ele)")
}

Output

Array elements along with their index:
Element [0]: oo
Element [1]: pp
Element [2]: ee
Element [3]: ww
Element [4]: ss
Element [5]: qq
Element [6]: ll
Element [7]: rr
Element [8]: tt

Here in the above code, we have an array of string type. Then we run a for loop along with enumerated() function. Here, enumerated() function return two values, that are the index value and the elements from the given array.

Conclusion

So this is how we can iterate over the array. All the discussed methods works pretty well you can use any of the method according to your requirement.

Updated on: 08-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements