0Pricing
Swift Academy · Lesson

Closed and Half-Open Ranges

Use ... and ..< correctly in loops and slices.

Closed and Half-Open Ranges is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Range

A range represents a sequence of values between a lower and upper bound. Swift has two main range operators for this.

let r = 1...5
for n in r {
    print(n)
}

Closed Range

The closed range operator a...b includes both endpoints. 1...5 contains 1, 2, 3, 4, and 5.

for n in 1...5 {
    print(n)
}

Half-Open Range

The half-open range operator a..<b includes the lower bound but excludes the upper. 0..<5 contains 0, 1, 2, 3, 4.

for n in 0..<5 {
    print(n)
}

Why Half-Open Is Common

Half-open ranges pair perfectly with counts and indices. 0..<array.count visits every valid index exactly once.

let items = ["a", "b", "c"]
for i in 0..<items.count {
    print("\(i): \(items[i])")
}

Ranges Have a count

A range knows how many values it spans via its count property.

let closed = 1...5
let halfOpen = 0..<5
print(closed.count)
print(halfOpen.count)

Checking Membership

Use contains to test whether a value lies within a range.

let valid = 1...100
print(valid.contains(50))
print(valid.contains(0))
print(valid.contains(101))

Lower and Upper Bounds

Every range exposes lowerBound and upperBound properties.

let r = 10...20
print(r.lowerBound)
print(r.upperBound)

Empty and Single Ranges

A half-open range like 5..<5 is empty. A closed range cannot be empty because both ends are included.

let empty = 5..<5
print(empty.isEmpty)
let single = 5...5
print(single.count)

Ranges in switch

Ranges work as switch case patterns to classify a value into buckets.

let score = 73
switch score {
case 0..<60:
    print("Fail")
case 60...100:
    print("Pass")
default:
    print("Invalid")
}

Bounds Must Be Ordered

The lower bound must not exceed the upper bound. Writing 5...1 crashes at runtime, so keep bounds in order.

let lo = 2
let hi = 8
if lo <= hi {
    for n in lo...hi {
        print(n)
    }
}

Counting Iterations

When you only need to repeat something N times and ignore the index, use a range with _.

var total = 0
for _ in 1...4 {
    total += 10
}
print(total)

Quick Check

Test closed vs half-open ranges.

Recap: Closed and Half-Open Ranges

a...b includes both ends; a..<b excludes the upper end and pairs naturally with counts and indices. Ranges support count, contains, and bound properties.

for i in 0..<3 { print(i) }

Frequently asked questions

Is the “Closed and Half-Open Ranges” lesson free?

Yes — the full text of “Closed and Half-Open Ranges” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Closed and Half-Open Ranges”?

Use ... and ..< correctly in loops and slices. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Closed and Half-Open Ranges” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Closed and Half-Open Ranges
  2. One-Sided Ranges
  3. Counting with stride(from:to:by:)
  4. Ranges as Collections
← Back to Swift Academy