Chapter 45: Swift if

1. What does if actually do?

if is the most basic decision-making tool in programming.

It lets your code ask a yes/no question and then choose what to do based on the answer.

The condition inside if must be something that results in true or false (a Bool value).

Basic form:

Swift

Very simple first example:

Swift

If you run this code when temperature is 33 → you see the message. If you change it to 25 → nothing happens (because condition is false).

2. Adding else — what to do when condition is false

Swift

Now the code always does something — either the hot message or the nice weather message.

3. else if — checking several possibilities

When you have more than two possible situations, you use else if.

Swift

Important rules about else if chains:

  • Swift checks conditions from top to bottom
  • As soon as one condition is true, it runs that block and skips all the following ones
  • Order is very important — put the most specific / highest value conditions first
  • The final else (optional) catches everything that didn’t match above

Classic mistake example (very common for beginners):

Swift

Correct version:

Swift

4. Real-life examples — the kind of code you will write every day

Example 1 – User login status

Swift

Example 2 – Basic age check (very common)

Swift

Example 3 – Weather advice (classic beginner-to-intermediate exercise)

Swift

Example 4 – Mini grade calculator

Swift

5. Important Details & Best Practices

Detail 1 – Condition must be Bool

Swift

Detail 2 – Prefer positive conditions when possible

Swift

Detail 3 – Use guard for early exit (very modern & recommended)

Swift

guard makes code flatter and much easier to read than deep nesting.

6. Very Common Beginner Mistakes & How to Fix Them

Mistake Wrong code Correct / Better way Why?
Using = instead of == if age = 18 { … } if age == 18 { … } = is assignment – compile error here
Writing == true / == false everywhere if isAdult == true { … } if isAdult { … } Cleaner, more idiomatic
Wrong order in else-if chain else if score >= 90 { “A+” } before >=80 Put highest condition first First true condition wins
Deep nesting instead of guard many nested ifs Use guard + early return Flatter, easier to read code
Comparing optionals directly if optional == 10 { … } if let value = optional, value == 10 { … } optional == 10 usually means you forgot nil

7. Quick Reference – if patterns you will use most

Situation Recommended pattern Example
Simple yes/no if condition { … } else { … } if isRaining { takeUmbrella() } else { walk() }
Multiple ranges / categories if … else if … else if … else { … } temperature, grade, score ranges
Early exit / validation guard condition else { return / throw / … } guard let user else { return }
Optional unwrapping + condition if let value = optional, value > 10 { … } Safe optional handling
Short one-liner if condition { doSomething() } if isLoading { showSpinner() }

8. Small Practice – Try these

  1. Write weather advice code:
    • 38 → “Extreme heat! Stay inside”

    • 32–38 → “Very hot – drink water”
    • 25–32 → “Warm day”
    • 15–25 → “Pleasant”
    • else → “Cold”
  2. Simple login check:
    • not logged in → “Please sign in”
    • logged in, not premium → “Welcome! Get Premium?”
    • logged in + premium → “Welcome Premium member!”
  3. Mini grade calculator:
    • ≥ 90 → “A+”
    • 80–89 → “A”
    • 70–79 → “B”
    • 60–69 → “C”
    • below 60 → “Fail”

Paste your code here if you want feedback or want to see cleaner/more modern versions!

What would you like to explore next?

  • guard statement in depth (very important companion to if)
  • switch statement (often cleaner than long if…else if chains)
  • if combined with optionals (if let, if case let, etc.)
  • Conditional logic in SwiftUI (showing/hiding views)
  • Or move to another topic (loops, functions, arrays, optionals…)

Just tell me — we’ll continue in the same clear, patient, detailed style 😊

You may also like...

Leave a Reply

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