Swift Operators: Arithmetic, Comparison, and Logical Operators Explained


How do you calculate a movie’s average rating? Check if a character’s scare score beats the record? Combine multiple conditions to decide what happens next? You use operators — special symbols that perform operations on values. They’re the verbs of your code.

In this guide, you’ll learn Swift’s essential operators: from basic arithmetic through comparison, logical, and range operators. We won’t cover custom operators or advanced operator overloading — those are intermediate topics.

This guide assumes you’re familiar with data types.

What You’ll Learn

What Are Operators?

An operator is a special symbol or phrase that checks, changes, or combines values. When you write 3 + 5, the + is an operator that adds two numbers together.

Think of operators like the controls on the machines at the Monsters, Inc. Laugh Floor. One lever adds laughs to a canister, another compares levels, and a switch flips the power on or off. Each does one specific job.

Arithmetic Operators

Swift provides the standard math operators you’d expect:

let a = 10
let b = 3

print(a + b)   // Addition
print(a - b)   // Subtraction
print(a * b)   // Multiplication
print(a / b)   // Division
print(a % b)   // Remainder
13
7
30
3
1

The remainder operator (%) gives you what’s left over after dividing. 10 % 3 is 1 because 3 goes into 10 three times with 1 left over.

Note: Integer division always drops the decimal. 10 / 3 gives 3, not 3.333. Use Double values if you need decimal results.

String concatenation also uses +:

let first = "Buzz"
let last = "Lightyear"
print(first + " " + last)
Buzz Lightyear

Compound Assignment Operators

Compound assignment operators combine an operation with assignment in a single step:

var scarePoints = 100
scarePoints += 50    // Same as: scarePoints = scarePoints + 50
scarePoints -= 20    // Same as: scarePoints = scarePoints - 20
scarePoints *= 2     // Same as: scarePoints = scarePoints * 2
print(scarePoints)
260

These are just shortcuts. += means “add and assign the result back.” They work with any arithmetic operator.

Comparison Operators

Comparison operators compare two values and return a Bool — either true or false:

let sulleyScore = 95
let mikeScore = 80

print(sulleyScore == mikeScore)  // Equal to
print(sulleyScore != mikeScore)  // Not equal to
print(sulleyScore > mikeScore)   // Greater than
print(sulleyScore < mikeScore)   // Less than
print(sulleyScore >= 95)         // Greater than or equal
print(mikeScore <= 80)           // Less than or equal
false
true
true
false
true
true

You’ll use comparison operators constantly with control flow — they’re how your code makes decisions.

Logical Operators

Logical operators combine boolean conditions. There are three:

  • !NOT (flips true to false, false to true)
  • &&AND (both must be true)
  • ||OR (at least one must be true)
let isScary = true
let isFunny = true
let isBoring = false

print(!isBoring)              // NOT boring → true
print(isScary && isFunny)     // Scary AND funny → true
print(isScary && isBoring)    // Scary AND boring → false
print(isBoring || isFunny)    // Boring OR funny → true
true
true
false
true

Think of && as strict — everything must be true. Think of || as lenient — anything being true is enough.

Tip: Swift evaluates logical operators left to right and short-circuits: if the first condition in && is false, it doesn’t bother checking the second one.

Ternary Conditional Operator

The ternary operator is a compact way to choose between two values based on a condition. It uses the pattern condition ? valueIfTrue : valueIfFalse:

let score = 92
let verdict = score >= 90 ? "Fresh" : "Rotten"
print(verdict)
Fresh

This is equivalent to an if/else but written in a single line. Use it for simple choices — for complex logic, an if/else is more readable.

let age = 5
let ticket = age < 12 ? "Child" : "Adult"
print("\(ticket) ticket")
Child ticket

Range Operators

Range operators create sequences of values. Swift has two:

The closed range (...) includes both endpoints:

let countdown = 1...5
for number in countdown {
    print(number, terminator: " ")
}
print()
1 2 3 4 5

The half-open range (..<) includes the start but excludes the end:

let indices = 0..<3
for i in indices {
    print("Index \(i)")
}
Index 0
Index 1
Index 2

The half-open range is especially useful with arrays, where indices start at 0 and the last valid index is count - 1.

Apple Docs: Range — Swift Standard Library

Nil-Coalescing Operator

The nil-coalescing operator (??) provides a default value when something might be absent. You’ll use this extensively with optionals:

let nickname: String? = nil
let displayName = nickname ?? "Anonymous"
print(displayName)
Anonymous

Think of it as a fallback: “use this value, or if it’s missing, use that one instead.” It’s much cleaner than writing an if/else to check for nil.

let favoriteMovie: String? = "Wall-E"
print(favoriteMovie ?? "No favorite set")
Wall-E

Since favoriteMovie has a value, ?? returns it directly. The default “No favorite set” is only used when the value is nil.

Common Mistakes

Confusing = and ==

// ❌ This assigns, not compares
var score = 100
// if score = 100 { }  // Error in Swift!
// ✅ Use == for comparison
var score = 100
if score == 100 {
    print("Perfect score!")
}
Perfect score!

A single = assigns a value. A double == compares two values. Swift actually catches this mistake at compile time — it won’t let you use = inside an if condition.

Dividing Integers and Expecting Decimals

// ❌ Integer division drops decimals
let result = 10 / 3
print(result)
3
// ✅ Use Double for decimal results
let result = 10.0 / 3.0
print(result)
3.3333333333333335

If either side of a division is an Int, the result will be an Int with the decimal truncated.

What’s Next?

  • Arithmetic operators (+, -, *, /, %) do math
  • Comparison operators (==, !=, <, >) return bools
  • Logical operators (!, &&, ||) combine conditions
  • The ternary operator (? :) is a compact if/else
  • Range operators (..., ..<) create sequences
  • The nil-coalescing operator (??) provides defaults

With operators in your toolkit, you’re ready to handle text like a pro. Head over to Working with Strings to learn about interpolation, multiline strings, and Unicode.