0Pricing
Swift Academy · Lesson

Comparison and Identity Operators

Compare values with ==, !=, and identity with ===.

Comparison and Identity Operators is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

Frequently asked questions

Is the “Comparison and Identity Operators” lesson free?

Yes — the full text of “Comparison and Identity Operators” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Arithmetic and Compound Assignment Operators
  2. Comparison and Identity Operators
  3. Logical Operators and Short-Circuiting
  4. Bitwise and Overflow Operators
← Back to Swift Academy