in , ,

Swift for-in Loop

Swift for-in Loop
Swift for-in Loop

In this tutorial, we will learn to use for-in loop in Swift with the assistance of examples.

In computer programming, loops are used to repeat a block of code.

A for loop is an essential concept of programming. You can repeat code with a for loop, and make your code more expressive and elegant. You use for-in, while and repeat-while to loop in Swift.

For instance, assuming we need to show a message 100 times, we can use a loop. It’s simply a basic example; you can accomplish considerably more with loops.

There are 3 types of loops in Swift:

  1. for in loop
  2. while loop
  3. repeat…while loop

Swift for-in Loop

In Swift, the for-in loop is used to run a block of code a specific number of times. It is used to repeat over any sequences like an array, range, string, and so forth

The syntax of the for-in loop is:

for val in sequence{
  // statements
}

Here, val accesses each item of sequence on each iteration. Loop proceeds until we arrive at the last item in the sequence.


Example: Loop Over Array

// access items of an array 
let languages = ["Swift", "Java", "Go", "JavaScript"]

for language in languages {
      print(language)
}

Output

Swift
Java
Go
JavaScript

In the above example, we have created an array called languages.

At first, the worth of language is set to the first element of the array,i.e. Swift, so the print statement inside the loop is executed.

language is updated with the next element of the array and the print statement is executed once more. This way the loop runs until the last element of an array is accessed.


for Loop with where Clause

In Swift, we can likewise add a where clause with a for-in loop. It is used to execute filters in the loop. That is, if the condition in where the clause returns true, the loop is executed.

// remove Java from an array

let languages = ["Swift", "Java", "Go", "JavaScript"]

for language in languages where language != "Java"{
  print(language) 
}

Output

Swift
Go
JavaScript

In the above example, we have used a for-in loop to access each element of languages. Notice the use of where clause in the for-in loop.

for language in languages where language != “Java”

Here, if the condition in where clause returns true, the code inside the loop is executed. Else, it isn’t executed.

Iterationlanguagecondition: != “Java”Output
1stSwifttrueSwift is printed.
2ndJavafalseJava is not printed
3rdGotrueGo is printed
4thJavaScripttrueJavaScript is printed

for Loop With Range

A range is a progression of values between two numeric intervals. For instance,

var values = 1...3

Here, 1…3 characterizes a reach containing values 1, 2, 3.

In Swift, we can use for loop to iterate over a range. For instance,

// iterate from i = 1 to 1 = 3
for i in 1...3 {
    print(i)
}

Output

1
2
3

In the above example, we have used the for-in loop to iterate over a range from 1 to 3.

The worth of I is set to 1 and it is updated to the next number of the range on each iteration. This interaction proceeds until 3 is reached.

IterationConditionAction
1sttrue1 is printed. i is increased to 2.
2ndtrue2 is printed. i is increased to 3.
3rdtrue3 is printed. i is increased to 4.
4thfalseThe loop is terminated

for Loop with Stride Function

On the off chance that we need a loop to increase by some fixed value in each iteration, rather than range, we need to use stride(from:to:by) function. For instance,

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

Output

1
3
5
7
9

In the above example, the loop iterates over the range from 1 to 10 with an interval of 2. Be that as it may, 10 is excluded from the sequence.

Here, the value of I is set to 1 and it is updated by an interval on each iteration.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Swift switch Statement

Swift switch Statement

Swift while and repeat while Loop

Swift while and repeat while Loop