DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Swift Fallthrough: An Introduction

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.

G. Ryan Spain user avatar by
G. Ryan Spain
·
Jun. 30, 16 · Tutorial
Like (2)
Save
Tweet
Share
3.82K Views

Join the DZone community and get the full member experience.

Join For Free

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 100, 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:

Swift Essentials

Swift (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps Roadmap for 2022
  • Utilize OpenAI API to Extract Information From PDF Files
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them
  • Top 5 Java REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: