Range Operators

Learn how to declare ranges of values using the Swift range operators.

Swift includes several useful operators that allow you to declare ranges of values. As we will see in a later lesson, these operators are invaluable when working with looping in program logic. Swift currently supports closed range, half-open, and one-sided range operators.

Closed range operator

The syntax for the closed range operator is as follows:

x…y

This operator represents the range of numbers starting at x and ending at y where both x and y are included within the range. The range operator 5…8, for example, specifies the numbers 5, 6, 7, and 8. Ranges are often used when working with loops in Swift (a topic we will cover in the “Swift Control Flow” section of the course). The following example uses the closed range operator within a for loop:

Get hands-on with 1400+ tech skills courses.