Menu
Coddy logo textTech

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: 2

When 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 icon

Challenge

Easy
Write 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 number
  • b (Int): The second number
  • c (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: 2

Integer 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 // 20

Try it yourself

func calculateExpression(a: Int, b: Int, c: Int) -> Int {
    // Write code here
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals