Swift if Statements with Logical Operators
🔀 Swift if with Logical Operators – Complete Beginner Guide
In Swift, you can use logical operators with if statements. This helps you combine multiple conditions. It also makes your decisions clear and easy to read.
1️⃣ Logical Operators in Swift ⭐
| Operator | Name | Meaning |
|---|---|---|
&& |
Logical AND | All conditions must be true |
|
||
! |
Logical NOT | Reverses the condition |
2️⃣ if with Logical AND (&&) ⭐
Example
Output
📌 Both conditions must be true.
3️⃣ if with Logical OR (||) ⭐
Example
Output
📌 Only one condition needs to be true.
4️⃣ if with Logical NOT (!) ⭐
Example
Output
📌 ! reverses false to true.
5️⃣ Combining Multiple Logical Operators ⭐⭐
Example
Output
6️⃣ Complex Condition Example ⭐⭐⭐
Output
7️⃣ Using Parentheses for Clarity ⭐⭐
Output
8️⃣ Logical Operators with Boolean Variables ⭐⭐
Output
9️⃣ Common Mistakes ❌
❌ Using & instead of &&
❌ Using | instead of ||
❌ Forgetting parentheses in complex conditions
❌ Confusing = with ==
📌 Interview Questions (Swift Logical Operators)
Q1. Difference between && and ||?
👉 && → all true
👉 || → any true
Q2. What does ! do?
👉 Reverses boolean value
Q3. Can logical operators be chained?
👉 Yes
✅ Summary
✔ Logical operators combine conditions
✔ && → AND
✔ || → OR
✔ ! → NOT
✔ Makes code clean & readable
✔ Essential for Swift interviews
