Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Swift Fallthrough: An Introduction

DZone's Guide to

Swift Fallthrough: An Introduction

If you're not used to Swift's fallthrough statement for switch cases, then this brief summary will give you a sense of how they work.

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

Swift switch statements are extremely useful and powerful conditionals. They allow you to slim down your code by avoiding repetitive if-else statements or nested ifs, and their pattern matching capabilities are extremely robust. But, while Swift’s switch works much the same as C’s, there are some variations. One such variation is the fact that it is unnecessary to break out of a case in a Swift switch; the switch statement breaks out of the first case that is matched. This means there is no “implicit fallthrough” in Swift switch statements; it makes it harder to accidentally execute on multiple cases.

If you want your switch statement to fall through cases, Swift offers the fallthrough statement. This statement forces the execution of the next case’s code block. It’s important to note that the condition of the next case does not matter; the code in that case will execute whether the condition has been met or not. Consider the example:

var whatever = 100

switch whatever {
    case 1...1000:
        print("Within 1-1000")
        fallthrough
    case 1...100:
        print("Within 1-100")
        fallthrough
    case 1...10:
        print("Within 1-10")
        fallthrough
    default:
        print("Out of range")
}

Here, whatever, with a value of 10, matches the first range case of 1-1000, causing the case 1...1000: block to execute. The fallthrough then causes the next block to execute. In this case, the condition would still match. But the fallthrough here causes the next block to execute, even though this condition is not a match. The final fallthrough executes the default case’s code block, so for an output we get:

Within 1-1000
Within 1-100
Within 1-10
Out of range

So you can’t use fallthrough to try to match multiple cases, but you can use it to create cascading “default” cases based on the matched value. For example:

var whatever = 3

switch whatever {
    case 3:
        print("Three.")
        fallthrough 
    case 2:
        print("Two.")
        fallthrough
    case 1:
        print("One.")
        fallthrough 
    default:
        print("Done.")
}

This switch statement will match case 3 and cascade through the rest of the cases, producing:

Three.
Two.
One.
Done.

But matching case 1 will skip the first two matches, producing:

One.
Done.

Anything outside of the 1-3 range will just produce the default output Done.

Be careful of your switch statement construction; coming from a C or Objective-C background can take a bit of adjustment from continuously breaking from switch cases, while not having that experience can create an issue if you aren’t aware of what fallthrough can and cannot do.

Related Refcard:

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:
swift ,mobile ,switch ,conditionals ,code

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}