Chapter 26: Swift Comparison Operators
1. What are Comparison Operators?
Comparison operators let you compare two values and always return a Bool (either true or false).
They answer questions like:
- Is this equal to that?
- Is this bigger than that?
- Is this smaller or equal?
The six comparison operators in Swift are:
| Operator | Name | Meaning | Example | Result (true/false) |
|---|---|---|---|---|
| == | Equal to | Are the two values exactly the same? | age == 18 | true or false |
| != | Not equal to | Are the two values different? | password != “123456” | true or false |
| > | Greater than | Is left bigger than right? | score > 90 | true or false |
| < | Less than | Is left smaller than right? | temperature < 0 | true or false |
| >= | Greater than or equal | Is left bigger or equal to right? | marks >= 40 | true or false |
| <= | Less than or equal | Is left smaller or equal to right? | age <= 21 | true or false |
2. Basic Examples – Try these right now
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
let age = 19 print(age == 18) // false print(age != 18) // true print(age > 18) // true print(age >= 18) // true print(age < 16) // false print(age <= 21) // true |
|
0 1 2 3 4 5 6 7 8 9 10 |
let temperature = 36.6 print(temperature == 37.0) // false print(temperature > 37.5) // false print(temperature <= 38.0) // true |
|
0 1 2 3 4 5 6 7 8 9 |
let username = "Aarav007" print(username == "aarav007") // false ← case-sensitive! print(username != "guest123") // true |
3. Very Common Real-Life Examples
Example 1 – Age checks / access control
|
0 1 2 3 4 5 6 7 8 9 10 11 |
let age = 17 let hasParentalConsent = true let canWatchMovie = age >= 18 || hasParentalConsent print("Can watch: \(canWatchMovie)") // true (because of consent) |
Example 2 – Form validation (very common)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let password = "Secret123!" let minLength = 8 let isLongEnough = password.count >= minLength let hasUppercase = password != password.lowercased() let hasNumber = password.rangeOfCharacter(from: .decimalDigits) != nil let isStrongPassword = isLongEnough && hasUppercase && hasNumber print("Strong password? \(isStrongPassword)") |
Example 3 – Showing/hiding UI elements
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let cartItemsCount = 5 let isLoggedIn = true let shouldShowCheckoutButton = cartItemsCount > 0 && isLoggedIn if shouldShowCheckoutButton { print("Show 'Proceed to Checkout' button") } else { print("Show 'Add items' message") } |
Example 4 – Grading system (very typical)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
let marks = 82 let grade: String if marks >= 90 { grade = "A+" } else if marks >= 80 { grade = "A" } else if marks >= 70 { grade = "B" } else if marks >= 60 { grade = "C" } else { grade = "Needs improvement" } print("Grade: \(grade)") // A |
4. Important Details You Must Know
Detail 1 – String comparison is case-sensitive
|
0 1 2 3 4 5 6 7 |
print("Apple" == "apple") // false print("Apple".lowercased() == "apple") // true |
→ Almost always use .lowercased() or .uppercased() when comparing user input.
Detail 2 – Comparing floating-point numbers
|
0 1 2 3 4 5 6 7 8 9 10 |
let a = 0.1 + 0.2 let b = 0.3 print(a == b) // false ← floating point imprecision! print(a) // 0.30000000000000004 |
→ Never use == to compare Double or Float for exact equality when doing math.
Better ways:
|
0 1 2 3 4 5 6 7 |
let tolerance = 0.000001 let approximatelyEqual = abs(a - b) < tolerance // true |
Detail 3 – Comparing optionals
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let optionalScore: Int? = 85 let requiredScore = 80 // Safe way if let score = optionalScore, score >= requiredScore { print("Qualified") } // Dangerous way (will crash if nil) if optionalScore! >= requiredScore { … } // ❌ avoid this |
5. Very Common Beginner Mistakes & How to Fix Them
| Mistake | Wrong / Dangerous code | Correct / Better way | Why? |
|---|---|---|---|
| Comparing strings without case handling | if input == “yes” | if input.lowercased() == “yes” | User might type “Yes”, “YES”, etc. |
| Using == true or == false everywhere | if isLoggedIn == true { … } | if isLoggedIn { … } | Cleaner & idiomatic |
| Comparing floating-point with == | if calculated == 1.1 { … } | Use tolerance or Decimal | Floating-point math is imprecise |
| Forgetting optionals can be nil | value! > 10 | if let value, value > 10 { … } | Avoids runtime crashes |
| Using = instead of == | if age = 18 { … } | if age == 18 { … } | = is assignment — will not compile here |
6. Quick Reference – Comparison Operators Cheat Sheet
| Operator | Meaning | Example | Returns true when… |
|---|---|---|---|
| == | Equal | a == b | a and b are exactly the same |
| != | Not equal | a != b | a and b are different |
| > | Strictly greater | a > b | a is bigger than b |
| < | Strictly less | a < b | a is smaller than b |
| >= | Greater or equal | a >= b | a is bigger or same as b |
| <= | Less or equal | a <= b | a is smaller or same as b |
7. Small Practice – Try these right now
- Write code to check if a person can vote in India (age ≥ 18)
- Check if a password is acceptable (length ≥ 8 AND contains at least one uppercase letter)
- Decide discount level
- ≥ ₹5000 → 20%
- ≥ ₹2000 → 10%
- otherwise → 5%
Paste your attempts if you want feedback!
What would you like to explore next?
- Combining comparison operators with logical operators (&&, ||)
- Comparison operators in switch statements
- Comparing custom types (with Equatable)
- Floating-point comparison pitfalls in more detail
- Or move to another group of operators (logical, range, nil-coalescing…)
Just tell me — we’ll continue in the same clear, patient, teacher-like style 😊
