 Hey everybody, this is Brian. Welcome to the 14th LAMP tutorial today. We're going to be discussing the while and do loops. Go out to w3schools.com and just look up the loops. You'll see the while loop loops through a block of code while a specified condition is true. The do loop loops through a block of code once, and then repeats a loop as long as a specified condition is true. The distinction here is very important. The do loop will always execute at least one time. Don't worry about the four in each. We're going to cover those in another tutorial. The while loop, remember this will evaluate a condition and then execute a block of code if the condition is true. An example of this would be you have a variable i equals one while i is less than or equal to five. Execute this code. Increment your value. Very important. If you don't, you will create what's called an infinite loop, which means this will just run infinitely forever. It will never stop until PHP or the server you're on crashes or just until somebody shuts it off. So as you can see, the number is one, two, three, four, five. Once it hits five, that's no longer true. So dumps it out and you're done. The do loop, remember, do will always execute. And then you've got your condition. So what happens here is it says do, run this block of code, evaluate the condition. If that condition is true, jump back up to do, run it again. If it's false, then just stop. Very important distinction is that the do loop will always execute at least one time. And here you see the code they're using. Exact same, do, increment, print out the value, and then evaluate. And the result is two through six. Now let's actually jump into our IDE and let's play around with this a little bit. So we're just going to say, well, condition. And I tend to write out my skeleton first. So we'll say less than or equal to five, echo out the value. And concatenate a HTML hard return or break as it's called. Then very important, do not ever forget to increment. Create an infinite loop. Now, why would you want to create an infinite loop? Well, if you're making like a socket program, which you know is how programs talk over the internet. Well, you don't know when that connection is going to close. You just constantly loop until the program is over with. All right, now we're going to echo out, finished, run your program. And you can see how it says one, two, three, four, five, finished. So let's examine this very quickly. We have a variable with the values one. So one is less than or equal to five. That's true. Runs this code because it was true. It loops back up here. Notice how we incremented it. So it's now two and then three and then four. And then once it hits five, five is less than or equal to five. Runs it one more time increments it to six. Six is not less than or equal to five. So then it's just finished with this loop. Over the block of code. It just ignores all of this and jumps immediately down to here. That's how a loop and exits work. See one, two, three, four, five, finished. Now remember our discussion on the break command and ifs. You could actually say if I equals three break. Now what do you think is going to happen here? This is a pretty interesting combination here. We've already learned the if statement. So we're looping from one to five. But here we're saying if our variable is equal to three break. Remember break will remove itself from the current context. Run this. See one, two. It loops it through. I is equal to three. So it says, you know what? I'm done with you. And just breaks out and jumps right here. That's how you exit a loop is using the break command. Pretty simple to understand. Although that's somewhat of the elusive obvious. Some people have a hard time grasping that. Now remember if you're going to use an if statement, never do this. Because then what you're saying is every time this loops, set I to three. So you've created an infinite loop. It's just going to constantly loop. Why? Because when this is evaluated, it's saying if it's less than or equal to five, run this code. Even though you're incrementing, you're resetting it to three. So it will always be three. It'll go one, two, three, and then it'll just stay three for infinity. So never do that. Now we're going to examine the do loop. And we're going to say do. And we're going to say, well, whoops. Notice the semicolon at the end. And we're going to say, well, I is greater than zero. So what we're going to do here is we're going to loop through this while I is greater than zero. Now we're going to do the exact opposite. Instead of counting up, we're going to count down. So we're going to spit this out here. I'm a big fan of copy and paste if you haven't noticed. And we're going to decrement, meaning we're going to reduce that value. Let's actually throw some comments in here. This is starting to get a little cluttered. Do loop, while loop. And let's expand this out. We'll say 10. We're going to put this at five. So basically this will loop. But if it hits five, it's just going to break out. So that kind of pointless to have it. I'm just doing it for illustrative purposes. So you know how to break out of a loop. And then we're just going to do a do loop. Now this will run. Get down to here, print out finish just like we did. And then it's going to say do. Run this block of code. Then evaluate the statement. If the statement's true, it's going to jump back up here. Now notice how we're decrementing. What that means is we're taking one away. So we're going to start at, you guessed it, five. And then reduce until we get to zero. Once we hit zero, it's just going to finish out again. So let's look at our example and see what happens. You guessed it. One, two, three, four, finished. Because we broke out. And then notice how the value is suddenly five. The reason why the value is five is because right here. We're saying if I equals five, which in this case it would, because we've incremented up to five, then break. We executed that. Now our value is five because we're still using I. This is always executed. So it's five, four, three, two, one. Once it's here and it's one and we decrement it's now zero. So that's no longer true. It jumps back out. Pretty easy to understand. Hard in practice. You just need to keep practicing, use it as much as possible. Loops will save you a lot of time and a lot of code. They're there for a reason. They're good for you. And they taste good with milk. I'm just kidding. It's a pain. Making sure you're paying attention. Once again, you could actually throw an if statement in here and break out. So we'll say if, and let's get creative here. If I is less than two. Now I want to show you a different way with the if statement. Notice how we don't have the brackets here. It's all nice neat on one little line. That's called a single line condition or a single line if. What we're saying is if this is true, run this code. Now, if there's a bracket here, it'll execute the code block. But because there's no code block, we're just running it all on one line. Save and run. Let's actually pull this up so you can see what happens here. Do and then echo, decrement, and then if I is less than two, break. So we're saying five, four, three, two. Remember it's here. Gets to one and boom. You guessed it. Breaks right out of there. Pretty easy to understand. I would recommend you actually practice loops. Never, ever, ever forget to increment or decrement your value or you will create an infinite loop. That's one of those little gotchas with programming. That's all for this tutorial. Thank you for watching. I hope you found this educational and entertaining.