Reputation: 655
In the following code sample, I don't understand why "Value 7" gets printed instead of "Default". Case 7, i.e. x=7 is not met because x=6, so why does "Value 7" get printed instead of being skipped and the execution falling through to default?
let x = 6
switch x {
case 0...5:
println("0 through 5")
case 6:
fallthrough
case 7:
println("Value 7")
default:
println("Default")
}
Upvotes: 2
Views: 1660
Reputation: 4729
When x
= 6 the switch statements finds the case
for 6 then it 'falls through' to the next case not the default one. If you want the case
for 6 to execute the code under default
remove that case
as it does nothing. If you plan to add some code to the case
for 6 then make it the last one before default
so it falls through to the place you want.
Upvotes: 5