Swift – Iterate over a Range

To iterate over a range of values, we can use for-in loop.

The syntax to iterate over a range of values using for-in loop is

</>
Copy
for value in <range> {
    //code
}

Example

In the following example, we will iterate over a range of numbers [1,9] using for-in loop and print them in the for loop body.

main.swift

</>
Copy
for value in 1...9 {
    print(value)
}

Output

Swift - Iterate over a Range of Values

Conclusion

In this Swift Tutorial, we learned how to iterate over a range of values in Swift, using for-in loop.