Flow Control Mastery: Navigating Control Structures

1. Conditional branching: if-statement

The if-statement starts with the if keyword, followed by the condition, and the code that should be executed if the condition is true inside opening and closing braces. The condition expression must be boolean. The braces are required and not optional. Parentheses around the condition are optional.

let a = 0

if a == 0 { // Parentheses around the condition are optional

b = 1

}

An additional, optional else-clause can be added to execute another piece of code when the condition is false. The else-clause is introduced by the else keyword followed by braces that contain the code that should be executed.

The else-clause can contain another if-statement, i.e., if-statements can be chained together. In this case the braces can be omitted.

let a = 0

var a = 0

if a == 1 {

b = 1

} else if a == 2 {

b = 2

} else {

b = 3

}

// b is 3

1.2 Optional Binding

Optional bindings are declared using the if keyword like an if-statement, but instead of the boolean test value, it is followed by the let or var keywords, to either introduce a constant or variable, followed by a name, the equal sign (=), and the optional value.

let maybeNumber: Int? = 1

if let number = maybeNumber {

// This branch is executed as maybeNumber is not nil

} else {

// This branch is *not* executed as maybeNumber is not nil

}

2. Switch

The switch-statement starts with the switch keyword, followed by the tested value, followed by the cases inside opening and closing braces. The test expression must be equatable. The braces are required and not optional.

Each case is a separate branch of code execution and starts with the case keyword, followed by a possible value, a colon (:), and the block of code that should be executed if the case's value is equal to the tested value.

An optional default case may be given by using the default keyword. The block of code of the default case is executed when none of the previous case tests succeeded. It must always appear last.

fun word(_ n: Int): String {

// Test the value of the parameter n

switch n {

case 1:

// If the value of variable n is equal to 1,

// then return the string "one"

return "one"

case 2:

// If the value of variable n is equal to 2,

// then return the string "two"

return "two"

default:

// If the value of variable n is neither equal to 1 nor to 2,

// then return the string "other"

return "other"

}

}

word(1) // returns "one"

word(2) // returns "two"

word(3) // returns "other"

word(4) // returns "other"

3. Looping

3.1 while-statement

The while-statement starts with the while keyword, followed by the condition, and the code that should be repeatedly executed if the condition is true inside opening and closing braces. The condition must be boolean and the braces are required.

The while-statement will first evaluate the condition. If it is true, the piece of code is executed and the evaluation of the condition is repeated. If the condition is false, the piece of code is not executed and the execution of the whole while-statement is finished. Thus, the piece of code is executed zero or more times.

var a = 0

while a < 5 {

a = a + 1

}

// a is 5

3.2 For-in statement

The for-in statement starts with the for keyword, followed by the name of the element that is used in each iteration of the loop, followed by the in keyword, and then followed by the array that is being iterated through in the loop.

If there are no elements in the data structure, the code in the loop will not be executed at all. Otherwise, the code will execute as many times as there are elements in the array.

let array = ["Hello", "World", "Foo", "Bar"]

for index, element in array {

log(index)

}

3.3 continue and break

In for-loops and while-loops, the continue statement can be used to stop the current iteration of a loop and start the next iteration.

The break statement can be used to stop the execution of a for-loop or a while-loop.

Immediate function return: return-statement

The return-statement causes a function to return immediately, i.e., any code after the return-statement is not executed. The return-statement starts with the return keyword and is followed by an optional expression that should be the return value of the function call.