Arithmetic Operators
Part of the Fundamentals section of Coddy's Swift journey — lesson 16 of 86.
Swift provides arithmetic operators to perform basic mathematical calculations. These work just like the math you already know, making it easy to add, subtract, multiply, and divide numbers in your code.
Here are the four basic arithmetic operators:
let sum = 10 + 5 // Addition: 15
let difference = 10 - 5 // Subtraction: 5
let product = 10 * 5 // Multiplication: 50
let quotient = 10 / 5 // Division: 2When dividing integers, Swift performs integer division — it discards any decimal portion and returns only the whole number. If you need decimal results, use Double values instead:
let intResult = 7 / 2 // 3 (integer division)
let doubleResult = 7.0 / 2.0 // 3.5 (decimal division)You can combine multiple operators in a single expression. Swift follows standard mathematical order of operations — multiplication and division happen before addition and subtraction:
let result = 2 + 3 * 4 // 14, not 20
let grouped = (2 + 3) * 4 // 20 (parentheses first)Challenge
EasyWrite a function calculateExpression that takes three integers a, b, and c, and returns the result of the expression: a + b * c.
Remember that Swift follows standard mathematical order of operations — multiplication happens before addition.
Parameters:
a(Int): The first numberb(Int): The second numberc(Int): The third number
Returns: The result of a + b * c (Int)
Cheat sheet
Swift provides arithmetic operators for basic mathematical calculations:
let sum = 10 + 5 // Addition: 15
let difference = 10 - 5 // Subtraction: 5
let product = 10 * 5 // Multiplication: 50
let quotient = 10 / 5 // Division: 2Integer division discards decimals and returns only whole numbers. For decimal results, use Double:
let intResult = 7 / 2 // 3 (integer division)
let doubleResult = 7.0 / 2.0 // 3.5 (decimal division)Swift follows standard order of operations (multiplication and division before addition and subtraction). Use parentheses to change the order:
let result = 2 + 3 * 4 // 14
let grouped = (2 + 3) * 4 // 20Try it yourself
func calculateExpression(a: Int, b: Int, c: Int) -> Int {
// Write code here
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input