Chapter    14

Arithmetic Operators

You use the arithmetic operators to do addition (+), subtraction (-), multiplication (*), and division (/). Generally, you use these operators in the same way you would in math (see Listing 14-1).

Listing 14-1. Arithmetic Operators

let r1 = 1 + 2let r2 = 3 - 1let r3 = 6 * 5let r4 = 12 / 3

If you are using arithmetic operators on two integers, then you can expect an integer result by default. All of the examples in Listing 14-1 will result in integer output. If one of the values is a Float or Double data type, then the result will be a floating-point data type as well.

Note  Be careful when dividing two integers since you can’t always be sure that the result will be an integer. You will not see the remainder ...

Get Swift Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.