Skip to content

Files

Latest commit

 

History

History
78 lines (62 loc) · 3.24 KB

5.3 - Control Transfer Statements.md

File metadata and controls

78 lines (62 loc) · 3.24 KB

These statements change the order in which your code is executed. In Swift there are 5:

  • continue
  • break
  • fallthrough
  • return
  • throw

In this note we will look at the first 3. return is covered in Functions, and throw is covered in error handling.

continue

This behaves exactly as in other languages; it ends the current iteration of the loop and resumes at the start of the next iteration of the loop. e.g.:

let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
for character in puzzleInput {
    if charactersToRemove.contains(character) {
        continue
    } else {
        puzzleOutput.append(character)
    }
}
print(puzzleOutput)
// Prints "grtmndsthnklk"

break

In loops, break behaves in exactly the same way as it does in other languages; it ends the current loop and resumes code after the loop's closing brace (})

In a switch statement, break is used to allow certain conditions in the loop to be skipped. This is because

  1. Every switch case block must contain an executable statement
  2. Switch statements do not have implicit fallthrough, so break statements are not needed as they are in other langauges.

fallthrough

Because switch statements do not have fallthrough as they do in C, Swift has the fallthrough keyword to allow this behaviour when necessary, as in the folowing example:

let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += " a prime number, and also"
    fallthrough
default:
    description += " an integer."
}
print(description)
// Prints "The number 5 is a prime number, and also an integer."

Note that when using the fallthrough keyword, the case statement into which the code is falling is not checked. It simply causes execution to move to the next case (or default) Thus, be careful when employing it.

Labelled Statements

In Swift, both loops and conditionals can be escaped using the break and continue keywords. This can lead to some confusion as to what exactly you are escaping from. To handle this, Swift allows you to label your statements using keywords. You can then specify this label when using a control transfer statement. For example, if we wanted to repurpose the code snippet from the continue section, it would look like this (notice the introduction of puzzleLoop on lines 4 and 6):

let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
puzzleLoop: for character in puzzleInput {
    if charactersToRemove.contains(character) {
        continue puzzleLoop
    } else {
        puzzleOutput.append(character)
    }
}
print(puzzleOutput)
// Prints "grtmndsthnklk"

Previous Note | Back To Contents | Next Note