Swift Operators

➕➖ Swift Operators (Complete & Easy Guide)
Types of Swift Operators
Swift operators are divided into these main categories:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Range Operators
Ternary Conditional Operator
Nil-Coalescing Operator
Let’s understand each one clearly 👇
1️⃣ Arithmetic Operators
Used for mathematical calculations.
2️⃣ Assignment Operators
Used to assign values.
3️⃣ Comparison Operators
Used to compare two values. Result is Bool (true / false).
4️⃣ Logical Operators
Used with Boolean values.
AND (&&)
OR (||)
NOT (!)
5️⃣ Range Operators (Very Important 🔥)
Closed Range (...)
Includes both start and end.
Output: 1 2 3 4 5
Half-Open Range (..<)
Excludes the last value.
Output: 1 2 3 4
One-Sided Range
6️⃣ Ternary Conditional Operator
Shortcut for if–else.
7️⃣ Nil-Coalescing Operator (??)
Used with Optionals.
✔ Prevents crashes
✔ Very common in Swift
8️⃣ Identity Operators (Class Types)
Used to check reference equality.
❌ Common Mistakes
❌ Mixing different types:
✅ Correct:
🧠 Summary Table
| Operator Type | Example |
|---|---|
| Arithmetic | + - * / % |
| Assignment | = += -= |
| Comparison | == != > < |
| Logical | `&& |
| Range | ... ..< |
| Ternary | ? : |
| Nil-Coalescing | ?? |
