Swift Closed Range Operator

Swift Closed Range Operator is used to define an inclusive range of numbers. A closed range starts from first value, and goes up to and includes the last value.

To define a closed range, specify starting value, then three consecutive period (dot) characters, followed by last value in the range.

The syntax to define a closed range from a to b is

</>
Copy
a...b

Example

In the following example, we will iterate over a closed range [4, 9] and print each value.

main.swift

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

Output

Swift Closed Range Operator

Conclusion

In this Swift Tutorial, we learned what a closed range operator is in Swift, and how to define a closed range in Swift.