2.3 Control Flow Part 1: Loops
As with any language, you are eventually going to need to control the flow of your application. One of the more common ways to do this is through the use of loops. Loops allow you to repeat sections of your code for a certain number of iterations. In this lesson we will cover the basic types of loops within the Swift language.
1.Introduction4 lessons, 26:24
2.Language Constructs12 lessons, 1:51:34
3.Swift and Object Oriented Programming5 lessons, 27:52
4.Built-In Types5 lessons, 42:18
5.Conclusion1 lesson, 01:19
2.3 Control Flow Part 1: Loops
At this point, I think it's gonna be necessary for us to start to understand some control flow within our application. Meaning, what happens if we wanna be able to execute a number of lines, a certain amount of times, or I want to be able to do some operation a bunch of different times based on some sort of rule. Or based on some sort of expression, or conditional statement. So let's take a look at that. We have a couple different options within the Swift programming languages that are fairly common in other languages as well. And we're gonna start with the concept of a for loop. So there's two different types of for loops within the Swift programming language. We could do the more conventional one that's been around for quite some time, which is going to say all right, I'm gonna use the for statement, or the for keyword. And then I'm going to create a variable. Very similarly to how I would in other, in other instances. So I can say, for var index equal to 0. And now I need to give it a condition for our, for what is the rule that's going to allow this loop to continue firing. So I need to specify a semicolon. So this is one of those few re, a few times where you do need to use that semicolon so you can specify all these different pieces of the for loop. So the rule for this for loop to continue firing, is as long as index is less than 10, then continue firing. And then we need to give it some increment steps, so how does this for loop increment its index? And this is a very useful construct to use when you know that it's going to, that this particular value is going to increase or increment in a consistent way. So, in this case, we're gonna say index is always going to increase by one. So, I can do that a couple of different ways. I could say index plus equal to 1 that's gonna be valid. Or I can use the shorthand of plus, plus that's just going to increment that particular var, that particular variable by one every single time this runs. And then I'm going to provide the curly braces and that's going to provide the scope to this particular for loop. So everything that I do within here is going to be within the context of this value of index that's provided up here. So at this point, I can do any sort of operations. I could do some complex math, I could, you know, go hit databases, call web services, all sorts of complicated things. Or in our case, I can merely just say, all right, what is the value of index? And as you can see on the right-hand side, that this is executed 10 times. And I can come and take a look at my value history, and you're going to see exactly what has happened via this graph. But if you don't wanna continue looking at your graphs like that, you can use the built in method called print line. And merely print out the value of index each time. So if I were to come in here and take a look at this, I don't have to look at that graph anymore. I now have the actual values of the index variable. So as you can see, it goes from 0 to 9 and once it's incremented again, it hits 10, but that breaks the rule of it being less than 10. So it stops and goes about the flow of the application. So that's the more common version of the for loop, but a number of other languages have adopted the for in loop, which definitely is existent within the Swift programming language as well. Now the for in loop, the structure is a little bit different and we did kind of introduce this ever so briefly in a previous lesson. But let's see what this looks like. So let's say that we have a collection of data, so let's say maybe we have an array or some sort of range of data. So let's go ahead and create a very simple array and we're gonna talk about arrays a little bit later, when we get into the standard template library. But just take, for example that we have an array, and this is going to be equal to, and we'll just say 1, 2, 3, 4 and 5. Now this is some arbitrary number of values that are in this array. I'm explicitly giving it five, but you could maybe retrieve values from a database or from a web service. And, so we're not gonna know how many values are in this particular array. But what we do know is we want to be able to iterate over all of them and treat them all in a certain way regardless of how many there are. So we could kind of contrive together the previous example of the for loop to go through that in a similar fashion. But there's a better way to do it. We can use the for in loop to say I want to loop through all of the values within this array and do something with them. And I don't wanna have to reference them by indexes or anything like that, and just give me all the values one at a time and I'll do some sort of operation. So, in this case, we're going to specify the name of the variable we want to refer to each of the values within this array. So in this case, we'll just call it val. And we'll say for all the values that are in this particular array. So now I'm going to loop through all of the values within this array, and within the curly braces, each one of these individual values will, in turn, be stored into val. So in this case, I can print line and I can print line val. So as you can see here, now, I've printed 1 through 5 and it's pulled them all in order to do some sort of operation to them within this loop. So that's a very nice convention, an easy convention to use if you don't necessarily know how many of those values there are, or how you're going to increment them, and it doesn't really matter. All you wanna do is loop through all of them and work on them individually. So those are the two basic for loops. The other loops that are common within most programming languages and Swift as well is the concept of a while loop. So while is beneficial if you need to go through some sort of operations over and over again, based on just a rule or a couple of different rules each time that you're going through the iterations. So, let's take a look at what that might look like. So, let's say we have a var index and this starts out to be the value of 10. Now, we wanna do some sort of operation, regardless of what it is, somewhere else. Like I said, going to the database, calling web services, accessing local functions of your phone or device, or whatever it is you're working with based on some sort of rule. So in this case, I'm going to say well, I want to do something while index is less than 100. So we'll do our curly braces again, and in this case, we can do whatever it is we want in here, and we don't have to be iterating over a collection, which is kind of outside of what we're trying to do here. We just wanna be able to do something over and over and over again. But one thing to keep in mind here is, we can say, all right, I want to, you know, do a print line here. I wanna see what the value of index is. But if we do that, you're gonna see that this is basically causing an infinite loop. This is gonna continue to run forever. And before we start to get into trouble, I'm gonna stop this. So as you can see, in just that little amount of time, that particular while loop ran over 10,000 times. So one thing to remember when you're working with a while loop, if you're specifying something like this where you're going over index. Now we, we're not doing anything to change index, which is a problem. So if we wanna do something this way, we're gonna have to put in an incrementer to say, all right, I wanna do this while index is less than 100. But I have to do something with it. So now I'm gonna increment index. And then we'll come in here again and we'll just do another print line, and we're gonna print line index, and theoretically this should run 90 times. And as you can see here, it definitely ran 90 times and we're gonna have 10 through 99 here. So that's the basic concept of a while loop. But now let's say for instance, I come up here and I change index to start off at 100. Now as you can see here, it skipped over this while loop because coming into it, index did not, the value of index did not fall within the range or whatever we were trying to do within this statement, within the while loop, that was dictating how many times this is going to run. But let's say it's important that this while loop always runs at least once. So, how can we change this? Well, the while loop can morph itself into something called a do while loop. So let's go ahead and cut this out. So we're gonna say well yeah, I understand that it breaks this condition, but I need it to run at least once for some reason. So we're gonna say that we want to do this and we're gonna paste in this print line. And we're going to increment the index by one again. And we want this all to run while index is less than 100. So as you can see here, this only ran once because I executed what was in the do loop before I checked the while. So if I were to bump this back down to 10 ,you're gonna see that this is gonna run again 90 times because it ran the first time and then goes through the process of using the test within this while loop. So those are the basic control structures, the looping structures between the for and the while loop that you're probably gonna spend most of your time using within your applications. But the next thing we're gonna have to take into consideration is being able to check for certain conditions. And if certain rules are true or if certain things are true, we, how we can split off the logic of our application based on those particular condition.