Swift For in Loop

In this tutorial you will learn about the Swift For in Loop and its application with practical example.

Swift For in Loop

The for in loop takes a collection of data(arrays, dictionaries, sets , or anything that provides an iterator) and iterate through the items one at a time in sequence.

Swift For In Loop Flow Diagram

swift-for-in-loop-flowchart-dagram

Syntax:-

Here, items provides an iterator that allows us to fetch each of the single element from the given collection, and item hold the the current element fetched from the items.

Iterating Over Range Using For In Loop

In Swift, we can loop through the range elements using for-in loop as following –

Example:-

Output:-

swift_iterating_range_using_for_in_loop

Iterating Over an Array Elements Using For In Loop

In Swift, we can loop through the array elements using for-in loop as following –

Example:-

Output:-

swift_iterating_array_elements

Iterating Over a Dictionary Using For In Loop

In Swift, we can loop through the dictionary elements using for-in loop as following –

Example:-

Output:-

swift_iterating_dictionary_elements

Swift Iterating Over a Set Using For In Loop

In Swift, we can loop through the set elements using for-in loop as following –

Example:-

Output:-

swift_iterating_set

In this tutorial we have learn about the Swift For in Loop and its application with practical example. I hope you will like this tutorial.