Hey guys! Its Ashank here back with another guide in the Beginner’s Guide to Swift series! Today we are going to be looking at control flow in Swift.
What is Control Flow?
Control flow refers to a loop that is bound by restrictions and executes a program multiple times according to that restriction. In this blog post, we will be talking about three types of control flow: For-In Loops, For Loops, and While Loops. Let’s take it to XCode to explain these three types of control flow.
For-In loops allow you to iterate through a set of numbers or general data. For example, a for-in loop would be described as so: For every number in the set of 6 numbers (1-6), perform some action. How would we write this in XCode?
You may be wondering what “index” is. Well, it is essentially a variable that holds every value of the set of numbers . The set of numbers we have here is 1 through 5. The three dots, or ellipses, signify that it is a range of numbers from 1 to 5. So we have 5 numbers: 1, 2, 3, 4, and 5. The “index” is a variable that does not need to be declared (var index is not needed, simply “index” after “for” will suffice).
The first time the loop runs, “index” will be assigned the first value in the set of numbers 1 to 5. So, “index” will be set equal to 1 and the code in the brackets will be executed. After this, “index” will be set equal to the second value, or 2. Then the code will be executed again. This process will repeat until “index” has nothing to be assigned to. The compiler will then exit the loop and carry out any code underneath the for-in loop.
That was a mouthful, but it’s extremely important to understand the two paragraphs above this one. Now, let’s add code to our loop. It will have a simple print statement as follows:
As you can see, the console shows “(5times).” To see what was actually printed, hover over that line, and click the eye.
We see that it simply prints the last loop, or loop number 5. Let’s make it so that we can see what it printed for all loops. Hover over the dialogue box and click the two parallel lines to see what the console printed for all 5 loops.
And there you have it! We can see that the console printed out the statement 5 times after each loop. When the compiler realized that there were no numbers after 5, it exited the loop and stopped printing. You can play around with the range of numbers. If we change it to 1-10, we can see that the statement will be printed 10 times.
So that concludes it for for-in loops! Let’s now talk about for loops.
A “for loop” is used with a restriction as well as a modification to a variable that holds that restriction. The three parts of a for loop are separated by semi-colons. The first part is an initialization of a variable to hold the loop number that the program is on. The second part is the restriction imposed on that variable, and the third part is how you change that variable.
Let’s write a for loop in XCode.
As you can see, we have the basis of our for loop set up. The first part defines a variable named “count”, which is set equal to 0. The second part of the for loop, the restriction, says to keep the loop going so long as “count” is less than 5. The final part, the modification of count, says to increase count by 1 each time the loop restarts. For example, the first time it goes through the loop, “count” is 0, and it is less than 5, so the loop continues to run. After it has run through the entire block of code within the loop, “count” will increase as 1, and the loop will start again.
Let’s try to put some code within this loop to see what the console returns.
Sure enough, after adding a simple print statement, we can see that the loop has run 5 times. Hover over this, click the eye, and then click the parallel lines to see what the console really outputted.
As you can see, all of print statements are displayed. In the first loop, “count” is 0, and then the sentence is printed, displaying “count.” Then, “count” is increased by 1, and the process repeats until “count” is no longer less than 5.
That wraps up for loops, so now let’s proceed to while loops!
While loops are quite frankly the easiest loops to understand. There is only one condition that needs to be satisfied for the while loop to keep going. If that condition is always satisfied, then the while loop will continue to run infinitely. However, you can make it so that the loop increments by 1 each time it starts a new loop. Let’s try this out in XCode.
We will set up two variables: firstNumber and secondNumber. Then we will use these two numbers to create a restriction as the basis for the while loop.
firstNumber is equal to 5, and secondNumber is equal to 10. Then, in our while loop restriction, we wrote, “while firstNumber is less than secondNumber, execute the following code.” Let’s write that code now. It will be a simple print statement of firstNumber.
But wait. At the right you can see that the number of times the loop is looping is increasing continuously!
To stop this from happening, and this is a common error in making while loops, we should add a command that increases firstNumber by 1 at the end of each loop. The command that we have learned for this is ++firstNumber, which basically tells Swift to increase firstNumber by 1 at the end of each loop.
Immediately you will see that loop stops at the fifth time around. Why? Well let’s understand what we are doing here. At the beginning of the first loop, firstNumber is 5, and the condition is satisfied, so the 1st loop is initiated. firstNumber is printed, and then firstNumber is incremented by 1 to 6. Now it checks if 6, or our new firstNumber, is less than 10. Since it is, the loop commences for the second time. The loop continues like this until firstNumber reaches 10. Since 10 is not less than 10, Swift exits the loop, and 10 is not printed.
We can check what the console printed by clicking the eye and then the parallel lines.
As you can see, 10 was not printed out since the while loop exited the moment that the condition wasn’t satisfied.
Control flow and loops are extremely important concepts to understand. As an avid programmer, I have used for loops, for-in loops, and while loops an innumerable amount of times. Once you master these concepts thoroughly, you will be able to know when to use each type of loop in different scenarios.
That’s all from me this time. I’ll catch you guys later!