var index = 30
switch index {
case 10 :
println( "Value of index is 10")
case 20 :
case 30 :
println( "Value of index is either 20 or 30")
case 40 :
println( "Value of index is 40")
default :
println( "default case")
}
Asked
Active
Viewed 650 times
0

CodeBender
- 35,668
- 12
- 125
- 132

KSR
- 1,699
- 15
- 22
-
Good example of fallthrough usage here http://stackoverflow.com/a/31782490/2303865 – Leo Dabus Aug 01 '16 at 04:39
2 Answers
5
Fallthrough is allowed in Swift, but you do have to state it explicitly:
switch index {
case 10:
println( "Value of index is 10")
case 20:
fallthrough
case 30:
println( "Value of index is either 20 or 30")
...
For your situation though, it's probably better to just group your cases:
switch index {
case 10:
println( "Value of index is 10")
case 20, 30:
println( "Value of index is either 20 or 30")
...

sam-w
- 7,478
- 1
- 47
- 77
1
Or you can write it this way (Swift 2.2 syntax):
switch index {
case 10:
print("Value is: \(index)")
case 20, 30:
print("Value is: \(index)")
default:
print("Default value is: \(index)")
}

CodeBender
- 35,668
- 12
- 125
- 132