Back to questions
JuniorTheoryCommon
What does the program print?
let n = 5
let a = (1..<5).contains(n) ? "in" : "out"
let b = (1...5).contains(n) ? "in" : "out"
print(a, b)out in
- Expecting
in in: reading..<as including its upper bound.1..<5holds the numbers 1 to 4, socontains(5)isfalseandaisout. - Expecting
out out, as in languages where a range never includes its end: in Swift the closed range1...5includes 5.
- How many numbers does
0..<5hold, and which ones? - When is
..<more convenient than...?