Operations with our Integers
In our Playground, we know that numAge
is an Int, but with Ints, we also can write arithmetic expressions using numbers, variables/constants, operators, and parentheses. Let's start with addition, subtraction, multiplication, and division. Add the following into Xcode:
// (+) operator let numSum = 23 + 20 // (-) operator let numResult = 32 - numSum // (*) operator let numTotal = numResult * 5 // (/) operator let numDivide = numTotal / 10
So, numSum
added two integers (+
operator) together, totaling 43
in our preceding example. Then, we subtracted (-
operator) numSum
from 32
to create numResult
(−11
in our example). After that, we took numResult
and multiplied (*
operator) it by 5
(see -55
in the Results panel). All of this is pretty basic math—however, you may have noticed something different with our division equation (/
operator). When you divide two integers, the result will be a third integer. So, instead of -55
divided by 10
equaling -5...