# Ranges

Ranges are a data type the specifies a list of numbers over which to execute–it's exactly what it sounds like. This is analogous to ranges in math. \[1, 2] species all numbers between 1 and 2 inclusively. The one difference from mathematical ranges is that **we must include the lower bound**. A normal, inclusive range, is written `<lower bound>...<upper bound>`. This is called a closed range. To exclude the upper bound, we write `<lower bound>..<<upper bound>`. That's looking kind of gross with my formatting so let's show some code.

```swift
let myInclusiveRange = 0...1
// myInclusiveRange: ClosedRange<Int> which is equivalent to [0, 1]

let myExclusiveRange = 0..<1
// myExclusiveRange: Range<Int> which is equivalent to [0, 1)

let myFailingRange = 1...0
// Fatal error: Range requires lowerBound <= upperBound
```

You can technically do this with both `Double`s and `Float`s, but I have never had a use for it. You will most often use ranges for loops or indexing into arrays. For this to happen, the range must conform to the `Sequence` protocol. Jargon aside, this just means that it must be easily convertible to a list of numbers in that sequence. With decimals, the program doesn't know where to draw the boundaries, so this doesn't work. Hence, ranges are most often constructed with integers.

Ranges have two more properties that may be of use: `contains` and `isEmpty`. For those that are interested or want a second explanation, you may find Apple's documentation to be helpful:

{% embed url="<https://developer.apple.com/documentation/swift/Range>" %}
Range Documentation
{% endembed %}

{% embed url="<https://developer.apple.com/documentation/swift/closedrange>" %}
Closed Range Documentation
{% endembed %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ios-course.cornellappdev.com/resources/swift-foundations/ranges.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
