Flow Control

Swift has all of the expected flow control statements ( if, while, for and switch ) that one expects. However, each has its own unique Swift way of writing them. There are creative differences in the syntax for each control statement, and instead of do-while and for-each loops Swift has an equivalent repeat-while and for-in loop statement.

Conditional Statements

If & If-Else Statements

If and If-Else statements in Swift look quite similar to the way they are written in other languages. The following shows a simple if statement and a more complex one.

// Simple if statement
let height = 189

if height < 200 {
    print ("under max height")
}

// If-Else statement
if height > 100 && height < 150 {
    print ("over min height")
}
else if height >= 150 {
    print ("over suggested height")
} 
else {
    print ("under min height")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Swift Fundamentals - The Good Old if Statement and What's Different About It Pluralsight

Switch Statements

Switch statements are similar to other languages, but there are some unique differences that you should note. Swift switch statements are exhaustive, meaning it must handle all possible values for a given variable. This really means you must include a default clause at the end. The next difference is that each case clause is treated as its own block of code, and must contain some executable code (comments are not enough). This means you do not need to add a break statement at the end of each case statement. This also means that there is no automatic "fall-through" from one case to the next. If you wish to have multiple case statements all fall-through to execute one block of code you must use one case statement that lists all the possible values that should run the same block of code.

// Simple switch statement
var areaCode: Int = 613

switch areaCode {
    case: 819:
        print("Gatineau")
    case: 613:
        print("Ottawa")
    case: 416:
        print("Toronto")
    default:
        print("Other location")
    
}


// A switch statement that check for multiple clauses per case statement
var currentSemester = 3

switch currentSemester {
    case 1,2:
        print("First year")
    case 3,4:
        print("Second year")
    case 5,6:
        print("Third year")
    default:
        print("Incorect semester value")
}


// A switch statement that uses ranges of numbers in a case statement
var age: Int = 189

switch age{
    case 1...15:
        print("You are too young to drive in Ontario")
    case 16...80:
        print("You are able to drive in Ontario")
    default:
        print("You must take a vision test every two years")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Swift Fundamentals - Writing Switch Statements Pluralsight

Logic Loops

While & Repeat-While Loops

The while loop will look similar to other languages you have seen before, but instead of a do-while look, Swift has a repeat-while loop. The following code demonstrates this.

// Simple While loop
var counter = 0
while counter < 10 {
    counter += 1
}

// Repeat-While loop
repeat {
    counter-=1
} while counter > 10
1
2
3
4
5
6
7
8
9

For & For-In Loops

Swift has a slightly different way to write loops. For loops look similar to other languages, but instead of a for-each, Swift uses a For-In notation.

// The format of a for loop is like this
for c in 0 ..< 8 {
    print(c)
}

// Or if you want to include the final value
for c in 0 ... 8 {
    print(c)
}

// If you wish to iterate in reverse order
for c in (0 ... 8).reversed() {
    print(c)
}

// If you wish to repeat some action, but don't need the iterated number
for _ in 0 ..< 8 {
    print("repeat task")
}

// Using stride lets you iterate through a set in more complex ways
for c in stride(from: 0, to: 100, by: 10) {
    print(c)
}

// For-In loops let you iterate through an array like this
var loopArray = [3, 5, 8, 13, 19, 26]
for v in loopArray {
    print (v)
}

// For-In loops let you iterate through a dictionary like this
var loopDictionary = ["cars": 14, "vans": 7, "trucks": 4]
for (key, value) in loopDictionary {
    print("\(key) = \(value)")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

The following videos will explain how to use flow control statements in Swift:

Swift Fundamentals - Creating Loops and Making Ranges Pluralsight

Swift Developer Docs - Control Flow

Back to Week 3

Last Updated: 9/24/2020, 4:59:06 PM