Chapter 47: Swift else if

1. What does else if actually do?

else if lets you check multiple different conditions in sequence.

It means:

  • First check the if condition
  • If that is false → check the first else if
  • If that is false → check the next else if
  • …and so on
  • If none of them are true → run the final else (if you wrote one)

Very important rule:

Swift stops at the first true condition it finds It never checks the conditions that come after

This is why order matters a lot.

2. Basic pattern – the skeleton you will see everywhere

Swift

The last else is optional — you can leave it out.

3. Very first examples – feel how it works

Example 1 – Temperature advice

Swift

What happens when temperature = 29?

  • 38? No

  • 32? No

  • 26? Yes → prints “Warm and sunny – light clothes 😎”

  • stops — never checks the rest

Example 2 – No final else

Swift

This pattern is very common when you only want to react to certain good cases and ignore bad ones.

4. Real-life examples – code you will write in real apps

Example 1 – User access level / subscription status

Swift

This is one of the most common if…else if…else patterns in real apps.

Example 2 – Mini grade calculator (classic teaching + real pattern)

Swift

Example 3 – Weather + rain advice

Swift

Example 4 – Age-based content restriction

Swift

5. Very Important Rules & Best Practices

Rule 1 – Order is critical

Always put the most restrictive / highest value conditions first.

Wrong order example:

Swift

Correct:

Swift

Rule 2 – Prefer positive conditions

Swift

Rule 3 – Use guard instead of deep nesting (very modern & strongly recommended)

Swift

guard makes code much flatter and easier to follow.

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 higher condition after lower one 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
Forgetting final else can be missing assuming something always happens It’s ok — sometimes you only care about true cases Intent is clear

7. Quick Reference – if…else if…else patterns you will use most

Situation Typical pattern Example
Simple yes/no if … { … } else { … } if isRaining { takeUmbrella() } else { walk() }
Multiple ranges / levels if … else if … else if … else { … } temperature, grade, score, age checks
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 *