Fallthrough Statement
Swift 4 fallthrough statementis used to simulate the behavior of Swift 4 switch to C/ C++ style switch. In Swift 4, the switch statementcompletes its execution as soon as the first matching case is completed unlike the C and C++ programming languages where falling through the bottom of subsequent cases happen.
Syntax of SwitchStatement in C/C++
In the above code, we need a break statement to come out of a case statement, otherwise the execution control will fall through the subsequent case statements available below the matching case statement.
Syntax of Switch Statement in Swift 4
In the above code, if we don’t usefallthrough statement, then the program will come out of the switch statement after executing the matching case statement.
Let’s see an example to make it clear.
Example: (Swift 4 example with fallthrough statement)
Let’s see how to use a switch statement in Swift 4 without fallthrough statement:
Example 1:
Output:
tutoraspire is an educational portal
Example 2:
Let’s see how to use a switch statement in Swift 4 programming with fallthrough statement.
Output:
This is tutoraspire tutoraspire is an educational portal