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 10Create a variable called
closedRange using the closed range operator ... to select shelves 3 to 7Create 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