100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Operators in Swift

Understand Swift's arithmetic, comparison, logical, and range operators, and how the closed and half-open range operators differ.

BasicsBeginner7 min readJul 8, 2026
Analogies

Introduction

Operators are special symbols that perform operations on one or more values. Swift provides arithmetic operators for math, comparison operators for testing relationships between values, logical operators for combining Boolean conditions, and range operators for expressing sequences of values. Understanding these operators is fundamental to writing conditions, loops, and calculations in Swift.

🏏

Cricket analogy: Arithmetic operators are like tallying runs off each ball, comparison operators are like an umpire's LBW check of whether the ball height clears the stumps, logical operators combine conditions like 'wicket AND boundary,' and range operators express an over's six deliveries as a sequence.

Syntax

swift
let sum = 5 + 3
let isEqual = (5 == 5)
let result = true && false
let closedRange = 1...5
let halfOpenRange = 1..<5

Explanation

Arithmetic operators include +, -, *, /, and % for addition, subtraction, multiplication, division, and remainder. Comparison operators such as ==, !=, <, >, <=, and >= compare two values and produce a Bool. Logical operators combine or invert Boolean values: && is logical AND, || is logical OR, and ! is logical NOT. Range operators describe a sequence of values: the closed range operator a...b includes both a and b, while the half-open range operator a..<b includes a but excludes b, which is especially useful when indexing into arrays. Swift also has the nil-coalescing operator ??, which supplies a default value when an optional is nil; it is covered in more depth as a separate topic.

🏏

Cricket analogy: Runs scored use +, -, *, / like tallying boundaries and singles, while == and > check if a score equals or exceeds a target; && combines 'ball missed AND stumps hit' for a bowled, and the range 1...6 covers every legal delivery in an over, unlike 1..<6 which would exclude the sixth ball.

Example

swift
let a = 10
let b = 3
print(a % b)
print(a > b && b > 0)

for i in 1...3 {
    print("closed: \(i)")
}
for i in 1..<3 {
    print("halfOpen: \(i)")
}

Output

swift
1
true
closed: 1
closed: 2
closed: 3
halfOpen: 1
halfOpen: 2

Key Takeaways

  • Arithmetic operators (+, -, *, /, %) perform basic math on numeric types.
  • Comparison operators (==, !=, <, >, <=, >=) return a Bool result.
  • Logical operators &&, ||, and ! combine or invert Boolean expressions.
  • The closed range operator a...b includes both endpoints a and b.
  • The half-open range operator a..<b includes a but excludes b.
  • The nil-coalescing operator ?? provides a fallback value for optionals and is covered separately.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#OperatorsInSwift#Operators#Syntax#Explanation#Example#StudyNotes#SkillVeris