100 Days of SwiftUI Day 5

100 Days of SwiftUI – Day 5 – Conditional Statements

It’s day 5 of the 100 Days of SwiftUI! Yesterday, we focused on type annotations and we solved the second checkpoint. Today, we dive into conditional statements.

If/else and else if conditional statements in SwiftUI

When writing code, you’ll regularly write conditional statements. A conditional statement is used to check whether a predefined condition is either true or false. For example, you might want to check if someone is either older than 18 or not, or if a score is higher than the previous high score, or not. Swift features multiple ways to check conditions and if/else and else if statements are probably the most common.

let age = 18

if age >= 18 {
    print("You are eligible to vote.")
} else {
    print("Sorry, you are too young to vote.")

// Since age is equal to 18, "You are eligible to vote" will print.

let score = 80
let highScore = 95

if score > highScore {
    print("Congratulations, that's a new high score!")
} else if score == highScore {
    print("You have equalled the high score, well done!")
} else {
    print("Better luck next time!")

// This will print "Better luck nest time!"

Checking multiple conditions

Sometimes, you don’t just want to check if a single condition is true, but instead, you want to check multiple. That’s also possible using the same if/else statements above by adding && (AND) or || (OR) operators. Let’s build on the example above.

let newScore = 99
let newHighscore = 95

if newScore > 40 && newScore < 60 {
    print("The score is between 40 and 60")
} else if newScore > 60 && newScore < 80 {
    print("Your score is between 60 and 80")
} else if newScore > 80 && newScore <= 95 {
    print("Your score is between 80 and 95")
} else if newScore > newHighscore || newHighscore > 100 {
    print("New high score! You're score is \(newScore)")
} else {
    print("Your score is \(newScore)")

Switch statement

Another way to check multiple conditions besides using an if/else statement is by using a switch statement. A switch uses cases instead of if/else conditions. The key difference is that a switch statement is exhaustive. This means that either every single case needs to be checked, for example when using an enum, or there must be a default value, which triggers if none of the cases validate to true.

enum weekdays {
    case Monday, Tuesday, Wednesday, Thursday, Friday

let weekday = weekdays.Thursday

// Create switch

switch weekday {
case .Monday:
    print("It's Monday")
case .Tuesday:
    print("It's Tuesday")
case .Wednesday:
    print("It's Wednesday")
case .Thursday:
    print("It's Thursday")
    print("It's Friday!")

// This will print "It's Thursday".

You might be wondering when to use an if/else statement and when to use a switch. I already listed one reason, when you need to exhaust all possible cases, but Paul has an article detailing a few more possible reasons if you’re interested.

Ternary conditional operator

The last way to check a condition is by using a ternary conditional operator. Paul mentioned that while this was not used much in early versions of Swift, it has become increasingly more common in current versions. Therefore, it’s important to understand how they work.

The ternary operator uses 3 inputs. An interesting mnemonic for these inputs is “WTF”. No, it’s not what you think. “WTF” in this case stands for WHAT, TRUE, FALSE. You start your ternary operator with the WHAT, the condition you want to check. Then, you provide what should be done if that condition is either TRUE or FALSE. An example will probably help greatly, so here it comes.

// Ternary conditional operator

let maverickAge = 27

let ageCheck = maverickAge >= 18 ? "18 or older" : "Younger than 18"

// Prints 18 or older, because maverickAge = 27.

// a more complex example
enum Theme {
    case light, dark

let theme = Theme.light

let background = theme == .dark ? "black" : "white"

// Prints "white" because background is not dark.

If you want to know when to use the ternary operator, then Paul, like always, has got you covered.

That was it for day 5! Tomorrow, we’ll focus on loops, as well as face a new challenge with the third checkpoint. Time to get ready. As always, let me know what you think in the comments.


Hi! My name is Darryl and this is my personal blog where I write about my journey as I learn programming! You'll also find articles about other things that interest me including games, tech and anime.

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

100 Days of SwiftUI – Day 66

100 Days of SwiftUI – Day 47

100 Days of SwiftUI – Day 14 – Optionals

100 Days of SwiftUI – Day 74