Chapter 9: Math Operators

Math, being an integral part of programming, can be used easily in Swift with a bit of know-how and a basic understanding of arithmetic.


What you will learn

  • Basics of Math in Swift
  • The Assignment Operator
  • Arithmetic Operators
  • The Modulo Operator

Key Terms

  • Assignment Operator: the equals sign used in Swift to show assignment to a certain value or equality to another value.
  • Arithmetic Operators: the four basic math operations – addition, subtraction, division, and multiplication.
  • Modulo Operator: a math operator in Swift that allows for calculating the remainder of a division problem.

Math is a necessary part of programming. There's no getting around it. Those who are learning to program who don't come from a math/science background; those of you who don't have a degree in engineering (hint: I don't either), can still learn to code, though! The math needed in programming doesn't often go far beyond what a majority of the population knows.

In Swift, there are several operators that we can use to perform mathematical equations. In this brief chapter, we will discuss each one.

Setting up

First, open Xcode if you haven't already and click Create New Playground. Give it a name like Math Operators and click Next. Choose somewhere to save this .playground file and click Create to save it. You should see a screen like the one in Figure 1.9.1.

Figure 1.09.1 Screen Shot 2016-10-10 at 5.01.11 AM.png

Delete all the boilerplate code on the left side but leave import UIKit as it is necessary.

The Assignment Operator

The assignment operator (=) is used just like the equals sign is used in math – to declare equality. Type out the example below in your Playground to see how this works:

var three = 3

When we create the above variable and name it three. We set it to literally equal three. The name of the variable is actually irrelevant. We could have named it anything and it still would serve as a way to use the value 3 throughout our code.

Arithmetic Operators

The four basic arithmetic operators (+, -, * , /) are used in Swift just as you would in a math class or in a graphing calculator. Here are some examples to show you how they can be used in Swift. Add the following to your Playground:

var product = 10 * 20 // Multiplication operator = *

var sum = 5 + 6 // Addition Operator = +

var difference = 10 - 3 // Subtraction Operator = -

var quotient = 30 / 3 // Division Operator = /

Helpful Hint: The division operator performs division and rounds to the nearest whole number. If we wrote 12 / 5, we would get a return value of 2.


The Modulo Operator

There is an amazing operator in Swift (and other languages, too) called the Modulo (sometimes referred to the remainder operator). It's purpose is to show the remainder left over when dividing two numbers. Here is an example of it in use for you to try in your Playground:

var remainder = 13 % 5 // Prints 3 because 10 / 5 is the nearest whole number division that is possible. Three is left over as a remainder.

Here is another way you could look at the modulo operator to help it make sense:

var quotient = 13 / 5 // Prints 2 because Swift rounds up when it divides.

var remainder = 13 % 5 // Prints 3 because that is the remainder.

var result = "The result of 13 / 5 is \(quotient) with a remainder of \(remainder)"

// Prints "The result of 13 / 5 is 2 with a remainder of 3"

Wrapping up

That wasn't so bad was it? Using math in Swift is basic and easy. There's not much to it. Remember these operators and they will get you far as you go deeper into learning Swift.

Exercise

Create a variable that stores the result of 4 x 7. Create another variable that stores the result of 4 * (5 - 6) - 5. Use the modulo operator (%) to calculate the remainder of 123 / 7.