Swift Arithmetic Operators Tutorial šŸ“

beginner
25 min

Swift Arithmetic Operators Tutorial šŸ“

Welcome to the Swift Arithmetic Operators tutorial! In this lesson, we'll explore the essential arithmetic operators Swift offers. Let's dive in!

Understanding Arithmetic Operators šŸ’”

Arithmetic operators are symbols that Swift uses to perform mathematical operations on values. We'll learn about five types of arithmetic operators in Swift:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Modulus (% - Remainder)

Let's see some examples of each operator.

Addition ( + ) šŸŽÆ

Addition is used to combine two numbers.

swift
var sum = 5 + 3 print("The sum is \(sum)") // Output: The sum is 8

Subtraction ( - ) šŸŽÆ

Subtraction is used to find the difference between two numbers.

swift
var difference = 10 - 3 print("The difference is \(difference)") // Output: The difference is 7

Multiplication ( * ) šŸŽÆ

Multiplication is used to find the product of two numbers.

swift
var product = 5 * 3 print("The product is \(product)") // Output: The product is 15

Division ( / ) šŸŽÆ

Division is used to divide one number by another.

swift
var quotient = 12 / 3 print("The quotient is \(quotient)") // Output: The quotient is 4

Modulus ( % ) šŸŽÆ

Modulus operator gives the remainder after division.

swift
var remainder = 12 % 3 print("The remainder is \(remainder)") // Output: The remainder is 0 (since 12 is divisible by 3)

Arithmetic Operator Precedence šŸ“

When multiple operators are used in an expression, Swift follows a specific order to evaluate them. This order is called precedence. Let's take a look at a complex example:

swift
var result = 5 + 3 * 2 print("The result is \(result)") // Output: The result is 11 (first, Swift multiplies 3 * 2, then adds 5)

šŸ“ Note: To change the order of operations, use parentheses ().

swift
var result = (5 + 3) * 2 print("The result is \(result)") // Output: The result is 20 (first, Swift adds 5 + 3, then multiplies by 2)

Operator Associativity šŸ“

Operator associativity determines how multiple operators of the same type are grouped in expressions. Swift follows the following rules for arithmetic operators:

  • Addition (+) and subtraction (-) are left-associative.
  • Multiplication (*) and division (/) are left-associative.

For example:

swift
var result = 5 + 3 + 2 print("The result is \(result)") // Output: The result is 10 (first, Swift adds 5 + 3, then adds 2)

šŸ“ Note: You can use parentheses to explicitly group terms, like so:

swift
var result = (5 + 3) + 2 print("The result is \(result)") // Output: The result is 10 (first, Swift adds 5 + 3, then adds 2)

šŸŽÆ Practice time! Let's solve a few problems with arithmetic operators.

Quick Quiz
Question 1 of 1

What is the result of the following code?

Quick Quiz
Question 1 of 1

What is the remainder when 17 is divided by 4?

That's all for now! In the next lesson, we'll explore comparison operators in Swift. Keep practicing, and happy coding! šŸŽ‰