Flow control structures enable developers to apply logical processes and make decisions about what code is executed. Most modern programming languages provide a similar set of flow control structures:

It's said that virtually any programming control flow requirement can be implemented with a while statement alone. However, the other various control structures allow programmers to create control flow that's more concise and clearly expresses the intent of the logical program flow.

Indeed, Swift provides a rich and powerful set of control structures, which you'll learn about in this section.

The if Statement

The most basic flow control statement in programming is the if statement, which executes a block of code if some Boolean expression is true. The preceding diagram is the flow chart of the if statement. The syntax for the Swift if statement is as follows:

The following code example implements an if statement:

A {condition-list} can be one or more expressions that each return a Bool data type. Any of the following are valid for a Swift {condition}:

Swift has several rules regarding the if statement that may be different from other programming languages you're familiar with:

Have a look at the following diagram. It illustrates how the switch statement works:

The switch Statement

A switch statement is a powerful and flexible branching structure that most developers will use very often in their programs. Swift's switch has powerful, flexible features that we'll cover in detail next.

Creating a program that needs to execute different code blocks depending on the same {Boolean expression} is a common requirement, and can be implemented with the if statement as follows:

The preceding code implements the requirement to print a child's life stage depending on their current age, but repeating the condition for each case quickly becomes repetitive and can be more prone to coding errors than a more concise switch statement.

The previous code fragment can be easily rewritten with a switch/case statement as follows:

A switch statement evaluates a single control expression, personAge, in this case, and then executes the lines of code contained within the first matching case block.

Using the switch control structure to implement this logic results in code that's more concise and easier to read and maintain.

The switch statement is essentially a more structured and readable way to implement a nested if statement. It's common to refactor a nested if to a case statement to make the code more readable and maintainable. Let's do this now.

Use an Xcode playground to convert a code with if statements to an equivalent code with switch statements.

  1. Launch Xcode, create a new playground, and save it to your desktop with the name CaseRefactor.playground.
  2. Add the following code, which uses a nested if statement to determine the country code given a country name:
    let countryName = "United States"
    var countryCode = ""
    
    if countryName == "United Kingdom" {
        countryCode = "GB"
    } else if countryName == "Mexico" {
        countryCode = "MX"
    } else if countryName == "Canada" {
        countryCode = "CA"
    } else if countryName == "Spain" {
        countryCode = "ES"
    } else if countryName == "United States" {
        countryCode = "US"
    } else {
        countryCode = "??"
    }
    print("Country named '\(countryName)' has code \(countryCode)")
  3. Next, let's employ an enumeration, which we learned in the last lesson, to encapsulate the country names into a more maintainable data structure. Add the following code underneath the print statement:
    enum Countries:String {
        case uk = "United Kingdom"
        case mx = "Mexico"
        case ca = "Canada"
        case es = "Spain"
        case us = "United States"
        case unknown = ""
    }
  4. Add a switch statement, which accomplishes the same logic as the nested if—but in a more readable and structured way. Also note that because a case statement is required to be exhaustive, it would be a compiler error to forget to add countries included in the enumeration to the case statement:
    switch Countries(rawValue: countryName) ?? .unknown {
        case .uk: countryCode = "GB"
        case .mx: countryCode = "MX"
        case .ca: countryCode = "CA"
        case .es: countryCode = "ES"
        case .us: countryCode = "US"
        case .unknown: countryCode = "??"
    }
  5. To make the conversion complete, add the original print statement below the switch statement:
    print("Country named '\(countryName)' has code \(countryCode)")