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
let sum = 5 + 3
let isEqual = (5 == 5)
let result = true && false
let closedRange = 1...5
let halfOpenRange = 1..<5Explanation
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
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
1
true
closed: 1
closed: 2
closed: 3
halfOpen: 1
halfOpen: 2Key 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
1. Which operator represents the remainder (modulo) operation in Swift?
2. What values does the range 1..<5 include?
3. Which operator is logical AND in Swift?
4. What does the closed range operator 1...5 include?
Was this page helpful?
You May Also Like
if-else Statements in Swift
Learn how Swift's if-else statement branches execution using strictly typed Bool conditions and mandatory braces.
Loops in Swift (for-in, while, repeat-while)
Understand Swift's three loop types — for-in, while, and repeat-while — since the C-style for loop was removed.
Nil Coalescing Operator in Swift
The `??` operator supplies a default value when an optional is nil, in a single concise expression.
Data Types in Swift
Explore Swift's core data types—Int, Double, Float, Bool, String, Character, and tuples—and how Swift handles numeric type safety.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics