Your next career begins with
Save 50% off your seat in our next iOS Bootcamp. Limited time only. Sessions start April 3.
Your First iOS & SwiftUI App: An App from Scratch
Jan 11 2022 Swift 5.5, iOS 15, Xcode 13
Part 3: Coding in Swift
25. If / Else Statements

Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
About this episode
Learn how to write if/else statements in Swift and how you can use them to implement the algorithm.
Instructors
Contributors
Instructor
Instructor
Illustrator
Video Editor
Over 300 content creators. Join our team.
Version history
iOS 15, Swift 5.5, Xcode 13 (Selected)
Jan 11 2022iOS 16, Swift 5.7, Xcode 14
Feb 13 2023iOS 14, Swift 5, Xcode 12
Dec 15 2020iOS 13, Swift 5, Xcode 11
Sep 3 2019iOS 12, Swift 4, Xcode 10
Jul 24 2018iOS 11, Swift 4, Xcode 9
Sep 19 2017iOS 10, Swift 3, Xcode 8
Nov 7 2016Swift 2
Sep 1 2015Leave a rating/review
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
This video If / Else Statements was last updated on Jan 11 2022
Let’s implement the algorithm we just designed to calculate the difference between the slider and the target value in Swift.
Along the way, you’ll learn about an important new construct in Swift development: the if else statement. Let’s dive right in.
Go to points(sliderValue:), explain if statements
if something is true {
then do this
} else if something else is true {
then do that instead
} else {
do something when neither of the above are true
}
Full implementation
func points(sliderValue: Int) -> Int {
var difference: Int
if sliderValue > self.target {
difference = sliderValue - self.target
} else if self.target > sliderValue {
difference = self.target - sliderValue
} else {
difference = 0
}
var awardedPoints: Int = 100 - difference
return awardedPoints
}
Cmd-U to run tests, show it works.
Then build and run. Show it working in the actual game.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more