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

Swift
Swift
Swift

3. Very Common Real-Life Examples

Example 1 – Age checks / access control

Swift

Example 2 – Form validation (very common)

Swift

Example 3 – Showing/hiding UI elements

Swift

Example 4 – Grading system (very typical)

Swift

4. Important Details You Must Know

Detail 1 – String comparison is case-sensitive

Swift

→ Almost always use .lowercased() or .uppercased() when comparing user input.

Detail 2 – Comparing floating-point numbers

Swift

Never use == to compare Double or Float for exact equality when doing math.

Better ways:

Swift

Detail 3 – Comparing optionals

Swift

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

  1. Write code to check if a person can vote in India (age ≥ 18)
  2. Check if a password is acceptable (length ≥ 8 AND contains at least one uppercase letter)
  3. 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 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *