# Math Operators in Swift: Complete Guide with Examples

The four common math operators in Swift are:

• for subtraction.
• * for multiplication.
• / for division.

For example, let’s perform some basic maths with these operators and store the results into variables:

```var sum = 5 + 10
var sub = 50 - 40
var prod = 5 * 4 * 3
var frac = 10.0 / 3.0```

In this guide, you are going to learn how to do basic math in Swift. If you know what addition, subtraction, multiplication, and division mean, this chapter is going to be a walk in the park.

Also, before learning about the math operators, you are going to learn how to display values computed by your program.

Let’s jump in!

## The 4 Basic Math Operators

The very basic math operators taught in elementary school are:

1. Addition, for example, 5 + 5 = 10
2. Subtraction, for example, 8 – 3 = 5
3. Multiplication, for example, 5 x 5 = 25
4. Division, for example, 16 / 8 = 2

Similar to how these operations are useful in real life, you are going to need them in your code.

Now, let’s move on to math operators in Swift.

## Math Operators in Swift

The basic math operators in Swift are:

• for subtraction
• * for multiplication
• / for division

Let’s go through these operators in more detail.

To add two or more values together in Swift, use the addition operator (+).

For example:

```let x = 10
let y = 20

let sum = x + y

print(sum)```

Output:

`30`

Remember to write the above code to your Playground and run the playground file to see the result! Also, feel free to tweak the names and values of the constants.

As another example, let’s chain the addition operators similar to how you commonly do in maths:

```let sum = 1 + 2 + 3 + 4
print(sum)```

Output:

`10`

Both of these examples show you how to use the addition operator to add values together in Swift.

Next, let’s talk about the opposite, that is, subtraction.

### Subtraction (-)

To subtract one value from another in Swift, use the subtraction operator ().

For example, let’s create two constants and subtract one from the other:

```let x = 10
let y = 20

let diff = x - y

print(diff)```

Output:

`-10`

As another example, let’s chain the subtraction operators similar to how you commonly do in maths:

```let diff = 1 - 2 - 3 - 4
print(diff)```

Output:

`-8`

Next, let’s move on to multiplication.

### Multiplication (*)

To multiple two or more values in Swift, use the multiplication operator (*).

For example:

```let x = 10
let y = 20

let prod = x * y

print(prod)```

Output:

`200`

As another example, let’s chain the multiplication operators similar to how you commonly do in maths:

```let prod = 5 * 4 * 3
print(prod)```

Output:

`60`

Last but not least, let’s talk about the inverse operation of multiplication, that is, division.

### Division (/)

To divide a value by another in Swift, use the division operator (/).

For example:

```let x = 10.0
let y = 20.0

let frac = x / y

print(frac)```

Output:

`0.5`

You can also chain the division operators:

```let frac = 5.0 / 10.0 / 2.0
print(frac)```

Output:

`0.25`

This completes our guide on the basic math operators in Swift.

## Conclusion

In this guide, you learned what are math operators in Swift and how to use them.

To recap, there are four main math operators: