Start course
2h 50m

This course will provide you with a comprehensive understanding of the fundamentals of Swift. We're going to learn about variables, constants, arrays, dictionaries, sets, if statements, and more! 

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills


To get the most out of this course, you should have some basic knowledge of iOS.


Hi. Within this lecture, we're going to learn about a new loop called the for loop. Again, we will use this for loop in order to do some operation repetitively until a condition breaks. So, let me rename this to for loop and we're going to start writing in here. So, for loop basically works perfectly with arrays. So, you're going to see a lot of examples where we use for loop with arrays. And in fact, I'm going to tell you with an array example so that it will be on your heads. For loops go perfectly in synchronized with arrays. So, let me create an array named myFruitArray and I'm going to name this Banana, Apple, maybe we can add one more, like Orange. So, we have an array with three elements inside. So, let's suppose that I have to print out these three elements. So, how do we do that?

I believe right now, you know the answer, right? We have seen this. So, let me comment out this prints that we have been doing so far, so that the logs will be clear when we print something. And let me run this in order to just delete everything from here. So, right now I can easily say bring me the first element. You don't have to do this. myFruitArray[0] and I can just call print myFruitArray[0] And then I can copy and paste this line and just say myFruitArray[1], [2], and this will bring me Banana, Apple, and Orange in the log. So, that was easy. So, what would happen if I had 1000 fruits in my fruit array? Then I will be copying and pasting this for 1000 times and it won't make sense to do such work. So, we don't do it this way we use a for loop in order to have every element do something in our arrays. So, you start with four keyword. And you choose a variable name. So, I'm going to call this fruit, but it doesn't matter. You can call the distinct anything you want.

I'm going to explain what it does later on. Right now, you have to add one keyword in. So, this in keyword after this, in keyword you have to say in what in your array. So, choose your array and open curly braces, and it will create a coding block for you. So, what does this do let me explain. So, we're doing this inside of our myFruitArray, and this loop will take these elements one by one and assign it to be a fruit. So, in the first loop, the first the fruit will be Banana. In the second loop, it will be Apple. And the third loop, it will be Orange. So, if I print this fruit, see what will happen. We got the exact same result over here. So, let me explain one more time what happened here. So, it started with iterating through this element. So, it took Banana and assigned it to be fruit. 

So, we printed out Banana, and then it went to Apple, and then it went to Orange. And this continues as long as there is an element inside of an array. The condition here is that there is an element; if there is not, the loop breaks. So, we went through this loop three times, and then it broke. So, let me do another example. Let me create my numbers' array, and it will be some integer type- 10, 20, 30, 40, 50, 60. Can we do for looping in an integer array? Of course we can. And it would make much more sense to you right now, because we're going to do some mathematical operations for each element inside of this array. And as a practice, I suggest you pause the video and try to come up with a for loop that takes these numbers and divide them by five. So, you're going to have to divide each element inside of my numbers' array with five. I hope you got it by yourself. But if you couldn't, it doesn't matter. 

I'm just going to show you how to do that. I'm going to create another for loop and I will call this numb or number. In my numbers, I will open my curly braces, then I'm going to print out num / 5. So, this will take num. This will take every element and assign it to be num and divide it by five. As you can see now, we can see the result. As you can see by using for loops, we can do things very efficiently. When you use an array in your app, I believe there should be or that would be probably a time in which you have to use for loop, because they bring so much efficiency in system and this is not the only place that we can use for loops. In fact, we can use for loops by their own as well. We don't need some array, but it would make much more sense to use this with arrays. We generally use them for array iterating through purposes. Let me do another example. I'm going to call this myNumber. I'm going to say in 1... 5. So, what does this mean? It means that create an array like object, and it will come from, it will start from one and it will end in 5. So, let me change this, by the way, because this is going to be a messy in here. We named everything myNumbers. 

So, let me call this mynewinteger, and let me run this. As you can see, we got the result of 1, 2, 3, 4, 5. So, we can do stuff like this, we can just multiply it by five as well. So, we will get five, 10, 15, 20, 25. So, as you can see, we can do it like this without arrays, but it doesn't make sense. But with arrays it makes much sense. It's great to use for loops with arrays, and we're going to do that a lot. Just don't forget that when you use an array, if you do something with each element of it, you must use for loops. And if you feel kind of lost right now, it's perfectly normal. You are just starting to learn about a new language and new development environment. It's perfectly normal. We're going to do a lot of examples. We're going to build a lot of apps in which we will practice all of this information. Right now, you're just learning, learning, learning, and you're not practicing at all. Don't worry, we're going take care of it. So, let's stop here. And within the next lecture, we're going to use some if controls.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics