Swift Nested if Statement

Swift Tutorial

🧩 Swift Nested if Statement – Complete Beginner Guide

In Swift, a nested if statement refers to putting one if statement inside another.
It is using when a decision relies on several conditions evaluated one at a time.


1️⃣ What is Nested if in Swift? ⭐

A nested if is using when:

  • First condition must be true

  • Then another condition is checked inside it

Syntax



2️⃣ Simple Nested if Example ⭐


 

Output

Adult

📌 Second if runs only if first condition is true.


3️⃣ Nested if–else Example ⭐⭐


 

Output

First Division

4️⃣ Nested if with Multiple Conditions ⭐⭐


 

Output

Login Successful

5️⃣ Nested if vs Logical Operators ⭐⭐⭐

Nested if


Using Logical AND (&&) – Better Way


📌 Interview tip:
Nested if is useful when different actions are needed at each level.


6️⃣ Nested if with Boolean Values ⭐⭐


 

Output

Welcome Admin

7️⃣ Common Mistakes ❌

❌ Missing braces { }
❌ Too many nested levels (hard to read)
❌ Using nested if when && is enough
❌ Confusing = with ==


📌 Interview Questions (Swift Nested if)

Q1. What is nested if?
👉 An if inside another if

Q2. When should nested if be used?
👉 When conditions depend on previous conditions

Q3. Can nested if be replaced by logical operators?
👉 Yes, in simple cases using && or ||


✅ Summary

✔ Nested if = if inside if
✔ Used for dependent conditions
✔ Improves decision control
✔ Avoid deep nesting (use logical operators)
✔ Common in authentication & grading logic

You may also like...