0
0
Swiftprogramming~5 mins

Range operators (... and ..<) in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A2, 3, 4
B2, 3, 4, 5
C3, 4, 5
D2, 3, 5
Which operator excludes the end value in Swift ranges?
A..<
B...
C=>
D<..
What will the loop for i in 0..<3 print?
A0 1 2
B0 1 2 3
C1 2 3
D1 2
Is 5...5 a valid range in Swift?
ANo, ranges must have at least two values
BNo, start and end cannot be equal
CYes, it includes only 5
DYes, it includes 5 and 6
Can you create a range of characters using ... in Swift?
ANo, ranges only work with numbers
BOnly with strings, not characters
COnly with integers converted to characters
DYes, if characters are comparable
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.