Control Transfer Statements in Swift

You have definitely used the keyword return or probably seen the keyword break. These keywords change the order in which your code gets executed. They can transfer control from one piece of code to another. But there are actually five control transfer statements in Swift. And they are: break, continue, fallthrough, return, and throw.

In the present article let’s cover the following questions:

  • What are these keywords?
  • When do each of them is used?
  • Why it can be useful to use it?

For what these keywords are used?

As I mentioned above, you use these keywords to control the flow of your code. You can move it from one piece of code to another. And it can be very useful or even necessary for the logic of your program.
We will go briefly through each of them with examples of how and when to use it.

Break

Break control statement allows ending the execution of an all control flow statement. It is used inside a loop or a switch block to finish its execution and exit it earlier.

Consider the following example, where you want to iterate through an array of numbers, and when you find the number you are looking for, print it and end the loop because there is no need to iterate further, spend time and resources when the number was already found.

Continue

The continue keyword is used inside a loop. It tells it to stop the current iteration and start the next one. It does not exit the loop entirely but starts again at the beginning of the next iteration.

Consider the following example, where you want to remove all duplicates in an array using a loop. When you iterate through the array and find that this number is already repeated in a temporary array, you use the continue keyword to start the next iteration.

Fallthrough

This keyword is used inside of the switch statement. By default, when the switch finds the first match, it executes the code associated with that specific case and ends the execution of the switch statement.

However, for example, in the C language, you would need to use break by the end of every case if you would not want it to fall through and execute another match. But in Swift, you do not need to use it, because as I mentioned previously when the code associated with the first matching case is executed, the whole switch statement completes. And if you want to have a behavior where the switch statement would continue looking for another match, you would need to use the fallthrough keyword.

Consider the following example, where you would want to print all ranges that contain a specific number. And there may be an intersection between these ranges, which says that there is a possibility that more than just one range contains a specific number. But without the fallthrough keyword, the switch statement would execute only the first match.

Return

I assume this is the keyword you are most familiar with. And that would not be a surprise because you will probably use it the most in your career out of these five keywords.

This keyword is straightforward and is used inside a function when you want to return some value.
Consider the following example where you want to double a number and return the result from a function.

Throw

There often may be a situation where your function can produce an error. In this case, you can mark your function as a throwing function using the keyword throws in the function’s declaration after its parameters.
Now inside this throwing function, you can throw an error in the place where it can appear.

Consider the following example, where you want to check whether a music player can play a specific song. If a song does exist, then the player plays it. Otherwise, it throws an error that the song you are trying to play is not added to your playlist using the keyword throw.

You will also have to handle this error, or it will just simply crash your application.

Summary

To sum up everything, these five keywords: break, continue, fallthrough, return, and throw are used to control the flow of your program and can move it from one place to another. Each of these keywords is used for specific cases, but every one of them is very powerful and handful.

Thanks for reading!

if you enjoyed this article, be sure to give it a clap so others will find it more easily.

--

--

--

22 y.o. iOS Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why You Should Not Switch to Dvorak

Keyboard

Could not create connection to database server. Attempted reconnect 3 times.

Taking a Go at Gateway and Sub-device Development

Working Software vs. Comprehensive Documentation — BDD Makes the Save

How To Make Your Code Reviewer Like You

Welcome “Kent Beck”, new SEAT:CODE Challenger Reviewer

Spring Boot — MongoDB Unit testing with Embedded MongoDB

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sergey Chsherbak

Sergey Chsherbak

22 y.o. iOS Developer

More from Medium

Access specifiers in Swift

Protocol and protocol inheritance in swift

Copiable in Swift — Clean Approach

Abstract vector created by vectorjuice — www.freepik.com

Swift programming: Classes