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.
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.
First, open Xcode if you haven't already and click
Create New Playground.
Give it a name like Math Operators and click
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.
Delete all the boilerplate code on the left side but leave
import UIKit as it is necessary.
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.
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 = /
The division operator performs division and rounds to the nearest whole number.
If we wrote
12 / 5, we would get a return value of
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"
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.
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.