# Introduction to Swift

Apr 24 2024 · Swift 5.10, iOS 17, Xcode 15

## Lesson 02: Flow Control & Functions

### Episode complete

Play next episode

Next

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

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

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

Unlock now

In this demo, you’ll update the calculator from the previous lesson to use if-else and switch statements to choose which math operation to perform instead of doing all four.

let int1 = 10
let int2 = 5
let intOperation = "+"
var result = 0
var resultMessage = ""
if intOperation == "+" {

} else if intOperation == "-" {

} else if intOperation == "*" {

} else if intOperation == "/" {

}
result = int1 + int2
resultMessage = "Integer sum of \(int1) and \(int2) is: \(result)"
print("Result: \(result)")
print("Message: \(resultMessage)")
result = int1 - int2
resultMessage = "Integer subtraction of \(int1) and \(int2) is: \(result)"
result = int1 * int2
resultMessage = "Integer multiplication of \(int1) and \(int2) is: \(result)"
result = int1 / int2
resultMessage = "Integer division of \(int1) and \(int2) is: \(result)"
else {
resultMessage = "Unknown Operator"
}
else {
resultMessage = "\(intOperation) is an unknown operator."
}
switch (intOperation) {
case "+":

case "-":

case "*":

case "/":

default:

}
result = int1 + int2
resultMessage = "Integer sum of \(int1) and \(int2) is: \(result)"
...
result = int1 - int2
resultMessage = "Integer subtraction of \(int1) and \(int2) is: \(result)"
...
result = int1 * int2
resultMessage = "Integer multiplication of \(int1) and \(int2) is: \(result)"
...
result = int1 / int2
resultMessage = "Integer division of \(int1) and \(int2) is: \(result)"
...
resultMessage = "Unknown Operator"
print("Result: \(result)")
print("Message: \(resultMessage)")