Chapter 43: Swift If…Else

1. What is if…else?

if…else is the most fundamental way to make decisions in code.

It answers the question:

“If this condition is true, do this block of code. Otherwise (else), do something different (or do nothing).”

Basic structure:

Swift

The else part is optional.

2. Very first examples – try these right now

Swift
Swift
Swift

3. else if — checking multiple conditions

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

Swift

Important rules about else if:

  • Swift checks conditions from top to bottom
  • As soon as one condition is true, it runs that block and skips all the rest
  • You can have as many else if as you want
  • The final else (optional) catches everything else

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

Example 1 – Simple login status

Swift

Example 2 – Form validation (very common in apps)

Swift

Example 3 – Temperature / weather advice

Swift

Example 4 – Score / grade system (very typical)

Swift

5. Important Details & Best Practices

Detail 1 – Conditions must be Bool

Swift

Detail 2 – Prefer positive conditions when possible

Swift

Detail 3 – Avoid very deep nesting

Bad (hard to read):

Swift

Better:

Swift

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 everywhere if isAdult == true { … } if isAdult { … } Cleaner & idiomatic
Forgetting else if chain order if score >= 80 { “A” } else if score >= 90 { “A+” } Put higher conditions first First true condition wins
Deep nesting instead of guard/early return many nested ifs Use guard or early return Flatter, easier to read code
Comparing optionals incorrectly if optional == 10 { … } if let value = optional, value == 10 { … } Direct comparison with nil is usually wrong

7. Quick Reference – if patterns you will use most

Situation Recommended pattern Example
Simple yes/no if condition { … } else { … } if isRaining { … } else { … }
Multiple ranges if … else if … else if … else { … } grade / temperature checks
Early exit / validation guard condition else { return } guard let user else { return }
Optional unwrapping + check 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 code that prints different messages based on temperature:
    • 35 → “Extreme heat”

    • 30…35 → “Very hot”
    • 25…30 → “Warm”
    • else → “Cool”
  2. Create a simple login check:
    • if not logged in → “Please sign in”
    • if logged in but not premium → “Welcome! Get Premium?”
    • if premium → “Welcome Premium member!”
  3. Write a mini grade calculator: marks ≥ 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 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)
  • Combining if with optionals (if let, if case let, etc.)
  • if in SwiftUI (conditional 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 *