In this chapter, you are going to learn the basic math operators in Swift.
The four common math operators in Swift are:
- + for addition.
- – 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.
This chapter is part of the free Swift for Complete Beginners series.
Disclaimer: To learn Swift, you have to repeat everything you see in this guide and in the following guides. If you only read the guides, you are not going to learn anything!
Display Computed Values: The print() Function
In Swift, there is a built-in function called print().
This function can be used to display values in the console.
For example, you can print the result of a calculation.
To demonstrate printing, please add these two lines to your Playground file:
var name = "Alice" print(name)
Now, you are going to see the name “Alice” on the console on the right-hand side.
The habit of printing values is something you need to know because you are going to do it a lot throughout your career as a developer.
For example, if your code causes some strange issues, you can print the variables to see their values. This helps you narrow down the root cause of the issue.
If you are using a Swift Playground, you do not need to print values because they are visible on the sidebar console on the right-hand side.
However, if you are using an online compiler, or later on when you are working with actual apps, there is no sidebar console that would display the values. In this case, you need to print values (or use a debugger) to easily see them!
From now on in this course, whenever you see a section similar to this:
Example:
var name = "Alice" print(name)
Output:
Alice
It means:
- Write these lines of code into your Playground file.
- Run the Playground.
- See the results in the console.
Now, let’s get back to the topic of this guide, that is, the math operators in Swift.
The 4 Basic Math Operators
The very basic math operators taught in the elementary school are:
- Addition, for example, 5 + 5 = 10
- Subtraction, for example, 8 – 3 = 5
- Multiplication, for example, 5 x 5 = 25
- 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.
Maths is not something you need to learn as an iOS developer. If you are good at maths, you possess algorithmic thinking and some concepts can be easier for you. However, being bad at maths is not a problem if you want to become an iOS developer.
Now, let’s move on to math operators in Swift.
Math Operators in Swift
The basic math operators in Swift are:
- + for addition
- – for subtraction
- * for multiplication
- / for division
Let’s go through these operators in more detail.
Addition (+)
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.
Next Chapter: Comparison 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:
- + for addition
- – for subtraction
- * for multiplication
- / for division
You can use these operators to do basic maths between two or more values.
In the next chapter you are going to learn how to compare values in Swift.
Next Chapter: Comparison Operators in Swift
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
Recent Posts
- Artificial Intelligence2023.05.16Humata AI Review (2023): Best PDF Analyzer (to Understand Files)
- Python2023.05.139 Tips to Use ChatGPT to Write Code (in 2023)
- Artificial Intelligence2023.04.1114 Best AI Website Builders of 2023 (Free & Paid Solutions)
- JavaScript2023.02.16How to Pass a Variable from HTML Page to Another (w/ JavaScript)