0Pricing
Swift Academy · Lesson

Arithmetic and Compound Assignment Operators

Use +, -, *, /, % and compound forms safely.

Arithmetic and Compound Assignment Operators is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Arithmetic Operators

Swift provides the four standard arithmetic operators for all number types: + (addition), - (subtraction), * (multiplication), and / (division). They work on Int, Double, and Float.

let sum = 7 + 3
let diff = 10 - 4
let product = 6 * 5
print(sum, diff, product)

Addition and Subtraction

Addition and subtraction behave exactly as in math. The result type matches the operand types: two Int values produce an Int.

let apples = 5
let eaten = 2
let remaining = apples - eaten
print("Remaining apples:", remaining)

Multiplication

The * operator multiplies two numbers. It is commonly used in formulas such as computing an area or scaling a value.

let width = 8
let height = 4
let area = width * height
print("Area:", area)

Integer Division Truncates

When you divide two integers with /, Swift performs integer division and discards any fractional remainder. 7 / 2 is 3, not 3.5.

let result = 7 / 2
print("7 / 2 =", result)
let result2 = 20 / 6
print("20 / 6 =", result2)

Floating-Point Division

To keep the fractional part, use floating-point operands. If either side is a Double, Swift performs real division.

let exact = 7.0 / 2.0
print("7.0 / 2.0 =", exact)
let half = 1.0 / 4.0
print("1.0 / 4.0 =", half)

The Remainder Operator

The remainder operator % returns what is left over after division. 9 % 4 is 1 because 4 fits into 9 twice with 1 remaining.

print(9 % 4)
print(10 % 5)
print(17 % 3)

Using Remainder for Even and Odd

A common use of % is testing whether a number is even or odd: a number is even when n % 2 == 0.

for n in 1...6 {
    let kind = n % 2 == 0 ? "even" : "odd"
    print(n, "is", kind)
}

Compound Assignment: Plus-Equals

The compound assignment operator += adds to a variable and stores the result back in one step. total += 5 means total = total + 5.

var total = 10
total += 5
print("After += 5:", total)
total += 3
print("After += 3:", total)

More Compound Operators

The same pattern works for -=, *=, /=, and %=. Each combines an operation with assignment.

var score = 100
score -= 20
score *= 2
print("Score:", score)
var count = 17
count %= 5
print("Count:", count)

Mixing Operators in Expressions

Swift follows standard precedence: multiplication and division happen before addition and subtraction. Use parentheses to make intent explicit.

let a = 2 + 3 * 4
let b = (2 + 3) * 4
print("Without parens:", a)
print("With parens:", b)

A Practical Calculation

Combining these operators lets you build real formulas. Here we compute an average and the leftover from sharing items.

let totalPoints = 87
let players = 4
let average = totalPoints / players
let leftover = totalPoints % players
print("Average:", average, "Leftover:", leftover)

Quick Check

Predict the output of the integer division and remainder.

Recap: Arithmetic and Compound Assignment

You learned the arithmetic operators + - * / %, that integer / truncates while floating-point keeps decimals, and the compound forms += -= *= /= %= that update a variable in place. Use % for even/odd checks and remember operator precedence.

var n = 50
n += 10
n /= 3
print("Final:", n, "Remainder of 50/3:", 50 % 3)

Frequently asked questions

Is the “Arithmetic and Compound Assignment Operators” lesson free?

Yes — the full text of “Arithmetic and Compound Assignment Operators” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arithmetic and Compound Assignment Operators”?

Use +, -, *, /, % and compound forms safely. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arithmetic and Compound Assignment Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Compound Assignment Operators
  2. Comparison and Identity Operators
  3. Logical Operators and Short-Circuiting
  4. Bitwise and Overflow Operators
← Back to Swift Academy