Comparison and Identity Operators
Compare values with ==, !=, and identity with ===.
Comparison and Identity Operators is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
Comparison Operators
Comparison operators ask a yes/no question and return a Bool. Swift has ==, !=, <, >, <=, and >=.
print(3 == 3)
print(5 != 2)
print(4 < 10)Equality and Inequality
== tests whether two values are equal; != tests whether they differ. These work for numbers, strings, and many other types.
let a = "swift"
let b = "Swift"
print(a == b)
print(a != b)Less Than and Greater Than
< and > compare ordering. They are the foundation of sorting and range checks.
let temperature = 22
print(temperature > 18)
print(temperature < 0)Less-or-Equal and Greater-or-Equal
<= and >= include the boundary value. Useful when a threshold itself should pass.
let score = 60
print("Pass:", score >= 60)
print("Perfect or less:", score <= 100)Comparisons in Conditions
Comparison results drive if statements and loops, letting your program make decisions.
let age = 17
if age >= 18 {
print("Adult")
} else {
print("Minor")
}Comparing Strings
Strings compare lexicographically (dictionary order) using Unicode. == checks for exact equality.
print("apple" < "banana")
print("Zoo" < "apple")
print("cat" == "cat")Chaining Comparisons with Logic
Combine comparisons with logical operators to express ranges, like checking a value falls inside bounds.
let value = 7
let inRange = value >= 1 && value <= 10
print("In range 1...10:", inRange)Value Types Compare by Content
For value types like Int and String, == compares the actual contents. Two structurally equal values are equal.
let point1 = (x: 1, y: 2)
let point2 = (x: 1, y: 2)
print(point1 == point2)Reference Identity with Triple-Equals
Classes are reference types. The identity operator === tests whether two references point to the exact same object in memory, not whether their contents match.
class Box { var n: Int; init(_ n: Int) { self.n = n } }
let x = Box(5)
let y = x
print(x === y)Not-Identical with Triple-Not-Equals
!== is the opposite of ===. Two separate objects with the same data are not identical.
class Box { var n: Int; init(_ n: Int) { self.n = n } }
let a = Box(5)
let b = Box(5)
print(a === b)
print(a !== b)Identity vs Equality
Use === only with classes to ask are these the same instance. Use == for value equality. They answer different questions.
class Box { var n: Int; init(_ n: Int) { self.n = n } }
let first = Box(1)
let alias = first
let other = Box(1)
print("same object:", first === alias)
print("different object:", first === other)Quick Check
Reason about identity versus equality for class instances.
Recap: Comparison and Identity
You covered == != < > <= >= for ordering and equality, that value types compare by content, and that classes add === / !== to test whether two references are the same instance.
class Node { var v: Int; init(_ v: Int) { self.v = v } }
let n1 = Node(3)
let n2 = n1
print("equal value check:", n1.v == n2.v)
print("same instance:", n1 === n2)Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Comparison and Identity Operators” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Comparison and Identity Operators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “Comparison and Identity Operators”?
Compare values with ==, !=, and identity with ===. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Comparison and Identity Operators” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arithmetic and Compound Assignment Operators
- Comparison and Identity Operators
- Logical Operators and Short-Circuiting
- Bitwise and Overflow Operators