Chapter 49: Swift Nested If

1. What is a nested if?

A nested if means putting one if statement inside another if statement.

It is used when you need to make a decision only if a previous condition is already true.

Visual structure:

Swift

Nested if answers questions like:

  • “If the user is logged in, then check if they have premium”
  • “If it’s raining, then check if I have an umbrella”
  • “If the form has an email, then check if it’s valid”

2. Very first examples – feel how nesting works

Example 1 – Very simple nesting

Swift

What happens in different cases:

  • isLoggedIn = true, hasPremium = true → “Welcome back, Premium member!”
  • isLoggedIn = true, hasPremium = false → “Welcome! You have basic access.”
  • isLoggedIn = false → “Please sign in first.” (the inner if is never even looked at)

Example 2 – Temperature + rain check

Swift

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

Example 1 – User role / permission check (very common)

Swift

Example 2 – Form validation (very typical in apps)

Swift

Notice: This kind of nesting is very common — but also very easy to make messy.

Example 3 – Nested if inside a function (real pattern)

Swift

4. The Big Problem: Deep Nesting (“Pyramid of Doom”)

When you have many nested ifs, code becomes hard to read:

Swift

This is called the pyramid of doom — very hard to follow.

5. Better alternatives to deep nesting

Alternative 1 – Use guard (very modern & strongly recommended)

Swift

Flatter, easier to read, easier to debug.

Alternative 2 – Combine conditions with &&

Swift

→ Shorter, but harder to give specific error messages.

Alternative 3 – Use switch or early returns (later topics)

6. Very Common Beginner Mistakes & Fixes

Mistake Wrong code Correct / Better way Why?
Forgetting to handle the else case nested if without final else Always consider what happens when all fail Avoids silent failures
Deep nesting without guard 5+ levels of if inside if Use guard + early return Much flatter & readable code
Not using else if when order matters multiple separate ifs Chain with else if when only one should run Separate ifs can run multiple times
Comparing optionals incorrectly if optional { … } if let value = optional { … } optional is Bool? — not what you want
Using nested if for simple logic if a { if b { doX() } } if a && b { doX() } Simpler & clearer

7. Quick Reference – Nested if patterns

Situation Typical pattern When to prefer it
Simple outer + inner check if outer { if inner { … } } Small & clear logic
Many required conditions guard … else { return } (multiple) Validation / early exit (very modern)
One main condition + several sub-cases if main { if a else if b else if c } Sub-options only matter if main is true
Optional unwrapping + check if let value = optional, value > 10 { … } Safe & concise
Deep permission / state check guard chain Flattest & easiest to read

8. Small Practice – Try these

  1. Write nested if for weather advice:
    • If temperature > 30 → “Hot”
      • If raining → “Take umbrella”
      • Else → “Wear sunglasses”
    • Else → “Normal weather”
  2. Write code that checks:
    • If logged in
      • If premium → “Premium content”
      • Else → “Basic content”
    • Else → “Sign in required”
  3. Write a function that returns a message based on:
    • If age >= 18
      • If hasLicense → “Can drive”
      • Else → “Adult but no license”
    • Else → “Under 18”

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 nested if)
  • switch statement (often cleaner than deep if…else if)
  • if let / optional binding with if
  • Conditional logic 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 *