Previous episode: 21. Challenge: For Loops
Next episode: 23. Challenge: Iterating Collections
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
So you've probably figured out by now that loops must be useful for more things than just counting up from zero to nine or printing strings a certain number of times. And you're right. Loops are really useful for processing data such as listed reviews, or hour by hour weather forecasts. Now, you've already worked with something that stores data in a linear way, and that's arrays. Arrays are a way to store ordered lists of items, and loops are a great way to process that order data in an orderly fashion. Now, in this video, you'll learn a few ways to process some arrays of data and you'll see how powerful and useful loops can be. So open up a playground page for this video, where I've given you some arrays of data. Now you're going to write a few loops to process this data and do some interesting things with it. At the top of the playground page, I've provided two arrays. One is for the days of the week, and the other is a measurement of the temperature of the pool on each of those days. Now, one of the simplest things you can do with these two arrays is to print out the pool temperature for each day in a nice format. So, since you want to go through, or iterate over, all items in that array, a for loop will be your best choice. So start by constructing your for loop with 'I' for index from zero to... What? (keyboard clicking) Well, you could define a constant name count up above and set it to seven, which is the length of each of those arrays. But, if you think back to when you learned about arrays, arrays have a series of properties that give you information about the array. And one of those is the count of the items in the array. Which you access using the count property, like so. (keyboard clicking) Now you can put a print statement in there to print out the day of the week and the pool temperature, in a neat fashion. (keyboard clicking) And I run my playground and, oh, I've got an error in the console. Okay. Well, what does it say? If I scroll up a bit and after the nice tidy list of temperatures and days, you'll see 'fatal error, index out of range', but you only iterated up to the count of items in the array, right? So why would you get this error? Remember that an array is zero indexed. So in an array of seven items, your top index is six, but the count property of day of the week gives you..? That's right. Seven. So to fix this, you need to stop short of seven and use? Right. The half open range with the less than operator. (keyboard clicking) Run it again. That's better. Now, each iteration through the loop, X code will print out the day of the week, followed by the temperature of that day. Now this works because both arrays are the same length and they're in the same order. Now, I only like to swim in the pool when the temperature is 80 or above. So I only want to print out the good swimming days where the temperature is 80 or above. (keyboard clicking) Rerun the playground. All right, hey, it looks like there's a few good swimming days this week. Now, this first line looks pretty clunky right here. It would be nice to write more compact codes since that would be easier for you and someone else reading your code to understand. So, let's say my friend is planning to visit me and wants to know what my average pool temperature is for the week. To get the average of a set of numbers, you simply add them all together, and divide by the count. So let me set up a sum variable and set it to zero. (keyboard clicking) If you want to operate on every element of a single array in this case, pool temperatures, Swift has a nice shorthand version of a for loop, like this. (keyboard clicking) You'd read this as for each temperature in pool temperature, temperature will be the value in each element of the array. Whereas before, you worked with the index, and got the elements value that way. Now, I simply sum each number in the array, like so, (keyboard clicking) and then outside the loop, I divided by the count of the elements in the array to find the average temperature. (keyboard clicking) Execute my, er, playground again. And the average is 79. The difference with this for loop is that I don't need to know the index of each element in the array, or even how many elements there are in the array. I just tell Swift I want to iterate over every element in the array, no matter how many there are. So that's all I wanted to show you in this video. There are more advanced ways to iterate over collections, but you'll cover that in later parts of this course. For now though, I have a short series of challenges for you, that will get you comfortable with iterating over collections.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.