Skip to content
Theory · Operators · lesson 1 of 7

Arithmetic

What you'll learn

What you'll learn:

  • predict the result of dividing whole numbers and of the remainder, negative numbers included;
  • explain what happens when a number does not fit its type, and what &+ is for;
  • compare values and combine conditions with &&, || and !;
  • check whether a number falls in a range, and pick one of two values with the ? : operator.
Seventeen sweets shared between five jars, three each, with two left on a saucer; labels 17 / 5 and 17 % 5

Swift counts like a calculator, but this calculator has rules of its own. It remembers the type of every number, whole or fractional, and the type decides the answer.

Adding, subtracting and multiplying whole numbers works the way it did at school. Division is different. Picture sharing 17 sweets among five kids. You can't cut the sweets, so each kid gets a whole number of them, and the extras stay with you. That is how Swift divides whole numbers:

SWIFT
let sweets = 17
let kids = 5
print(sweets / kids) // 3
print(sweets % kids) // 2

Let's see what the program printed:

  1. sweets / kids prints 3: seventeen sweets among five kids is three each.
  2. sweets % kids prints 2: two sweets stay with you.

A pocket calculator would show 3.4, but Swift showed 3. The sweets picture works well for positive numbers. Negative ones follow a rule of their own, and we'll get to it in the section on the remainder.

Five arithmetic operators

A symbol for an action, such as +, is called an operator. Swift has five of them for numbers:

  • + adds: 7 + 2 is 9;
  • - subtracts: 7 - 2 is 5;
  • * multiplies: 7 * 2 is 14;
  • / divides: 7 / 2 is 3;
  • % gives the remainder of a division: 7 % 2 is 1.

The order of operations is the same as in math: *, / and % run before + and -. Parentheses change the order: 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20.

Dividing whole numbers

When both numbers have type Int, the result is an Int too, and an Int has no fractional part. Such division is called integer division: the fraction of the result is dropped, not rounded.

  • 17 / 5 is 3, even though on paper it comes to 3.4.
  • 19 / 5 is 3 too, even though 3.8 is closer to four.
  • -17 / 5 is -3, not -4: a negative result drops its fraction as well.

Integer division always moves the result toward zero. In the previous chapter Int(2.9) turned into 2 and Int(-2.9) into -2 the same way.

The remainder and its sign

The % operator gives what is left after integer division. The quotient and the remainder are linked: the quotient times the divisor, plus the remainder, gives back the original number. For the sweets that is 3 * 5 + 2, which is 17.

This link gives the rule for negative numbers: the remainder has the same sign as the dividend, the number to the left of %.

SWIFT
print(-17 / 5, -17 % 5) // -3 -2
print(17 / -5, 17 % -5) // -3 2

Let's go through both lines:

  1. In the first line the quotient is -3, and -3 * 5 is -15. Getting to -17 takes another -2, and that is the remainder.
  2. In the second line the dividend 17 is positive, so the remainder 2 is positive too, even though the divisor is negative.

Interviewers like to ask this: what sign does the remainder have when the dividend is negative? In math the remainder is usually taken as non-negative, while in Swift it takes the sign of the dividend.

The % operator works only on whole numbers. If you apply it to a Double, the compiler reports an error.

A fractional result

A fraction comes from dividing numbers of type Double. There are two ways to get one.

A fractional number in the code

A number with a dot, such as 7.0, is already fractional. Swift reads the 2 next to it as a Double too, and 7.0 / 2 gives 3.5.

Converting constants

A constant already has a type, and that type never changes on its own. So both numbers are converted to Double before dividing: Double(sweets) / Double(kids) gives 3.4.

Compound assignment

score = score + 5 comes up so often that it has a shorthand: score += 5. Such operators are called compound assignment.

SWIFT
var score = 10
score += 5
score -= 3
score *= 2
print(score) // 24

Let's go line by line:

  1. score += 5 adds 5: score now holds 15.
  2. score -= 3 subtracts 3: now it holds 12.
  3. score *= 2 multiplies by 2: now it holds 24.
  4. print(score) prints 24.

Compound assignment changes the variable, so it works only with var.

The score++ you may know from other languages does not exist in Swift:

TEXT
error: cannot find operator '++' in scope; did you mean '+= 1'?

The compiler suggests the replacement itself: score += 1.

Common mistake

The result of dividing two Int values is converted to Double in the hope of getting a fraction:

SWIFT
let total = 7
let parts = 2
let wrong = Double(total / parts) // ✗
let right = Double(total) / Double(parts) // ✓
print(wrong, right) // 3.0 3.5

There is no compile error, but wrong ends up holding 3.0. First total / parts runs, which is integer division, and only its result 3 becomes a Double. Convert the numbers to Double before dividing, as on the line with right.

Key points

  • Dividing two Int values drops the fraction: 17 / 5 is 3, and -17 / 5 is -3.
  • % gives the remainder, and its sign matches the dividend's: -17 % 5 is -2.
  • To get a fraction, convert the numbers to Double before dividing, not the result.
  • +=, -= and *= change a variable in place, and Swift has no ++ operator.