Recall & Review
beginner
What does the closed range operator
... do in Swift?The closed range operator
... creates a range that includes both the start and end values. For example, 1...3 includes 1, 2, and 3.Click to reveal answer
beginner
What is the difference between
... and ..< operators?The
... operator creates a closed range including the end value, while the ..< operator creates a half-open range that excludes the end value. For example, 1...3 includes 3, but 1..<3 does not include 3.Click to reveal answer
beginner
How would you use the half-open range operator
..< in a for-loop?You can use
..< to loop from a start number up to but not including an end number. For example, for i in 0..<5 loops with i values 0, 1, 2, 3, and 4.Click to reveal answer
beginner
What happens if you use
1...1 in Swift?Using
1...1 creates a range with a single value: 1. It includes the start and end which are the same.Click to reveal answer
intermediate
Can you use range operators with types other than integers in Swift?
Yes, range operators can be used with any type that conforms to the
Comparable protocol, such as characters or floating-point numbers.Click to reveal answer
What values does the range
2...5 include?✗ Incorrect
The closed range operator
... includes both the start and end values, so it includes 2, 3, 4, and 5.Which operator excludes the end value in Swift ranges?
✗ Incorrect
The half-open range operator
..< excludes the end value.What will the loop
for i in 0..<3 print?✗ Incorrect
The loop runs from 0 up to but not including 3, so it prints 0, 1, and 2.
Is
5...5 a valid range in Swift?✗ Incorrect
A closed range with the same start and end is valid and includes that single value.
Can you create a range of characters using
... in Swift?✗ Incorrect
Range operators work with any Comparable type, including characters.
Explain the difference between the closed range operator
... and the half-open range operator ..< in Swift.Think about whether the last number is included or not.
You got /4 concepts.
Describe how you would use range operators in a Swift for-loop to iterate over numbers 1 to 5.
Remember which operator includes or excludes the end.
You got /4 concepts.