Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a switch case in Swift to continue to the next case condition? [duplicate]

In Swift, once a switch condition is reached it does implicitly "break" and get out of the switch case. In other terms it does not continues to the next condition one. How to achieve the regular behaviour as in C, C++, java, javascript etc… ?

like image 572
Flavien Volken Avatar asked Jun 16 '14 11:06

Flavien Volken


1 Answers

Taken from the Apple Swift documentation:

If you really need C-style fallthrough behavior, you can opt in to this behavior on a case-by-case basis with the fallthrough keyword. The example below uses fallthrough to create a textual description of a number:

let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += " a prime number, and also"
    fallthrough // explicitly tells to continue to the default case
default:
    description += " an integer."
}
println(description)
// prints "The number 5 is a prime number, and also an integer."
like image 76
Flavien Volken Avatar answered Nov 15 '22 21:11

Flavien Volken