Swift Operators

Operators are symbols that perform actions on values. They are the verbs of programming — they tell Swift to add, compare, combine, or check values. Swift organizes operators into clear categories.

Arithmetic Operators

These operators do basic math.

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (integer)
%Remainder10 % 31
let a = 10
let b = 3

print(a + b)   // 13
print(a - b)   // 7
print(a * b)   // 30
print(a / b)   // 3 (no decimal for Int division)
print(a % b)   // 1

Division With Doubles

Integer division drops the decimal. Use Double to keep it.

let x: Double = 10
let y: Double = 3
print(x / y)   // 3.3333333333333335

Assignment Operators

These operators set or update a variable's value.

var count = 0

count = 5      // Assign: count is now 5
count += 3     // Add and assign: count is now 8
count -= 2     // Subtract and assign: count is now 6
count *= 4     // Multiply and assign: count is now 24
count /= 6     // Divide and assign: count is now 4
count %= 3     // Remainder and assign: count is now 1

Comparison Operators

These operators compare two values and return true or false.

OperatorMeaningExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than7 > 3true
<Less than2 < 9true
>=Greater or equal5 >= 5true
<=Less or equal3 <= 4true
let age = 18
print(age >= 18)   // true — can vote
print(age == 21)   // false

Logical Operators

Logical operators combine Bool conditions.

AND — Both must be true

let hasTicket = true
let hasID = true

if hasTicket && hasID {
    print("Entry allowed")
}
// Output: Entry allowed

OR — At least one must be true

let isMember = false
let hasCoupon = true

if isMember || hasCoupon {
    print("Discount applied")
}
// Output: Discount applied

NOT — Flips the condition

let isDoorLocked = false

if !isDoorLocked {
    print("Door is open")
}
// Output: Door is open

Diagram: Logical Operators Truth Table

A       B       A && B    A || B    !A
-----   -----   ------    ------    ---
true    true    true      true      false
true    false   false     true      false
false   true    false     true      true
false   false   false     false     true

Range Operators

Range operators create sequences of values. They appear often in loops and switch statements.

Closed Range — Includes Both Ends

for i in 1...5 {
    print(i)
}
// Output: 1 2 3 4 5

Half-Open Range — Excludes the Upper End

for i in 0..<3 {
    print(i)
}
// Output: 0 1 2

Ternary Operator

The ternary operator is a compact way to write a simple if/else decision.

let temperature = 30
let weather = temperature > 25 ? "Hot" : "Cool"
print(weather)   // Output: Hot

Read it as: "If temperature is greater than 25, weather is Hot. Otherwise, weather is Cool."

Nil Coalescing Operator

The ?? operator provides a default value when an optional is empty (nil). You will learn more about optionals in a later topic.

var username: String? = nil
let displayName = username ?? "Guest"
print(displayName)   // Output: Guest

Summary

Swift operators cover arithmetic (+ - * / %), assignment (= += -=), comparison (== != > <), logical (&& || !), range (... ..<), ternary (? :), and nil coalescing (??). Each operator works on specific types and returns a predictable result.

Leave a Comment

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