‫Instructor: Hi. Within this lecture,
‫we are 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 gonna 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 gonna go over to my desktop
‫and save this into my iOS complete folder.
‫And now we are ready.
‫So we're gonna learn about this loop.
‫So we are gonna perform an operation
‫in a repetitive way
‫as long as a condition holds, okay?
‫And the first loop that we are gonna learn
‫is called While Loop.
‫So we are gonna say while this condition holds,
‫do the following.
‫And in order to do that,
‫we have to learn about some mathematical notations
‫in Swift actually.
‫So let me say that I have a number
‫and this number is one.
‫So I can add some new thing to this number
‫by saying that my number is now my number plus one.
‫So if I call my number right now,
‫what will happen to my number?
‫It will be two, right?
‫Because it will add one to one
‫and it will find two.
‫So this is easy.
‫And we have another way of doing this.
‫And it is even easier,
‫so you can do it like this:
‫plus equals to one.
‫And these two lines are exactly the same.
‫So it means that my number equals my number plus one.
‫So if I do this one more time
‫and if I call my number,
‫what will happen to my number?
‫So it'll be three, right?
‫Because it started as a one and we added one,
‫so we came up with two,
‫and now we added one and we came up with three.
‫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 one
‫or how to do this plus equal thing
‫and it works for minus as well.
‫So you can say minus equals to one
‫and it will subtract one from my number.
‫And at the end of this lecture
‫we are going to learn how to compare some numbers.
‫For example, how to compare my number with one,
‫how to see if my number is less than one,
‫greater than one.
‫And we will use this a lot
‫during when we are building apps, okay?
‫So let's suppose that I have a number, zero.
‫So this is another variable and its value is zero.
‫And we are going to use this number
‫inside of our while loop.
‫So basically we are going to use
‫the key word while, okay?
‫Call while, and after the while statement,
‫you have to give your condition.
‫So we are literally gonna 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 does it mean to import some UIKit?
‫So UIKit 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 are 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 my number is less than 10.
‫So, that's what I was talking about.
‫We are gonna compare this number.
‫So what does this mean?
‫I say while number is less than 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, will be only executed inside of while loop.
‫So whatever I write here is only going to be executed
‫while number is less than 10.
‫So for example, let me print the number
‫and let me add one 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 zero, one, two, three,
‫four, five, six, seven, eight, nine.
‫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 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 one,
‫and it added one to the one.
‫It went up until nine.
‫So it printed out nine and it added one to the nine,
‫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 less than and equal 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 the writing,
‫this will continue printing.
‫And as you can see whatever I write inside
‫of this block, the coding block,
‫is being executed inside of this while loop.
‫So we are going to learn about if later on,
‫but let me create a character over here.
‫So I'm gonna 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 is 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 is true,
‫then do the following.
‫For example you can print out,
‫and why am I using two equations,
‫two equal signs rather than one?
‫If I do two equal signs,
‫it means that just check if they're equal or not.
‫If I do one equal sign,
‫it means that make it equal, okay?
‫So when I use two equal 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 is true,
‫I'm going to print characterAlive.
‫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 characterAlive 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 three is less than five.
‫So you can control to see
‫if three is less than five
‫and it'll give you a true Boolean.
‫So this is indeed less than five.
‫And you can do something like this:
‫five is less than three and it'll give you false.
‫So this is less than,
‫of course you can do a greater than, okay,
‫five is greater than three.
‫You can go for less than or equal to,
‫greater than and equal to as well.
‫And you can do something like this:
‫equal to, two equal signs,
‫so important here, greater than, equal to,
‫first, greater sign, later equal sign.
‫So less than and equal to, less sign, equal sign.
‫And if you're checking for being equal, two equal signs.
‫And actually we have something
‫called a not equal to as well.
‫So you can control it like this"
‫exclamation point and an equal 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 gonna use those a lot.
‫And even if we are not using those with integers,
‫we are using these with Booleans,
‫with strings and everything.
‫Later on we are 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 are going to continue
‫with another loop called for Loop.