Chapter 44: If…Else

1. What does if…else actually do?

if…else lets your program make decisions.

It answers questions of the form:

“If this is true, do this. Otherwise (else), do that (or do nothing).”

The condition inside if must be a Bool (true or false).

Basic structure:

Swift

The else part is optional — you can have if without else.

2. Very first examples – try these right now

Example A – Simple yes/no

Swift

Example B – No else (do nothing if false)

Swift

Example C – Boolean directly

Swift

3. else if — checking several possibilities

When you have more than two cases, use else if.

Swift

Important rules about else if chains:

  1. Swift checks conditions from top to bottom
  2. As soon as one condition is true, it runs that block and skips everything below
  3. Order matters — put the most specific / highest priority conditions first
  4. The final else (optional) catches all cases that didn’t match above

Wrong order example (common beginner mistake):

Swift

Correct order:

Swift

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

Example 1 – Login / user status (very typical)

Swift

Example 2 – Form validation (extremely common)

Swift

Example 3 – Temperature / weather advice

Swift

Example 4 – Mini grade calculator

Swift

5. Important Details & Best Practices

Detail 1 – Conditions must be Bool

Swift

Detail 2 – Prefer positive conditions when possible

Swift

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

Swift

guard makes code flatter and easier to read — highly recommended.

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 incorrectly if optional == 10 { … } if let value = optional, value == 10 { … } optional == 10 is usually not what you want

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 & concise optional handling
Short one-liner if condition { doSomething() } if isLoading { showSpinner() }

8. Small Practice – Try these

  1. Write code that gives weather advice based on temperature:
    • 38 → “Extreme heat! Stay inside”

    • 32–38 → “Very hot – drink water”
    • 25–32 → “Warm day”
    • 15–25 → “Pleasant”
    • else → “Cold”
  2. Create a simple login check:
    • not logged in → “Please sign in”
    • logged in, not premium → “Welcome! Get Premium?”
    • logged in + premium → “Welcome Premium member!”
  3. Write a 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 *