0
0
Swiftprogramming~15 mins

Range operators (... and ..<) in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Range Operators (... and ..<) in Swift
📖 Scenario: You are organizing a small library and want to list books by their shelf numbers. You will use Swift's range operators to select shelves.
🎯 Goal: Learn how to use the closed range operator ... and the half-open range operator .. to select ranges of shelf numbers.
📋 What You'll Learn
Create an array called shelves with shelf numbers from 1 to 10
Create a variable called closedRange using the closed range operator ... to select shelves 3 to 7
Create a variable called halfOpenRange using the half-open range operator ..< to select shelves 3 to 7 (excluding 7)
Print the selected shelves for both ranges
💡 Why This Matters
🌍 Real World
Range operators help select parts of lists or sequences, like choosing shelves in a library or pages in a book.
💼 Career
Understanding range operators is useful for filtering data, looping through parts of collections, and writing clear, concise code in Swift.
Progress0 / 4 steps
1
Create the shelves array
Create an array called shelves with the numbers from 1 to 10.
Swift
Need a hint?

Use Array(1...10) to create an array with numbers from 1 to 10.

2
Create the closed range variable
Create a variable called closedRange using the closed range operator ... to select shelves from 3 to 7.
Swift
Need a hint?

Use shelves[2...6] to select shelves from index 2 to 6 inclusive.

3
Create the half-open range variable
Create a variable called halfOpenRange using the half-open range operator ..< to select shelves from 3 to 7, excluding 7.
Swift
Need a hint?

Use shelves[2..<6] to select shelves from index 2 up to but not including 6.

4
Print the selected shelves
Print the variables closedRange and halfOpenRange to show the selected shelves.
Swift
Need a hint?

Use print() with string interpolation to show the arrays.