‫Okay guys, now we're going to get into loops and iteration.
‫And loops are used to repeat a block of code a certain number of times.
‫And this is useful when you want to do something a certain number of times, or when you want to do
‫something for each item in an array.
‫And the reason why I wanted to jump into loops now is because we just looked at arrays.
‫And arrays are a great case for loops.
‫We can use them to iterate over each item and then do something with that item.
‫So we'll get into looping over arrays soon.
‫But I just want to start off with some basic loops.
‫And in this video we're going to look at three types the for loop, the while loop and the do while
‫loop.
‫So let's look at the structure of the for loop first before we jump in and create one.
‫So basically we have a which looks like a function.
‫We have the word for and then some parentheses and then some curly braces.
‫Now in these parentheses we have three parameters.
‫First is the initialization.
‫This is where you set the starting value of the loop.
‫The second is the condition.
‫This is where you set the condition that must be met for the loop to continue.
‫And next we have the update or the increment.
‫And this is where you set up how to update the loop variable each time the loop runs.
‫And in many cases you'll increment the loop variable by one or by however many increments.
‫So that's kind of the outline of a for loop.
‫Now let's jump in and create one.
‫So I just have the HTML template here.
‫We're going to go to the top for now because I want to show you I also want to show you how to loop
‫and output from within the HTML, because you'll do that quite a bit when dealing with arrays that come
‫from a database.
‫So let's say PHP, let's close our PHP tag, and then we're just going to do a very, very simple for
‫loop.
‫So just like in the the slide we're going to have four and then three arguments.
‫Now these are separated by semicolons not commas like functions.
‫So keep that in mind.
‫So the first one is the first one is the initialization.
‫So we want to initialize a variable.
‫So I'm going to call this I that's the common convention.
‫And we're going to set that to its initial number which in this case is going to be zero.
‫All right.
‫So that is the initialization.
‫Then I'm going to put a semicolon.
‫And then after that we want the condition.
‫So for this particular loop I'm going to say as long as I is less than ten okay that's that's the condition.
‫Now we need the update which is going to be I plus plus oops I plus plus which just means increment
‫by one.
‫So it starts at zero.
‫It executes the code.
‫It checks to see if it's still less than ten.
‫If it is it's going to then set I to one.
‫It's going to increment by one.
‫Then it'll run the code check to see if it's still less than ten.
‫If it is, then it'll go to two and it'll do that up until this is this is not met anymore okay.
‫So what do we want to do within here.
‫Well I'm just going to go ahead and echo out whatever I is.
‫Let's do I and then let's concatenate on a line break.
‫Just so just so it's not all scrunched together.
‫So let's go ahead and reload.
‫And now you'll see what happens.
‫Is it prints out 0 to 9.
‫Now it doesn't go to ten because of the condition.
‫It's as long as it's less than ten.
‫Ten is not less than ten.
‫So it's not going to show ten.
‫If I did this, if I said less than or equal to ten and then run it, then it'll actually show the number
‫ten.
‫All right.
‫If I had set the initial value here to five and then reload, then it's going to go 5 to 10, okay.
‫Because it started at five.
‫So pretty simple.
‫I'm just going to set that back to zero.
‫And I'm going to comment this out for now because I want to show you before we move on to the next type
‫of loop, I want to show you how we can do that.
‫Do it within the HTML.
‫So down here where it says output, let's create a ul, an unordered list.
‫And I'm going to open up my PHP tags.
‫And I'm going to say for.
‫And let's put in here the same, same thing we did above.
‫We'll set I equal to zero.
‫We'll say as long as I is, we'll say less than or equal to ten.
‫And then we want to increment I by one.
‫So I plus plus now instead of doing this you know because we can't we can't have multiple lines down
‫here.
‫I mean we can but it will look really ugly.
‫So we can do a shorthand which is using a colon.
‫So if I do that then I can come down here and to end it I can say PHP end for like that.
‫And then anything I do in here will run within the loop.
‫So let's output a list item and we'll just say number colon.
‫And then I'm going to just output.
‫Actually we can just do this.
‫I'm going to output whatever I is.
‫All right.
‫So let's go ahead and reload that.
‫And we get number colon.
‫And then Z starts at zero.
‫Goes through looks at this condition.
‫If it's not being met it'll I mean if it is met it'll go again until it's not met anymore.
‫So it goes up to ten.
‫All right guys.
‫So let's now do a while loop.
‫So I'm going to go back to the slides here.
‫So a while loop I mean we use a for loop and a while loop for a lot of the same things.
‫But they're set up differently.
‫So as you can see the while loop only takes in the condition the initialization happens before the while
‫loop, and the update happens within the while loop okay, after the code is executed.
‫So let's go ahead and set this up.
‫So I'm going to go.
‫Let me just put a comment here and I'll say basic basic for loop.
‫Then we'll go down here.
‫Basic.
‫While loop.
‫So as I said, the the initialization is going to go outside the loop.
‫So I'm going to say I equals zero up here just in the general scope and the global scope.
‫Then I'll have the while which takes in the condition.
‫So in this case it'll be as long as I is less than ten.
‫And then we have our code that we want to execute which I'll just echo out I and then I'll concatenate
‫onto that.
‫A line break.
‫And then underneath that, within the body of the loop is where we want to increment.
‫So we'll say I plus plus increment or update.
‫And then if I reload again we're going to get zero through nine.
‫And I'm just going to comment that out and show you that we can use it down below in the HTML, in the
‫template with kind of a shorthand syntax.
‫I don't do this very much.
‫I don't think I've ever used a while loop in this way, but I just want to show you that you can.
‫So let's.
‫Let's comment.
‫I guess we'll yeah, we'll comment all the you know what, I'm just going to replace it because I'll
‫have all the code, the final code for you guys in, in the sandbox and in the docs.
‫So what I could do is say PHP and then I'd set I equals zero.
‫And then while.
‫I is.
‫I is less than ten.
‫And then we could use a colon here.
‫And then down at the bottom we can say PHP.
‫Then we need our condition.
‫So I plus I'm sorry, not our condition, our increment or update I plus plus and then we can say end
‫while.
‫So it's kind of it's kind of messy.
‫I don't really like this, but you can do it.
‫Okay, so in here then we'll do a list item number.
‫And.
‫Then we'll just.
‫Yeah, I guess we'll just do I.
‫That should work.
‫Let's try it out.
‫Reload and we get the same thing.
‫All right.
‫Now we have a do while loop, which I almost never use.
‫But I mean, you can if you want.
‫The big difference between a while loop and a do while is a do while will always run whatever you have
‫in here at least once.
‫Okay, so a while loop and a for loop will check the condition first and then run if it if it needs
‫to.
‫A do while will always run it first and then check the condition to see if it'll continue to run.
‫So we have our initialization.
‫At the top we have this do block, and that's where we put our code to be executed as well as our update.
‫And then we have while and then the condition.
‫So let's jump back into our file here and let's.
‫Say basic.
‫And do while loop, so I'm going to have my condition.
‫I'm not at my condition, my initialization.
‫So I equals zero.
‫And then we're going to say do and we're not going to have parentheses here.
‫It's just do.
‫And then immediate curly braces.
‫Then we execute our code.
‫So let's say echo.
‫We'll just do echo I and then we have our update.
‫So I plus plus.
‫And then here is where we do while.
‫And then our condition which will be I is less than ten.
‫All right.
‫So we'll go ahead and reload and we get zero through nine.
‫I just want to show you if I put this to something above ten like 12 and reload, it does show 12 because
‫again it does run once.
‫So it runs whatever is in here.
‫So I is 12.
‫It echoes 12.
‫Even if I is a million it's going to echo it and then it's going to check okay.
‫So what happened here.
‫Was it printed out 12.
‫Then it checked to see if it's less than ten.
‫It's not.
‫So it didn't go any further.
‫If I set it to zero, it's going to run with zero.
‫Then it'll check.
‫It'll see that it's still less than ten and it'll run again.
‫And we get zero through nine.
‫And then if you wanted to use this down here, you could, but it's it'll get very messy.
‫But I guess I'll just.
‫Show you.
‫So if we do, I.
‫I equals zero and then do.
‫And then we.
‫We can't do this.
‫Like we can with for and while.
‫So you actually have to use your open and curly brace here.
‫And then we'll have our ending tags.
‫And then down here we would have.
‫Our AI plus plus.
‫And then we'd have to close the.
‫The do and then open the while, and then in the while we would have I is less than ten.
‫And then in here we'll have our list item number colon.
‫And then we want to just echo out the I.
‫All right, so that should work.
‫Let's reload.
‫And yeah, we get the same thing, but this looks super ugly.
‫All right.
‫So those are just basic loops.
‫Do while I'm sorry for while and do while now before we get into looping through arrays and stuff like
‫that, I want to get into nested loops because you can have loops within loops.