swift case falling through

Bilal Syed Hussain picture Bilal Syed Hussain · Jun 5, 2014 · Viewed 48.6k times · Source

Does swift have fall through statement? e.g if I do the following

var testVar = "hello"
var result = 0

switch(testVal)
{
case "one":
    result = 1
case "two":
    result = 1
default:
    result = 3
}

is it possible to have the same code executed for case "one" and case "two"?

Answer

Cezary Wojcik picture Cezary Wojcik · Jun 5, 2014

Yes. You can do so as follows:

var testVal = "hello"
var result = 0

switch testVal {
case "one", "two":
    result = 1
default:
    result = 3
}

Alternatively, you can use the fallthrough keyword:

var testVal = "hello"
var result = 0

switch testVal {
case "one":
    fallthrough
case "two":
    result = 1
default:
    result = 3
}