Range operators help you create a sequence of numbers easily. They let you pick numbers between a start and an end without writing each one.
Range operators (... and ..<) in Swift
start...end // Closed range: includes start and end start..<end // Half-open range: includes start but excludes end
The ... operator creates a range that includes both the start and the end numbers.
The ..< operator creates a range that includes the start number but stops just before the end number.
1...51..<5
for i in 1...3 { print(i) }
for i in 1..<3 { print(i) }
This program shows how both range operators work by printing numbers from 1 to 5 using the closed range, and from 1 up to but not including 5 using the half-open range.
import Foundation // Using closed range operator print("Closed range 1...5:") for number in 1...5 { print(number) } // Using half-open range operator print("\nHalf-open range 1..<5:") for number in 1..<5 { print(number) }
Use the closed range ... when you want to include the last number in your range.
Use the half-open range ..< when you want to exclude the last number, which is useful for zero-based indexes like arrays.
Ranges work well with loops and conditions to make your code simpler and cleaner.
Range operators create sequences of numbers easily.
... includes both start and end numbers.
..< includes start but excludes end number.
Practice
1...5 represent?Solution
Step 1: Understand the closed range operator
The...operator creates a range that includes both the start and end values.Step 2: Apply to the given range
For1...5, it includes 1, 2, 3, 4, and 5.Final Answer:
A range including numbers 1, 2, 3, 4, and 5 -> Option AQuick Check:
1...5includes 5 [OK]
- Thinking ... excludes the last number
- Confusing ... with ..<
- Assuming ..< includes the end number
Solution
Step 1: Identify half-open range syntax
The half-open range operator in Swift is..<, which includes the start but excludes the end.Step 2: Apply to the range 0 to 9
To include numbers 0 through 9, use0..<10because 10 is excluded.Final Answer:
0..<10 -> Option BQuick Check:
..<excludes end [OK]
- Using ... when half-open range is needed
- Writing 0..9 which is invalid syntax
- Confusing end number inclusion
for i in 3..<6 {
print(i, terminator: ",")
}Solution
Step 1: Understand the range used in the loop
The range3..<6includes 3, 4, and 5 but excludes 6.Step 2: Trace the loop output
The loop prints 3, then 4, then 5, each followed by a comma.Final Answer:
3,4,5, -> Option CQuick Check:
..<6excludes 6 [OK]
- Including 6 in output
- Missing commas in output
- Confusing terminator usage
let range = 1..5
for num in range {
print(num)
}Solution
Step 1: Check the range operator used
The code uses1..5, which is not a valid range operator in Swift.Step 2: Identify correct range operators
Swift uses...for closed ranges and..<for half-open ranges, but not...Final Answer:
The range operator .. is invalid in Swift -> Option AQuick Check:
Invalid operator .. causes error [OK]
- Using .. instead of ... or ..<
- Assuming .. is a valid operator
- Ignoring compiler error messages
Solution
Step 1: Choose correct range excluding 15
To exclude 15, use the half-open range5..<15, which includes 5 up to 14.Step 2: Filter even numbers
The filter condition{ $0 % 2 == 0 }selects even numbers from the array.Step 3: Verify other options
let numbers = Array(5...15).filter { $0 % 2 == 0 } print(numbers) includes 15, let numbers = Array(5..15).filter { $0 % 2 == 0 } print(numbers) uses invalid operator.., and let numbers = Array(5..<14).filter { $0 % 2 == 0 } print(numbers) uses5..<14which goes only up to 13, missing the even number 14.Final Answer:
let numbers = Array(5..<15).filter { $0 % 2 == 0 } print(numbers) -> Option DQuick Check:
..< excludes end, filter selects even [OK]
- Using ... to include 15 mistakenly
- Using invalid .. operator
- Not filtering correctly for even numbers
Ready to prove it?
Pass the test and this pattern moves unknown
