Swift Fallthrough statement
Swift fallthrough
statement let case
subsequent statements continue to run sequentially, regardless of whether the conditions are met or not.
The switch in Swift is not from the previous case
the branch falls intothe next case
in the branch. As long as the first match case
the branch completes the statement it needs to execute, the entire switch
the code block completes its execution.
Note: in most languages switch
in the sentence block case
keep up with it break
otherwise, case
subsequent statements are run sequentially, but in the Swift language, they are not executed by default switch
and it will stop. If you want to let in Swift case
if the subsequent statements continue to run in order, you need to use the fallthrough
statement.
Grammar
Swift fallthrough
the syntax format of the statement is as follows:
fallthrough
Generally in switch
statement does not use the fallthrough
statement.
Example 1
The following examples are not used fallthrough
statement:
import Cocoa
var index = 10
switch index {
case 100 :
print( "The value of index is 100")
case 10,15 :
print( "The value of index is 10 or 15")
case 5 :
print( "iThe value of index is 5")
default :
print( "default case")
}
When the above code is compiled and executed, it produces the following results:
The value of index is 10 or 15
Example 2
The following examples use the fallthrough
statement:
import Cocoa
var index = 10
switch index {
case 100 :
print( "The value of index is 100")
fallthrough
case 10,15 :
print( "The value of index is 10 or 15")
fallthrough
case 5 :
print( "The value of index is 5")
default :
print( "default case")
}
When the above code is compiled and executed, it produces the following results:
The value of index is 10 or 15
The value of index is 5