let firstNumber = 1
var numbers = ""
switch firstNumber {
case 1:
numbers = "1"
fallthrough
case 2:
numbers += "-2"
fallthrough
case 3:
numbers += "-3"
fallthrough
default:
numbers += ";"
}
//numberList is "1-2-3;"
print("numbers is '\(numbers)'")
| In Swift, switch statements don’t fall through the bottom of each case and into the next one. That is, the entire switch statement completes its execution as soon as the first matching case is completed. By contrast, C requires you to insert an explicit break statement at the end of every switch case to prevent fallthrough. Avoiding default fallthrough means that Swift switch statements are much more concise and predictable than their counterparts in C, and thus they avoid executing multiple switch cases by mistake. If you need C-style fallthrough behavior, you can opt in to this behavior on a case-by-case basis with the fallthrough keyword. |