Swift Program to Print an Array


This article will teach us how to write a swift program to print an array. To print an array, we are using the following methods 

  • Using array name

  • Using for loop

  • Using a while loop

  • Using forEach() method

Method 1: Print the array using the array name

We can print an array by directly using the name of the array in the print function.

Syntax

Following is the syntax −

print(arrayName)

Here we simply pass the name of the array in the print function to display the array.

Example

The following Swift program shows how to print an array using the name of the array.

import Foundation
import Glibc
 
// Creating arrays
var Array1 : [String] = ["birds", "sky", "moon", "cloud"]
var Array2 : [Int] = [73, 2, 87, 4, 9, 1]

// Printing the array directly using the name of the array
print("Array 1: ", Array1)
print("Array 2: ", Array2)

Output

Array 1:  ["birds", "sky", "moon", "cloud"]
Array 2:  [73, 2, 87, 4, 9, 1]

Here in the above code, we create two arrays of string and integer type, Now we directly use the name of the array in the print() function to display the array on the output screen.

Method 2: Print array using for loop

We can print an array by using for loop. For loop iterate through each element available in the given array and display them one by one on the output screen.

Syntax

Following is the syntax of for loop −

for variable in arrayName{
   // Statement
}

Here for the loop variable iterate through each element in the given arrayName.

Example

The following Swift program shows how to print an array using for loop.

import Foundation
import Glibc
 
// Creating arrays
var Array : [Int] = [73, 2, 87, 4, 9, 1]

// Printing the array using for loop
print("Array elements are:")
for ele in Array{
   print(ele)
}

Output

Array elements are:
73
2
87
4
9
1

Here in the above code, we create an array of integer types. Now we use a for loop to iterate through each element of the array to print on the output screen.

Method 3: Print array using while loop

To print the array we can also use a while loop. While the loop iterates through each element of the array till the given condition is true. If the given condition is false, then it will stop the iteration.

Syntax

Following is the syntax −

while booleanExpression{
   // Statement
}

Here the while loop iterates through each element till the given boolean expression is true.

Example

The following Swift program shows how to print an array using for loop.

import Foundation
import Glibc
 
// Creating an array of String
var Array : [String] = ["dance", "play", "music", "drama"]
var ele = 0

print("Array elements are:")

// Printing the array using a while loop
while (ele < Array.count){
   let result = Array[ele]
   print(result)
   ele += 1
}

Output

Array elements are:
dance
play
music
drama

Here in the above code, we create an array of string types. Now we use a while loop to iterate through each element of the array till the condition (ele < array.count) is true and also in each iteration the value of the ele variable is increased by one to move to the next element of the array.

Method 4: Print the array using the forEach() method

To print an array we can also use the forEach() function. This function calls the specified closure on all the elements present in the given array. This method does not skip any element of the given array or we can say you can not get out from the forEach() method without processing all the elements of the given array.

Syntax

Following is the syntax −

func forEach(_closure:(self.element)throws ->Void)rethrows

Here closer take the elements of the given sequence as a parameter.

Example

The following Swift program shows how to print an array using the forEach() method.

import Foundation
import Glibc
 
// Creating an array of Double
var Array : [Double] = [2.3, 56.3, 5.6, 2.1, 89.4, 3.4]

print("Array elements are:")

// Printing the array using forEach() method
Array.forEach{print($0)}

Output

Array elements are:
2.3
56.3
5.6
2.1
89.4
3.4

Here in the above code, we create an array of double types. Now we use the forEach() function to display the array. Here the iteration starts from index 0 because we use $0 if we use $1, then the index iteration starts from index 1.

Conclusion

So this is how can easily print an array using either, the array name, for loop, while loop, or forEach() method. All the methods work pretty well with the array so you can use them according to your requirement.

Updated on: 20-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements