Swift Fallthrough Statement

In this tutorial you will learn about the Swift Fallthrough Statement and its application with practical example.

Swift Fallthrough Statement

In Swift, the switch statement generally executes only the matching block, and then execution come at the end of the switch statement. Unlike many other programming languages there is no need of break keyword to stop the execution. Look at the below general syntax of a switch statement in other programming languages –

Syntax:-

Here, the switch statement generally executes only the matching block, and then execution come at the end of the switch statement as soon as it find break keyword. But, if we omit the break keywords from the cases,then all of the subsequent cases after the matching case will be executed additionally. This execution of additional cases is termed as the “fall through” behavior of switch statement.

The switch statement in swift generally executes only the matching block, and then execution come at the end of the switch statement. Look at the below general syntax of a switch statement in swift –

Syntax:-

In Swift, the fallthrough keyword allows to execute all the following statements after a matching block found.

Switch without fallthrough

Example:-

Output:-

switch_without_fallthrough

Switch with fallthrough

Example:-

Output:-

switch_with_fallthrough

In this tutorial we have learn about the Swift Fallthrough Statement and its application with practical example. I hope you will like this tutorial.