Flow Control

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous lesson, you started working on a basic calculator that did addition, subtraction, multiplication and division using the same two numbers you provided. No real calculators ever do that! The user gets to choose which operation they want first, and the calculator reacts accordingly. The execution of the calculator changes based on the input you provide. This is what flow control means.

Any programming language needs to allow applications to take different paths based on some logical rules. Swift provides a variety of ways to do this.

if Statement

The first of those ways you’ll learn about are `if` and `else`. They’re as easy as they sound. Take this real life example:

``````If the weather is sunny
I'll go out for a walk
else
I'll watch a movie
``````
``````if CONDITION {
THINGS_TO_DO_IF_CONDITION_IS_TRUE
} else {
THINGS_TO_DO_IF_CONDITION_IS_FALSE
}
``````
``````if weather == "raining" {
takeUmbrella()
}

goToTheOffice()
``````
``````if weather == "raining" {
takeUmbrella()
} else if weather == "snowing" {
takeHeavyFuryCoat()
} else if weather == "windy" {
takeLightCoat()
} else if weather == "sunny" {
takeCap()
}

if day == "workday" {
goToTheOffice()
} else {
goToTheClub()
}
``````
``````if weather == "raining" {
takeUmbrella()
}
if weather == "snowing" {
takeHeavyFuryCoat()
}
if weather == "windy" {
takeLightCoat()
}
if weather == "sunny" {
takeCap()
}
``````

switch Statement

Another form of flow control is the `switch` statement. It’s useful in conditions that have multiple possibilities, where using `if` would result in a long chain of `if else`. That long chain might become hard to read. Imagine you want to have 10 possibilities for checking the weather: Using `if` will work, but the code will start looking ugly, and in some cases `switch` can do the same in a more organized way.

``````switch (VARIABLE) {
case VALUE_1:
DoOperation_1()
case VALUE_2:
DoOperation_2()
case VALUE_3:
DoOperation_3()
case VALUE_4:
DoOperation_4()
default:
FallbackOperation()
}
``````
``````switch (weather) {
case "raining":
takeUmbrella()
case "snowing":
takeHeavyFuryCoat()
case "windy":
takeLightCoat()
case "sunny":
takeCap()
default:
print("Weather is none of the 4 conditions")
}
``````
``````switch (weather) {
case "snowing":
takeHeavyFuryCoat()
fallthrough
case "raining":
takeUmbrella()
case "windy":
takeLightCoat()
case "sunny":
takeCap()
default:
print("Weather is none of the 4 conditions")
}
``````
``````if weather == "snowing" {
takeHeavyFuryCoat()
takeUmbrella()
}
``````
``````switch (weather) {
case "snowing", "sleeting":
takeHeavyFuryCoat()
case "raining":
takeUmbrella()
case "windy":
takeLightCoat()
case "sunny":
takeCap()
default:
print("Weather is none of the 5 conditions")
}
``````