Chapter 2 - Basic Operators
- Jun 6
- 7 min read
The Swift Programming Language Book
Revision Guide
Chapter 2
Basic Operators
This chapter introduces the operators used throughout the Swift language, including assignment, arithmetic, comparison, ranges and logical evaluation.
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numeric values.
Swift supports the four standard arithmetic operators:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
These operators work with both integer and floating-point values.
Addition
Use the addition operator (+) to add two values together.
The value of sum is 15.
Subtraction
Use the subtraction operator (-) to subtract one value from another.
The value of difference is 5.
Multiplication
Use the multiplication operator (*) to multiply two values.
The value of product is 20.
Division
Use the division operator (/) to divide one value by another.
The value of quotient is 5.
Integer Division
When dividing integers, Swift discards any fractional component.
The value of result is 2.
This behaviour is sometimes referred to as truncation.
If a fractional result is required, use floating-point values instead.
The value of result is 2.25.
Remainder Operator
The remainder operator (%) returns the value left over after division.
The result is 1.
This can be read as:
Divide 9 by 4 and return the remainder
The remainder operator is commonly used when checking whether a number is even or odd.
The result is 0, meaning that 10 is evenly divisible by 2.
Unary Minus Operator
A minus sign placed before a value changes its sign.
The value of minusThree is -3.
Unary Plus Operator
A plus sign placed before a value returns the value unchanged.
The value of alsoMinusSix is still -6.
Unary plus exists for completeness alongside unary minus, although it is used far less frequently.
Compound Assignment Operators
Compound assignment operators combine an arithmetic operation with an assignment.
Instead of performing an operation and then assigning the result separately, both actions are written in a single statement.
This can be read as:
Add 2 to a and assign the result back to a
After the operation, the value of a is 3.
The statement above is equivalent to:
Addition Assignment
Use += to add a value and assign the result.
The value of score becomes 15.
Subtraction Assignment
Use -= to subtract a value and assign the result.
The value of score becomes 7.
Multiplication Assignment
Use *= to multiply a value and assign the result.
The value of score becomes 20.
Division Assignment
Use /= to divide a value and assign the result.
The value of score becomes 5.
Compound Assignment Does Not Return A Value
Like the standard assignment operator, compound assignment operators do not return a value.
Their purpose is to update the value stored in a variable.
Comparison Operators
Comparison operators compare two values.
The result of a comparison is always a Boolean value:
true
false
Swift provides the following comparison operators:
Equal to (==)
Not equal to (!=)
Greater than (>)
Less than (<)
Greater than or equal to (>=)
Less than or equal to (<=)
Equal To
Use == to determine whether two values are equal.
The result is true.
The result is false.
Not Equal To
Use != to determine whether two values are different.
The result is true.
Greater Than And Less Than
Use > and < to compare relative values.
The result is true.
The result is true.
Greater Than Or Equal To
Use >= to determine whether a value is greater than or equal to another value.
The result is true.
Less Than Or Equal To
Use <= to determine whether a value is less than or equal to another value.
The result is true.
Tuple Comparison
Swift can compare tuples when each value within the tuple can also be compared.
The result is true.
Swift compares tuple values from left to right.
The first unequal value determines the result.
The result is false.
The first values are equal, so Swift compares the second values.
Tuple comparison is particularly useful when sorting or comparing multiple related values.
Ternary Conditional Operator
The ternary conditional operator provides a concise way to choose between two values based on a condition.
Its syntax is:
This can be read as:
If the condition is true, use the first value. Otherwise, use the second value
Replacing A Simple If Statement
The ternary operator is often used when choosing between two values.
If hasHeader is true, the value 50 is used.
If hasHeader is false, the value 20 is used.
The value of rowHeight becomes:
Equivalent If Statement
The previous example can be written using a standard if statement.
Both examples produce the same result.
The ternary operator simply provides a more concise alternative.
Choosing Between Two Values
The ternary operator is commonly used when selecting one of two values.
The value of result is "Pass".
Readability
The ternary operator works best when the condition and resulting values are simple.
For more complex logic, a standard if statement is often easier to read and maintain.
The goal is to improve clarity rather than reduce the number of lines of code.
Nil-Coalescing Operator
The nil-coalescing operator (??) provides a default value when an optional contains nil.
Its syntax is:
This can be read as:
If optionalValue contains a value, use it. Otherwise, use defaultValue
Providing A Default Value
Because userDefinedColorName contains nil, the value of colorName becomes:
When A Value Exists
If the optional contains a value, that value is used instead of the default.
The value of colorName becomes:
Equivalent If Statement
The nil-coalescing operator is a concise alternative to optional binding.
Both approaches produce the same result.
The nil-coalescing operator simply expresses the intent more concisely.
Relationship To Optionals
The nil-coalescing operator works specifically with optional values.
It allows code to safely provide a fallback value without force unwrapping.
If username contains a value, that value is used.
If username contains nil, "Guest" is used instead.
Range Operators
Swift provides several range operators for working with sequences of values.
Ranges are commonly used with loops, collections, and slicing operations.
Closed Range Operator
The closed range operator (...) includes both the start and end values.
Output:
The range includes both 1 and 5.
Half-Open Range Operator
The half-open range operator (..<) includes the start value but excludes the end value.
Output:
The range includes 1 but excludes 5.
Half-open ranges are frequently used when working with collection indexes.
One-Sided Ranges
One-sided ranges continue in a single direction for as long as possible.
Values Up To A Position
This range includes every element up to and including index 2.
Values Before A Position
This range includes every element before index 2.
Values From A Position
This range includes index 2 and every element that follows.
Choosing The Correct Range
Use a closed range when both boundaries should be included.
Use a half-open range when the ending value should be excluded.
Use a one-sided range when only one boundary is known.
Understanding the difference between these operators helps prevent off-by-one errors when working with collections and loops.
Logical Operators
Logical operators combine or modify Boolean values.
Swift provides three logical operators:
Logical NOT (!)
Logical AND (&&)
Logical OR (||)
Logical operators always produce a Boolean result.
Logical NOT Operator
The logical NOT operator (!) reverses a Boolean value.
Because allowedEntry is false, !allowedEntry evaluates to true.
This can be read as:
If entry is not allowed, print "ACCESS DENIED"
The logical NOT operator is often used when checking the opposite of a condition.
Logical AND Operator
The logical AND operator (&&) creates an expression that is true only when both conditions are true.
The result is true because both conditions evaluate to true.
If either condition evaluates to false, the entire expression becomes false.
The result is:
Logical OR Operator
The logical OR operator (||) creates an expression that is true when at least one condition is true.
The result is true because at least one condition evaluates to true.
If both conditions evaluate to false, the entire expression becomes false.
The result is:
Combining Logical Operators
Logical operators can be combined to create more complex conditions.
Swift evaluates the expression and determines whether the overall result is true or false.
Parentheses can be used to make complex expressions easier to read.
Short-Circuit Evaluation
Swift evaluates logical expressions from left to right.
When the result is already known, Swift stops evaluating the remaining conditions.
This behaviour is known as short-circuit evaluation.
With the logical AND operator, if the first condition evaluates to false, the remaining conditions cannot change the result.
The result must be false, so Swift does not evaluate someExpensiveOperation().
With the logical OR operator, if the first condition evaluates to true, the remaining conditions cannot change the result.
The result must be true, so Swift does not evaluate someExpensiveOperation().
Readability
Logical operators are powerful, but deeply nested conditions can become difficult to understand.
When a condition becomes complex, consider breaking it into smaller Boolean values with descriptive names.
This often improves readability without changing the behaviour of the code.
Explicit Parentheses
Parentheses can be used to make complex expressions easier to read.
Even when parentheses are not strictly required, they can make the intended order of evaluation easier to understand.
Using parentheses to improve readability is often preferable to relying on operator precedence alone.
Read the Original Swift.org Chapter
You can find the original and official chapter here - Basic Operators.
We Converted The Swift Programming Language Book into Xcode Playgrounds
You can download the Xcode playground or view the GitHub repo below.
© 2026 3DaysOfSwift.com. All rights reserved.
Stay Interview Ready.
Don't abandon the technical depth you spent years building.
👩🏿💻🧑🏻💻🙋🏿♀️🧑🏼💻👩🏼💼👩🏽💻🧑🏿💻💁🏼♀️👩🏼💻👨🏼💻👨🏽💻🙋🏽♂️👩🏻💻🧑🏾💻👩🏻💻👩🏾💻👨🏼💻🙋🏻♂️👨🏿💻🙋🏼♂️
Google Search Terms - SEO Keywords
iOS Developer interview prep. Interview preparation. Revise Swift language features. Swift crash course. How to remind myself of Swift syntax. Swift interview material. How to revise for an iOS interview. iOS interview prep. iOS interview questions. last minute revision for interview. What do I need to do for an iOS interview. Help, I have forgotten how to code in Swift. Swift coding interview prep. Modern iOS interviews. iOS interview platform. Where can I remind myself of how to program Swift? iOS interview platform. What questions do they ask in an iOS interview? Last minute iOS interview revision. Learn Swift. The Swift programming language. learn swift everyday. Apple. iPhone. iOS. iOS Developers. Learn to build apps. Become an iOS Developer. Career building in iOS. Write code in Swift. How to learn the swift computer programming language. How to write code in Xcode? Online course teaching Swift. Online course teaching the Swift programming language. Swift online tutorials. Learn Swift online. Swift.org. Official Swift Language Documentation. https://docs.swift.org/swift-book/documentation/the-swift-programming-language/
What Is 3DaysOfSwift.com?
3DaysOfSwift.com is a professional Swift revision platform built for modern iOS developers who want to remain valuable, technically sharp, and highly employable in an AI-assisted software industry.
We are not affiliated with 100 Days of Swift. If you want to learn SwiftUI please visit HackingWithSwift.com.
Copyright 2026 www.3DaysOfSwift.com. All rights reserved.





Comments