With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Operations on Integers

Swift allows you to perform basic mathematical operations on integers using the familiar operators `+` (add), `-` (subtract), and `*` (multiply). Try printing the results of some arithmetic.

Listing 4.8 Performing basic operations

```...
let numberOfPeople: UInt = 40

print(10 + 20)
print(30 - 5)
print(5 * 6)
```

The compiler respects the mathematical principles of precedence and associativity, which define the order of operations when there are multiple operators in a single expression. For example:

Listing 4.9 Order of operations

```...
print(10 + 20)
print(30 - 5)
print(5 * 6)

print(10 + 2 * 5) // 20, because 2 * 5 is evaluated first
print(30 - 5 - 5) // 20, because 30 - 5 ...```

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required