Question:

What output will be produced by the code below?

let number = 5

switch number {
case 0..<5:
    print("First group")
case 5...10:
    print("Second group")
case 0...5:
    print("Third group")
default:
    print("Fourth group")
}

Correct answer: "Second group".

Explanation: Although there are overlapping ranges in this code, Swift will simply find and execute the first one that matches. The first case, 0..<5, will not be matched because it uses the half-open range operator, which excludes the upper bound.


Keywords:

© 2017 QuizBucket.org