WHILE AND DO…WHILE Loops

Hey guys welcome back in this video we will be looking at while loops and do while loops in PHP. So to kick things off, we will start by creating a new file and we will call this one though while do while loop. Now, originally, I would have split them into two. So I’m just going to actually merge these two, this link into one so it’s while do while loop dot php. And so I’m going to remove that reference here. And then I’m going to say sample wild slash Google wild. Now in our previous exercise, we would have looked at the for loop, and the for loop we would have characterized as a counter control because what it does is counts to a certain value and each time it checks If that value has exceeded another value, and so it decides to we or whether it continues or it exits the for loop is called a counter controlled loop. The while and do while loops over are called condition control loops because really and truly they aren’t commenting they aren’t concerned about counting. What they are concerned with is if a certain value meets a specific criteria, and our criterion and then it makes a decision, so we’ll just hop on over to our while do file and we will do our HTML template and we will give our document the cart type title which would be do slash sorry, while slash two while loop. Oops. Now I’ve prepped the page with two h1 tags, one for the wire loop section and one for the do while section So we will be looking at them in separate code blocks, but then we’ll see how similar they are and why I decided to put them all in one lesson. So let’s start with our while loop. The first thing we need is our PHP tag. And then the syntax for a while loop is that we see the word wild. And then we open or parentheses, and you will see that that’s a pattern every time you’re going to do a loop or certain kind of control structures, you just have that the parentheses don’t violate that rule, and you won’t have a hard time. So while and then what we need inside of these parentheses would be a condition. So the condition could be while some variable that C grade is less than 10. And then this would be my condition now it would kind of look very similar to the wild But the difference is that there’s nothing else that goes inside of these parentheses, it’s only a condition and a condition, once again, is comparing some value or variable on the left to some value, or even variable on the right. And this is the comparison tool. In this case we’re looking at, if it is less than it could be equal to equal, equal or greater then etc, etc. So I’m just doing a while this condition obtains, then curly braces, do some action. Alright, so I’m just going to declare my variable. And please note, if you would try to reference a variable without first declaring it that would be a problem. So I’m going to just initialize it to zero. And then we will see while greed is less than 10, we want to echo maybe a p tag, seeing I am less than 10. All right, so I’ll just put a p tag here and I am less than 10 exclamation sign alright and then close my feet are. So what this is expected to do is do this action while this condition is not true meaning as long as greed is less than 10 then it will continuously do this. Now, you will notice that when I browse this page, it will generate infinitely many of these echo statements and that would be because we will be locked in what we call an infinite loop. Alright, so an infinite loop comes about because there is no condition to or this condition would never get met. Because I’m not doing anything to greed. Greed is good is zero. I’m not changing the value of greed anywhere. And so greed will always be zero. So greed will always be less than 10 meaning this will always print So that’s one of the dangers with a while loop, you can run into that kind of error. Now, if I tried to browse to this page, I can expect a problem because the while loop will run almost indefinitely, will vary indefinitely until at least the machine or the server runs out of resources to perform the action. And so here we have it, it’s less than 10. And you can see that the machine is really struggling, because I’m getting that load inside. And if you look over the scroll bar is just continuously printing because the grade is just not ever going to be equal to 10. So I’m just going to close this quickly. And then I’m going to rectify the situation. So what we want is to ensure that our condition will get met. If it doesn’t, we are in an infinite loop. So I’m just going to leave a comment here and see in finite loop so that you know exactly what is happening there. Then I’m going to comment this out. And then I’m going to go below that and I’m going to write another while loop, that would probably actually, you know, exit at some point. So I’m going to repeat this. I don’t want to say grant less than 10. And then inside this, I’m going to carry out my action just the same. So I’m almost writing the same loop, except I’m actually going to deliberately on each run change the value of greed, alright. So that is another thing you can actually kind of simulate a count all right, but then that is a manual step. Once again, this is a condition control loop. So this does not facilitate the use of any counters. But then for our purposes, we can always count and and then change the value of grade or whatever method you want to use. You can use that method to change the The value of greed. So I’m just going to see greed. plus plus, as we saw in the for loop, the plus plus is like an auto incrementer. So what will happen is that greed starts off as zero, then it hits the while loop. And then this condition holds true, because greed is less than zero, less than 10. Then it echoes that, and then it increments and then it hits here and comes back and checks. The condition, again, is greed, less than 10. Greed at that point would be one, so it’s still less than 10. So it goes again, and does the same thing and over and over and over until greed eventually becomes 10, at which point 10 is no longer less than 1010 is equal to 10, not less than, so then it would exit the loop. So I’m just going to echo a quick statement here, so that we know when it exits the loop, and say, exit loop. All right, and so Winter rows to this page again. So when I click on the wild wild, I see that it ran 1-234-567-8910 times, and then it exited. So it did exactly what we expected because once it hit that condition, no longer being true than it exited. So now that we’ve addressed the wire loop, and what we call the while loop is a precondition loop meaning it checks the condition before it does an action which means if I started off with the value of greed, being 10, and then I refreshed that particular page, I would never ever get into the loop because guess what it checked the condition and it realized that well, Grady’s already 10 So clearly, I can’t do anything here. So it just went on to the next available statements. Alright, so we call that a precondition loop. Now if you’re in a situation where you need something to be done, At least once, but then repeated if a condition is not met, then we have our do while which is called a post condition loop. So I’m going to initialize another PHP block here. And as I say, you can initialize them almost as many times as you need to, of course, use your discretion and your, you know, creativity and your efficiency, of course, but when we initialize this block, what we want to do is say do and then we open our curly braces. So do and then inside the curly braces, we know by No, we always put whatever it is that the action should be for that particular control structure. So we say do open curly brace, and then we put in any action so I’m just going to print here. And I’m going to say I am do while loop just so we know exactly what’s being printed fire so I Do you see that there’s a little red squiggly line on the disk because we’re not finished. What we need to do after we have done this at least once is then check if the condition obtains. So I can reuse the same condition in the same syntax where I open the brace and then I see greed less than 10. And then I end with a semicolon. I’m sorry, I’m missing all the key word I did say that it is do while so I have to say do this, while conditions apologies. So we do an action while the condition so what will happen once again is that it will do it at least once. So I’m going to set this to zero. so great is zero. So the expectation is that it will do this while the greed is less than 10. And once again, if we do Call and then we will run into that in finite loop situation. So to avoid that, I’m just going to deliberately cheers a value off grid each time. So I’m just going to do that at the end of all the actions and I mark this value so that eventually it will meet the condition. So you don’t want to do that morphing in the middle of your actions. So let me just refresh this page and see what happens under our do while loop section. So we reset greed to zero. And you can see that greed is, is serving multiple purposes. So that’s one of the advantages to using variables. You can set it once and then use it multiple times throughout your code. So because it’s zero, our while loop is active once again, and our do while has done this action at least once. Now the reason for doing it at least once is the fact that by the time This one variable has finished with the while loop, remember that it would have incremented up to 10. So when it hits the do while loop, then its value is already 10. And so it did this at least once, then this went up to 11, because it was already 10 by the end of the for the while loop, sorry. And so when it hit the condition, this was 11. And that’s 10. So that does not hold true, so it would have exited. So once again to do we’ll do everything at least once and then check the condition and that’s why we call it a post condition loop. So I’m just going to add that comment right above the do while code block and say that it is a post condition loop. So what I’m going to do just to make sure that we get the most out of our do while loop here is in the block for the do while loop, I’m going to reset greed to be zero. So you can always set the value of your variable whenever you want. As you said it’s one, so up here, but I want it to be something else down here. So I’m setting it again. And then I will refresh my page. And what will happen is that the while loop gets the value zero and it loops and loops and loops until it’s 10. And then the do while resets it to zero, and then it will run 1-234-567-8910 times also Alright, and we could have put something outside to see exit loop, and then we would know when it actually finished executing. So I’ll just refresh. And there we go. Exit loop. Alright, so that’s pretty much it for the while and do while loops. You just need to know when to use Which one if you need something done at least once, use your do while if not use your while. And otherwise if you need to keep count of the number of times definitely like you definitely know how many times something should run, then therefore loop is probably best so you can use them at your discretion