In this post, we will start with conditional statements.
In conditional statements, the execution of a statement depends upon the condition.
Swift provides if–else and switch as conditional statements.
For now, we will concern ourselves to if-else statements.
The following block of code specifies how the if-else statements are written in swift.
if ( condition ){
//execute block1
} else {
//execute this block2
}
If the condition is true, block1 executes else block2 executes.
This will be more clear once we look into some examples.
If Statement
Example: We have a variable marksOfStudents. If the marks are less than 30, we will print fail. The code will be like this
var marksOfStudents = 25
if ( marksOfStudents <= 30 ){
print("Student Fails")
}
Here we check in the condition whether marks are less than and equal to 30. Since the condition is true “Student Fails” gets printed.
Suppose the condition is false. marksOfStudent = 45 (> 30). Let’s see what happens
var marksOfStudents = 45
if ( marksOfStudents <= 30 ){
print("Student Fails")
}
There is no output. Because we have not coded the false condition.
Else Statement
To code the false condition, else is used. The code will now become:
var marksOfStudents = 45
if ( marksOfStudents <= 30 ){
print("Student Fails")
} else {
print("Student Passes")
}
We can see from the output, “Student Passes” is printed as condition is false.
Else-If Statement
Suppose we want to print division and fail base upon the marks. If marks are greater than 75, we want to award “First Division”. If marks are greater than 30 but less than 75, we want to award “Second Division”, If marks are less than 30, we want to declare “Student fails”
We can see there are multiple conditions here, which cannot be handled using only if-else. So here, we will use if-else-if. The else-if statement is used to handle additional conditions.
Let’s see how the code for this one would look like:
var marksOfStudents = 45
if ( marksOfStudents >= 75 ){
print("First Division")
} else if (marksOfStudents >30 && marksOfStudents < 75) {
print("Second Division")
} else {
print("Student fails")
}
As we can see from the code, first we check the condition whether marks are greater than or equal to 75 using if. Next we check the middle condition whether marks are between 30 and 75 using the else if. Finally we use else to handle the situation when both the conditions become false.
The output is second division. Here the else-if condition becomes true as 45 is less than 75 and greater than 30.
“Control Flow” can be defined as the order in which statements in our programs gets executed.
Today we will delve into loops. Swift provides the for-in loop, while loop and repeat-while loop.
For-in Loops
If you know java or python, you would have encountered for loops. They are used to execute a block of statements multiple times.
The for loops in swift are written using the following syntax.
for index in range{
//statements
}
Example: to print the square of numbers from 1 to 5.
for number in 1...5 {
print("Square of \(number) is \(number*number)")
}
“…” is a closed range operator. It is used to select the range of our iteration, inclusive of the start and end index. Here the range is {1,2,3,4,5}. Our variable number is initialized with the value “1”, ie the starting value of the range. Then the print statement is executed, and our number variable increments to “2”, then again the print statement. This loop is repeated until the value of index reaches the end of our range.
Hence our output stops at 5.
For-in loops are also used to iterate over arrays, strings, and dictionaries. Let’s see examples of few:
Consider an array of colors. We will print all colors present in the array using for loop.
var color = ["Red", "Green", "Yellow"]
for element in color{
print("\(element) is a color")
}
Here element is our variable whose value changes and range is the color array. One by one all elements present in our array is printed.
Consider another example of a dictionary. Each dictionary item returns a key, value pair when iterated. And these keys and values can be used as variables in the for-in loop.
var groceryList = ["potatoes" : 4, "carrots" : 6, "onions" : 10]
for (vegetables, quantity) in groceryList{
print("We need \(quantity) \(vegetables) from the market")
}
Here vegetables are keys and quantity are values in our dictionary. We use them as variables to iterate over the dictionary and print it.
While loops
Next, comes the while loops. A while loop performs an iteration until the condition becomes false.
Swift provides two types of while loops – while and repeat-while.
While
The while checks the condition at the start of the iteration.
First, the condition is checked, if it is true then only the statements are executed.
For our example, we will find the factorial of a number. Factorial of a number, let’s say 5 is calculated as a product of all the numbers from 5 to 1, ie 5,4,3,2,1. It is 120.
This can also be written as 5 * (5-1) * 3 * 2 * 1 or 5 * (5-1) * (5-2) * 2 * 1
This will lead to an algorithm like this:
We will start with two variables num and factorial. num will store our integer 5 and factorial will store our product. Initially, factorial will be set to 1.
The second step is, we will assign product of factorial and num to variable factorial.
The third step will decrease the number by 1
Next, we will check the condition whether num > 1 or not. Based on that steps 2 and 3 will be repeated.
Suppose at present we have num= 5 and factorial= 1
After step 2 and step 3, factorial will be equal to 1 * 5 ie 5 and num will be equal to 5-1 ie 4
Then in step 3, we check the condition whether num > 1, Currently num = 4 which is greater than 1. Hence the condition is true, so we will again do step 2 and 3. And so on until num becomes 1.
We can see that steps 2 and 3 are going to be repeated 4 times until num becomes 1.
Now, we will convert this algorithm to our code in swift.
We will take a variable number as 5 and another variable factorial with initial value as 1
Our while will have a condition “number>1” as we saw in our algorithm. We will calculate factorial as the product of factorial and number.
//while loops
//Factorial of a Number
var number = 5
var factorial = 1
while(number>1){
factorial = factorial * number
}
print("Factorial is \(factorial)")
Hold on a minute. I am getting an error. Something like “Execution was interrupted“. Also, see that this loop has executed 28 times. According to us, our loop should have executed only 4 times.
This happens because our number’s value is always 5. It is not decreasing. So the condition in while statement remains true always. When we write “number = number – 1“. The code runs smoothly. We can print the result. And it shows 120.
//while loops
//Factorial of a Number
var number = 7
var factorial = 1
while(number>1){
factorial = factorial * number
number = number - 1
}
print("Factorial is \(factorial)")
Also if you change the number, for example to 7. Now our factorial should be 5040.
And we can see our code also gives us the same output.
If you want to check an individual value of factorial and number changing over each result. Click on show result. You might see a graph. To change to values, give a two finger click and select “value history”.
See the value changing. First, we have 7 which is the product of 7 and 1, then 42 which is the product of 6 and 7 and so on.
Similarly, you can see the decreasing value of a number by clicking on show result and again change to value history. Here the loop has run 6 times until number becomes 1.
Repeat-While
Next is repeat-while, which executes the code first and checks condition in the end.
For this one, we will write code to generate the binary representation of a decimal number.
The binary representation of 8 is “1000”. We do this using division. The number is divided by 2 and then the quotient is divided by 2 and so on until we get the quotient as 1 or 0. Simultaneously we save the remainders. Starting with this 1, we join all the remainders to finally have the binary representation.
The algorithm is like this:
We will have two variables: num storing the integer and bin which will store our binary string.
In the second step, we will first find the remainder by dividing num by 2 and add it to our binary string.
In the third step, we will divide num by 2.
In the final step, we check whether the number is not equal to zero, if yes, repeat step 2 or 3.
Suppose at present we have num equals 8
After step 2 and 3, bin = bin + 8%2 = “” + 0 = 0, and num = 8 /2 = 4.
Now when we check num != 0, it is not 0, and so step 2 and 3 will be repeated again and so on until num becomes 0.
When we see the final result, it is 0001, sort of reverse to our correct answer which is 1000.
We can change this by changing our expression for step 2 to bin = num%2 + bin. As you see here, now the final string becomes 1000.
Let’s do this in swift now:
First, we will initialize a decimalNumber variable. Then a string binary.
Inside our loop, first, we need to save the remainder as a string in our variable binary.
//repeat-while
//Binary representation of a number//
var decimalNumber = 8
var binary = ""
repeat{
binary = (decimalNumber % 2) + binary
decimalNumber = decimalNumber / 2
}while(decimalNumber != 0)
print("Binary representation is \(binary)")
But when we write binary += decimalNumber % 2, we get an error.
This error occurs because we are assigning an int value to a string variable. So we need to type cast this value into a string. Let’s do that.
//repeat-while
//Binary representation of a number//
var decimalNumber = 8
var binary = ""
repeat{
binary = (String)(decimalNumber % 2) + binary
decimalNumber = decimalNumber / 2
}while(decimalNumber != 0)
print("Binary representation is \(binary)")
Followed by while with a condition of decimalNumber != 0.
Now when we print binary, we get an output of “1000” as expected.
Let’s check for 25, the output should be “11001“. And we get the same output.
Here also we can check the individual value using show result and then value history for both binary and decimal values.
Now that we have learned arrays and sets, it’s time to finish collections by learning dictionaries.
Dictionaries. What do you think about when you hear the word dictionary. Well, I think about the dictionaries, the English dictionaries we used when we were kids to find the meaning of words. I use them still. Just that the dictionary is now on the internet.
Think about how we used to search a meaning of the word, for example, “rock” in the dictionary. We will straight away go to the r section of the book, then search for a word starting with “ro” and finally zero in on our word rock. Read its meaning and close.
We wouldn’t start with words starting with a, followed by b, and so on to reach the words starting with “r”. That is going to take a lot of time. Am I right? We will just straight away jump to the words starting with “r”
One important thing to note here is that rock is the key and its meaning is the value. We find the key, we find the meaning.
Just like those English dictionaries, dictionaries in swift are generally pairs/associations of keys and values. These keys and values can be of the same type or different. Also, every value is associated with a key, which is now an identifier for that value.
Like sets, dictionaries also have no ordering.
A dictionary is written as [“key” : “value”]
Example, a dictionary of antonyms : [“hot” : “cold”, “dark” : “light”, “big” : “small”]
Here “hot’ will be a key and “cold” its value.
The swift dictionary type is written using Dictionary[“Key” : “Value”]. This can also be written as [“Key”: “Value”]. The latter form is widely used.
Coming back to the playground, let’s learn how to initialize and use these dictionaries.
The first method, just like in arrays and sets, uses the initializer syntax.
Here we will use the antonyms example.
var antonyms = [String : String]()
This will generate variable antonyms which can store keys of type String and values also of type string. Currently, it is initialized as an empty dictionary.
Let’s print this and see
print(antonyms)
This symbol here [:] shows the dictionary is empty.
Now comment this line and the print line and again initialize the dictionary as empty.
var antonyms = [:]
This throws an error “Empty collection literal requires an explicit type”
This happens because the compiler doesn’t know the type of key and values. So we cannot use this way to initialize a dictionary.
When we uncomment our first line. Then remove “var” from this one the error vanishes.
Let’s add few antonyms to our dictionary. And remove the comment from print statement.
Well, now we know how to initialize the dictionary. so let’s have a look at the operations we can perform on it.
The first operation is “Insert”
We use subscript syntax to insert into dictionaries. In this method, we will use a new key having the same type as Keys and assign to it new value having the same type as Values.
Now we will add tomatoes and squash to our dictionary.
A set is just like an array. Only the difference being that sets have distinct values and no ordering.
example: A set of first four letters of an alphabet. [“a”, “b”, “c”, “d”]
How do we initialize a set in Swift?
We do the initialization using initializer syntax:
Let’s initialize a set containing ages of our friends.
var age = Set<Int>()
var age followed by Set, Int in open brackets, ending with function brackets.
When we print age, we see that it’s empty.
So, we need to add in the ages using
age = [38, 65, 97]
Now when we print age, we see it has three numbers 38, 65, 97.
The second method of initialization is using array literal.
For this one, we will use an array of colors.
var color : Set<String> = ["Red", "Green", "Yellow"]
The variable color is declared as a set if string values are written using Set<String>. The variable color can only save String values as we specified the type as String. Since we used three colors to initialize the set, color set is not empty.
In arrays, we used [String], but here we use Set<String> as Set type can not be inferred only with String in double brackets, word Set is very important.
This initialization can be done using only “Set” as Swift can infer the type of elements of the set because of its type inference property.
First commenting this initialization, we write
var color : Set = ["Red", "Green", "Yellow"]
Next, we work upon the “Operations on Set”
The first operation is “Inserting an element into the set“. For this, we will insert color “Blue” in the set “color”.
color.insert("Blue")
We can see here in the result bar that “Blue” has been added to set “color”
To confirm, we can print color.
A difference from the array can be seen here, we have not specified the location where we want the blue to be placed. Because unlike arrays, sets are unordered lists. So blue is added at a random position.
The subsequent function is “Removing an element from the set“. For this, we will remove color “Green” from the set.
color.remove("Green")
When we print the set now, the result is devoid of “Green”.
Now we move on to “Contain function: to check whether an element is inside the set”. Again for this first, we will check whether alphabet “a” is inside set and then check whether “Red” is inside the set.
print(color.contains("a"))
We can see the result here in the result are is “false” as “a” is not inside set color.
print(color.contains("Red"))
Red is inside set the color and so the result is true.
Next is the method to count the number of elements in the set.
print(color.count)
Clearly, the answer is 3 as seen here.
Apart from all these operations, there are certain operations that are performed on sets as we have studied in maths.
Let’s see one by one all the operations:
Consider two sets :
A = { “a”, “e”, “i”, “o”, “u” }
B = { “l”, “a”, “u”, “r”, “e”, “n” }
The two circles here denote sets A and B.
Since { “a”, “e”, “u”} is common in two sets, we place them in the intersecting of two circles.
{ “a”, “e”, “u”} is also the result of intersection of sets A and B. The yellow color is used to highlight An intersection B
Union operation on the sets leads to a set of all elements from A and B.
So here union of set A and B will have {“a”, “e”, “i”, “o”, “u”, “l”, “r”, “n”} with { “a”, “e”, “u”} only once. As we have seen earlier sets cannot have repeated values, hence no repeated values here.
On this slide, the yellow color is used to show the union of sets A and B.
Symmetric Difference gives values in each set but not in both. Here they will be { “o”, “i”, “l”, “r”, “n”}
Subtracting B from A will give values in A which are not in B. ie { “o”, “i” }
Now we will perform all these operations in Swift.
We have taken two sets:
var vowels : Set = ["a", "e", "i", "o", "u"]
var nameLetters : Set = ["l", "a", "u", "r", "e", "n"]
The first operation is Intersection
print(nameLetters.intersection(vowels))
The result is { “a”, “e”, “u”} as expected
Next is Union
print(nameLetters.union(vowels))
The result is all letters in both sets : [“a”, “i”, “r”, “n”, “u”, “e”, “l”, “o”]
For Symmetric Difference
print(nameLetters.symmetricDifference(vowels))
we get [“o”, “l”, “r”, “i”, “n”] as expected
The last is subtraction
print(nameLetters.subtracting(vowels))
The result is [“l”, “r”, “n”] as expected.
With this we complete Sets. In next post, we will cover Dictionaries, the last of collections.
Append, Insert, Remove, Count, and Retrieve are the operations that can be performed on arrays.
Append is used to add items at the end of an array. Here we are adding “Ravenclaw” to the array “house”. Ravenclaw is added to the end of the array.
Insert is used to add items at a desired position in the array.
“insert(newElement: Element, at: Int)
Here newElement is the element we want to add in the array. And after at: we specify the index at which element will be added.
In our example, we are adding “Slytherin” and index 1. Array index start from 0, so Slytherin gets added after Gryffindor which is at 0. Rest elements are shifted one place down.
Remove is used to delete items in the array.
“remove(at: Int)”
Here we just need to specify the index of the element we want to delete.
In our example we are deleting “Slytherin” which is at 1.
Count returns the total number of items in the array.
We can retrieve any element of the array through its index. Here we want to access Ravenclaw. So we use the index of Ravenclaw.