Chapter 46: Swift else

1. What does else actually mean?

else means “otherwise” or “in all other cases”.

It is the fallback branch that runs when the if condition (and all else if conditions) are false.

So the full mental model is:

  • if this is true → do this
  • else if that is true → do that
  • else (none of the above are true) → do this instead

The else block is optional — you can have an if without any else.

2. Very first examples – let’s feel what else does

Example 1 – Very basic

Swift
  • temperature = 33 → prints “It’s hot 🥵”
  • temperature = 26 → prints “The weather is comfortable 😊”

The else block is the safety net — it catches every case that did not satisfy the if.

Example 2 – No else (nothing happens when false)

Swift

This is very common when you only care about one specific case and want to ignore all others.

3. else with else if — the chain pattern

This is the most classic and most frequently written form in real apps.

Swift

How Swift reads this chain:

  1. Is score ≥ 90? → No
  2. Is score ≥ 80? → Yes → print “Very good – A 👍” → stops here — never looks at the rest

Important rule #1 about else if chains:

The first true condition wins — everything after it is ignored.

That’s why you must put the highest / most specific conditions first.

Wrong order example (very common mistake):

Swift

Correct order — always highest first:

Swift

4. Real-life examples — code you will actually write

Example 1 – User status / subscription check

Swift

Example 2 – Temperature advice (classic teaching + real pattern)

Swift

Example 3 – Simple age-based message

Swift

Example 4 – Mini discount 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 when you want early exit (very modern & recommended)

Swift

guard makes code much flatter and easier to follow.

6. Very Common Beginner Mistakes & Fixes

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…else 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 *