KinderSwift 4: Increments and Math Operators

In this episode, we’ll finish looking at the integer math operators, and talk a bit about how to count by one and two using increment operators.

Transcript

Welcome back to Kinderswift. Let’s continue from where we left off last time.In our last session, we used the let and var statements.
let and var statements are called definitions since they define the identifier and some information about the identifier. Once we do that we can use the identifier without var in an assignment. In a definition we do a special assignment called initializing which sets the first value of an identifier. For a constant, this value will be the only value.

Last time we used assignment to add the current value of myResult and assign a new value to myResult.

There is a special case of this we call an increment. The basic increment is this:

myResult = myResult + 1

We take myResult, add one and assign it back for a new myResult. We will use increments for counting. There are other ways to do the same thing. Type

myResult += 1

The += operator increments for us by the value on the left side of the equals. So here we increment by 1. It is the same as writing what we did just above. We can change the increment as well. If we wanted to increment by 2, we can write this:

myresult += 2

Which is the same as

myresult = myresult + 2

There is one way even simpler. That is the ++ operator. this always adds 1 to the value, but we need no equal sign.

++myResult

Increments myResult by one and then shows the value of myResult.

You can put the plus plus at the end too, but this get a little tricky.
myResult++

Shows the result and then increments the value. In the playground, We need to show the result again.
and then we see the increment.

That is not all we can do. we can subtract, multiply, and divide as well. for example

myResult = myNumber - myOtherNumber

Subtracts myNmber from myOtherNumber.

We use an asterisk for multiplication so:

myResult = myNumber * myOtherNumber

multiplies myNumber by myOtherNumber.

We use a forward slash to divide. Try

myResult = myNumber / myOtherNumber.

This divides myNumber by myOtherNumber giving an integer result.

Let’s use some literal numbers to see why this is important :

Dividing four by 2 gets us two as expected.

Dividing five by two also gets us two, since we are using integers. The remainder gets cut off.

So far I’ve kept the examples whole numbers. Try this:

myResult = 2 - 4

We get a negative number for a result. We can use negative numbers in calculations as well. This subtraction is the same as

myResult = 2 + -4

Which brings up a possible error you might get. Be careful with spacing. type this:
myResult = 2 + -4

We get an error since Swift thinks this reads myResult equals 2 negative 4. It wants an operator, not two numbers. Add a space between the minus and the 4.
myResult = 2 + -4

The error disappears.

Now try this: myResult = 1/4

We get a result of 0. So far we are only working with integers. Like 5 / 2 above, any non integer part of division is dropped off. If we need the remainder, often called a mod in Computer jargon we use the % sign. So 1 % 4 equals 1. and 5 % 2 also equals 1.

There are a few times we use a mod for calculations. Most of the time, I want 5 divided by two to be 1.5 and one divided by four to be 0.25. That requires something else called a Double, and that will be the topic for our next lesson.

One Reply to “KinderSwift 4: Increments and Math Operators”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s