<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/ClosedRange",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SN"
  },
  "title" : "ClosedRange"
}
-->

# ClosedRange

An interval from a lower bound up to, and including, an upper bound.

```
@frozen struct ClosedRange<Bound> where Bound : Comparable
```

## Overview

You create a `ClosedRange` instance by using the closed range
operator (`...`).

```
let throughFive = 0...5
```

A `ClosedRange` instance contains both its lower bound and its
upper bound.

```
throughFive.contains(3)
// true
throughFive.contains(10)
// false
throughFive.contains(5)
// true
```

Because a closed range includes its upper bound, a closed range whose lower
bound is equal to the upper bound contains that value. Therefore, a
`ClosedRange` instance cannot represent an empty range.

```
let zeroInclusive = 0...0
zeroInclusive.contains(0)
// true
zeroInclusive.isEmpty
// false
```

## Using a Closed Range as a Collection of Consecutive Values

When a closed range uses integers as its lower and upper bounds, or any
other type that conforms to the `Strideable` protocol with an integer
stride, you can use that range in a `for`-`in` loop or with any sequence or
collection method. The elements of the range are the consecutive values
from its lower bound up to, and including, its upper bound.

```
for n in 3...5 {
    print(n)
}
// Prints "3"
// Prints "4"
// Prints "5"
```

Because floating-point types such as `Float` and `Double` are their own
`Stride` types, they cannot be used as the bounds of a countable range. If
you need to iterate over consecutive floating-point values, see the
`stride(from:through:by:)` function.

## Topics

### Creating a Range

Create a new range using the closed range operator (`...`).

[`...(_:_:)`](/documentation/Swift/Comparable/...(_:_:))

Returns a closed range that contains both of its bounds.

### Converting Ranges

[`relative(to:)`](/documentation/Swift/ClosedRange/relative(to:))

Returns the range of indices described by this range expression within
the given collection.

### Inspecting a Range

[`isEmpty`](/documentation/Swift/ClosedRange/isEmpty)

A Boolean value indicating whether the range contains no elements.

[`lowerBound`](/documentation/Swift/ClosedRange/lowerBound)

The range’s lower bound.

[`upperBound`](/documentation/Swift/ClosedRange/upperBound)

The range’s upper bound.

### Checking for Containment

[`~=(_:_:)`](/documentation/Swift/ClosedRange/~=(_:_:))

Returns a Boolean value indicating whether a value is included in a
range.

### Clamping a Range

[`clamped(to:)`](/documentation/Swift/ClosedRange/clamped(to:))

Returns a copy of this range clamped to the given limiting range.

### Comparing Ranges

[`==(_:_:)`](/documentation/Swift/ClosedRange/==(_:_:))

Returns a Boolean value indicating whether two ranges are equal.

[`!=(_:_:)`](/documentation/Swift/ClosedRange/!=(_:_:))

Returns a Boolean value indicating whether two values are not equal.

[`overlaps(_:)`](/documentation/Swift/ClosedRange/overlaps(_:)-947dt)

Returns a Boolean value indicating whether this range and the given range
contain an element in common.

[`overlaps(_:)`](/documentation/Swift/ClosedRange/overlaps(_:)-7dfep)

Returns a Boolean value indicating whether this range and the given closed
range contain an element in common.

### Manipulating Indices

[`hash(into:)`](/documentation/Swift/ClosedRange/hash(into:))

Hashes the essential components of this value by feeding them into the
given hasher.

### Describing a Range

[`description`](/documentation/Swift/ClosedRange/description)

A textual representation of the range.

[`debugDescription`](/documentation/Swift/ClosedRange/debugDescription)

A textual representation of the range, suitable for debugging.

[`customMirror`](/documentation/Swift/ClosedRange/customMirror)

The custom mirror for this instance.

### Encoding and Decoding a Range

[`encode(to:)`](/documentation/Swift/ClosedRange/encode(to:))

Encodes this value into the given encoder.

[`init(from:)`](/documentation/Swift/ClosedRange/init(from:))

Creates a new instance by decoding from the given decoder.

### Infrequently Used Functionality

[`init(uncheckedBounds:)`](/documentation/Swift/ClosedRange/init(uncheckedBounds:))

Creates an instance with the given bounds.

[`hashValue`](/documentation/Swift/ClosedRange/hashValue)

The hash value.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)