Swift Half-Range Operator

Swift Half-Range Operator

Swift Half-Range Operator is used to define a range of numbers. An half-range starts from first value, and goes up to but does not include the last value.

To define a half-range, specify starting value, then two period (dot) characters, then less than symbol, followed by last value of the range.

The syntax to define a half-range from a to b is

</>
Copy
a..<b

The values of this range would be a, a+1, a+2, .., b-2, b-1.

Example

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

main.swift

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

Output

Swift Half-Range Operator - Example

Conclusion

In this Swift Tutorial, we learned what a half-range operator is in Swift, and how to define an half-range with example.