Sitemap

Basic Calculator Logic

3 min readAug 26, 2020

--

var DoMath = “and stuff”

Building a calculator in swift can seem a little daunting. However, like many things, if you break it down it becomes fairly simple. We wont build a full on app. Just do some basic logic to help you get started. I hate long winded intro’s so wer’e going to jump straight in.

Let’s start by opening a playground in xcode. Name it something you can remember so you can reference it later on.

So let’s think about what we need as far as calculations go.

We need to:

  1. Hold the numbers and results
  2. Calculate Equations

Pretty simple right!?

So what is the logic behind all this? Well let’s go to our playground. As far as logic goes we will need to use a function, an If statement (alternatively a guard let), and a switch statement. If you don’t know what a function, if statement, or switch is, or you need a refresher, I have included links to documentation.

Get Ian Value’s stories in your inbox

Join Medium for free to get updates from this writer.

To add the numbers and results we will use the following:

Press enter or click to view image in full size
This holds the value for the first number and second number

Next lets add the operator and equal buttons:

Press enter or click to view image in full size
Operand will be our operators (+,-,×,÷ , we will create these later.), and equals stores the result.

Pretty simple so far! Let’s keep it that way! Now lets create a function.

Press enter or click to view image in full size
This function will do our calculations. Let’s add some logic!

This is where the switch statement shines. We will switch through our operand to do the correct calculations.

Press enter or click to view image in full size
First we check if operand (we created in the beginning) is not equal to nil. We then switch operand to accept different cases. So if the user types in “+” our variable equals (created at the beginning as well) will return firstNumber + secondNumber. I used edit > emoji & symbols for the symbols.

We are not done with our calculatorBrain yet! Lets add an initializer after calculatorBrain so we can get our numbers and operators. We will also store that input into the variables we created earlier. When you are done it will look like this:

Press enter or click to view image in full size
Our input basically mimics an equation. First is the first number of our equation, op is our operators, and second is the second number of our equation. We then store the users input. firstNumber will be equal to first, and so on.

Now if we call calculatorBrain we will see the correct answer in the output:

Press enter or click to view image in full size
CalculatorBrain called at the bottom performs the math.

Thats it! Simple Logic for a basic calculator. There is more that goes into actually creating a calculator app. In addition to this you will also have to clear the calculator, add a decimal, add buttons and convert that input. However, understanding the fundamentals behind why things do what they do is essential for expansion.

Did this help you? Let me know below in the comments.

--

--

Ian Value
Ian Value

Written by Ian Value

ios dev, graphic designer, UI/UX, Photographer