While Loop
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 start learning about loops. So, loops are great tools to implement some operation repetitively as long as a condition holds. So, we're going to see what that means in real examples in a minute. So, let me create a new playground over here and name it Loops. So, I'm going to go over to my desktop and save it, save this into my IOS Complete folder. And now we are ready. So, we're going to learn about this loop. So, we're going to perform an operation in a repetitive way as long as a condition holds. Okay. And the first loop that we're going to learn is called while loop. So, we're going to say while this condition holds, do the following. And in order to do that, we have to learn about some mathematical notations in Xcode, in Swift actually. So, let me say that I have a number, and this number is 1, so I can add some new thing to this number by saying that myNumber is now myNumber + 1. 

So, if I call myNumber right now, what will happen to myNumber? It will be 2. Right. Because It will add 1 to 1 and it will find 2. So, this is easy, and we have another way of doing this, and it is even easier. So, you can do it like this, += 1. And these two lines are exactly the same. So, it means that myNumber = myNumber + 1. So, if I do this one more time, and if I call myNumber, what will happen to myNumber? So, it will be 3. Right. Because it started as 1 and we added 1, so we came up with 2 and now we added 1, and we came up with 3. So, why are we using this? Because I'm going to use this technique in the loops as well. So, right now you know how to add 1, or how to do this += thing, and it works for minus as well. So, you can say -= 1, and it will subtract one from myNumber. And at the end of this lecture, we're going to learn how to compare some numbers. For example, we have to compare myNumber with 1, how to see if myNumber is less than 1, greater than 1 and we will use this a lot during when we're building apps. Okay.

So, let's suppose that I have a number 0. So, this is another variable and its value is 0. And we're going to use this number inside of our while loop. So, basically we're going to use the keyword while. Okay. Call while, and after the while statement, you have to give your condition. So, we're literally going to say, while this condition holds, do the following. Okay. And maybe you realize that while is shown in pink and so var and so import, so we are importing a library here. So, these keywords are shown in a different color. So, these are predefined keywords so that we can understand something will happen when we use those keywords and again while is one of them. Okay. And we didn't really talk about library. 

So, what what does it mean to import some UIkit? So, UIkit it basically provides the basic functionality so that we can use Swift code, we can see the inputs and outputs, we can reach the string class, we can reach the integers and everything. Okay. We're going to import another libraries later on during our course. Don't worry about this right now. So, for example, I can do this, while number < 10. So, that's what I was talking about. We're going to compare this number. So, what does this mean? I say while number < 10, I open a curly brace and it closes it automatically. So, remember this creates a coding block for me and whatever I write here will be only called inside of while loop. So, it will be only executed inside of while loop. So, whatever I write here is only going to be executed while number < 10. So, for example, let me print the number and let me add 1 to my number. So, let's see what will happen. Let me run this and you will see what I mean. In the logs, you will see 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. So, that's fantastic. How did it happen? So, as long as number was less than 10, this went in a loop. 

So, it started with zero and it it was less than 10. So, it printed out zero and it added one to zero and it went back to the beginning. And it controlled one more time, one was less than 10 and it printed out 1 and it added one to the 1. It went up until 9. So, it printed out 9, and it added 1 to the 9 and it was 10 right now. So, 10 was not less than 10. So, our loop was ended. Okay. So, as long as number was less than 10, it got executed and once it was more than 10, it stopped being executed. So, if I change this to <=, it will also print out the 10 as you might see. So, you can change this number, and you can play with it, and you will see that as long as this condition holds, this will continue writing, this will continue printing. And as you can see whatever right inside of this block, coding block is only being executed inside of this while loop. So, we're going to learn about if later on, but let me create a character over here. 

So, I'm going to call this var characterAlive = true. So, I can check to see if my characterAlive condition holds. So, I can just go over here and say while characterAlive == true, do the following, right? So, it doesn't have to be an integer, it doesn't have to be less than, greater than. So, I can just say like this while characterAlive == true, then do the following. For example, you can print out. And why am I using two equation, two = signs rather than one? If I do == signs, it means that just check if they're equal or not. If I do = sign, it means that make it equal. Okay. So, when I use == signs, I'm checking for something to see if they are true or not. So, in this case, I'm checking to see if the characterAlive is actually true. So, while characterAlive == true, I'm going to print ("character alive") and of course I'm going to say characterAlive = false so this will get only just run one time, because it will change it to false, and it will stop doing this, and it doesn't make sense, I know. But if I don't do this, it will be an infinite loop. So, we will see an infinite number of character alive printing over here because it won't just break out of the loop, it won't stop the loop.

So, just for an example, I did that so that you would see you can use a Boolean, you can see an integer, you can even use a string in here as well. So, while this condition holds, and the condition doesn't even matter. As long as it makes sense obviously. So, right now let me show you the other checkpoints like we can do something like this, right? So, 3 < 5 . So, you can control to see if three is less than five, and it will give you a true Boolean. So, this is indeed less than five, and you can do something like this. 5 < 3 and it will give you false. So, this is less than. Of course you can do a greater than, okay, 5 > 3. You can go for <=, >= as well and you can do something like this == signs. So, important here >=, first > sign, later = sign. So, <=, < sign, = sign. And if you're checking for being equal, == signs. And actually we have something called != as well. So, you can control it like this ! and an = sign. So, it means that this is not equal to and as you can see we get true as the output of this. So, this is kind of cool, right?

We can use this kind of mathematical operations in our codes and we're going to use those a lot. And even if we're not using those with integers, we're using these with Booleans, with strings and everything. Later on, we're going to go a little bit deeper on this subject as well. Right now, I'm going to stop here. And within the next lecture, we're going to continue with another loop called the for loop.


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