Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
let score = 5
switch score {
case 0...5:
print("low")
case 5...10:
print("high")
}It does not compile
- Expecting
lowbecause the current value5has a branch. The compiler checks every possibleInt, so aswitchon a number needsdefault. - Expecting a crash only when no branch fits. A missing branch is found by the compiler before the program runs, not while it runs.
- What does the program print once you add
default: print("other")at the end? - Why does a
switchon aStringneeddefaultas well?