Operators In Swift

Hey Guys! It’s Ashank here back with another tutorial for you guys. This time, we will be talking about all the operators in Swift.

Let’s talk about some concepts first.

Swift has three types of operators: unary, binary, and ternary operators. A unary operator does operations on one target, a binary operator does operations on two targets, and a ternary operator does operations on three targets. There is only one ternary operator in Swift, but there are several unary and binary operators.

As we progress in the tutorial, ask if yourself if the operator being discussed is a unary, binary, or ternary operator.

Let’s take it to XCode to discuss all of the operators in Swift.

 

Arithmetic Operators

There are four standard arithmetic operators in Swift: +, -, *, and /: addition, subtraction, multiplication, and division respectively. All of these operators are binary operators, meaning that they are used between two variables.

Let’s take a look at these 4 operators in action.

 

 

 

Screen Shot 2016-01-31 at 5.24.28 PM

 

 

As you can see, each operator successfully does its operation and displays it on the right hand side. However, you may notice that a / b, or 5/10, is displayed as 0 when in reality the answer is 0.5. This is because we have specified “a” and “b” as whole numbers, not decimals. That’s why Swift performs an action called truncation when the compiler divides two whole numbers.

Swift truncates everything to the right of the decimal point of the result when two whole numbers are divided. The answer of “a/b” is 0.5, but since Swift truncates, or chops off, everything to the right of the decimal, “5” in “0.5” is removed. That is why we are left with 0.

Let’s take another example of truncation. Suppose our result ended up to be 1.683. If the original numbers that were whole numbers, then “683” would be cut off and the answer would be displayed as “1.”

What if we wanted Swift to display the decimal values of the quotient? Well, all we would have to do is make the whole numbers integers by adding “.0” to each of them.

 

 

Screen Shot 2016-01-31 at 5.42.17 PM

 

 

As you can see, by making “a,” 5.0, and “b,” 10.0, the division operation now displays “0.5.”

You can also concatenate, or add one string to the end of the other string by using the addition operator.

 

 

Screen Shot 2016-01-31 at 5.46.54 PM

 

 

As you can see, we concatenated the two strings by using the addition operator. It’s as simple as that!

 

Incrementing and Decrementing Operators 

Incrementing and decrementing operators are unary operators used to increase or decrease a numeric variable by 1. If you have been following the past tutorials, you will notice that we have used these operators several times.

 

 

Screen Shot 2016-01-31 at 5.52.07 PM

 

 

The incrementing and decrementing operators are very straightforward. To increase a number by 1, simply add two “+” symbols before the variable, and the same goes for the decrementing operator. As you can see, incrementing x by 1 returns 6. Decrementing y by 1 returns -1.

 

Compound Arithmetic Operators

Many times in Swift we want to manipulate a certain variable by adding, subtracting, multiplying, or dividing that variable by a certain number. Compound arithmetic operators allow you to code these operations very quickly instead of using a method you may have been using before. Let’s take a look at the 4 compound arithmetic operators.

 

 

Screen Shot 2016-02-07 at 12.36.10 PM

 

 

As you can see, compound operators are easier and shorter ways of writing a manipulation of that variable. For example, a += 5 is the same as saying a = a + 5.

 

Comparison Operators

Comparison operators compare two variables and return a boolean as either true or false. These operators are generally used as conditions in if statements and loops.

 

 

Screen Shot 2016-02-07 at 1.31.25 PM

 

 

Note that to check if two variables are equal to each other, you use two equal signs and not one equal sign. If you use one equal sign, that means you are actually setting that variable equal to the other variable. So, if you did a = b, then “a” would be set to 15. Also, a != b is a comparison operator for check if “a” is not equal to “b.” Because this is true, the console returns “true.”

The operators described in this tutorial are going to be the most common operators that you will use in your Swift endeavors. However, there are many more operators that I haven’t covered but are less common. You can check out the Swift Developer Guide

(https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html) for more operators.

That’s it for this time! I’ll catch you guys later!