Learning Swift and iOS Development Part 9: Math Operators

JonnyB
3 min readFeb 27, 2018

--

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 post, 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 below.

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 assign a value. 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 the numeric value 3. 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 is *var sum = 5 + 6 // Addition Operator is +var difference = 10 – 3 // Subtraction Operator is -var quotient = 30 / 3 // Division Operator is /

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. In the next lesson we are going to learn about Swift classes.

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.

If you want to learn more about Swift and iOS development, head over to www.devslopes.com and enroll in our Apple Developer Slope where you’ll learn everything you need to know to develop iOS apps. ❤️

--

--

JonnyB

Passionate about coding. Developer and Teacher at Devslopes.