Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie, we're going to learn to use Break with our loops. We've seen Break already. Remember, it was an important part of working with switch statements. We used Break to stop execution of any other cases, and then to exit switch function. We'll use it in a similar way with loops. Break will end execution of the loop. Not just the current iteration, but all iterations. You can think of it as breaking out of the loop. In the last movie, for the continue function I suggested the image of a Hollywood casting director saying next in the middle of an audition that was in progress.
Let's continue that metaphor here for a second. Break, would be if the casting director watched a few auditions and then said, this is the one. You've got the job, the rest of the auditions are canceled. I don't need to see any more. Break stops the whole process. And it's very useful when we've found what we're looking for or when we've reached a definitive answer to a question. We can stop looking, stop looping, and move on. Let's create an example. We'll start by taking basic.html I'm using Save As. And we'll save it as break.php.
Up here, we'll write break. And I'm just going to put in a simple loop. And I've got continue in there. This is the exact same loop that we had in the last movie. First, let's just bring up this version, and look at it in a browser. And remind ourselves what it does. Just save it, go to Firefox, and instead of continue now, I'm looking at break.php. So there it is. Notice that, the 5 is missing, because continue told it to skip over the 5. Instead of continue though, now let's tell it to break, so if it, count is equal to 5, it will break.
Come back here, and notice now it ended execution completely. It stopped not just this loop, and stopped from doing the echo statement, it ended this entire loop. It broke out of it. So once we get here and count is equal to 5, it executes break. And the very next thing it does, is it drops down here to the last curly brace and continues processing our PHP code from there. It never goes back an does any of these tests, or increments, or anything else. It just immediately exits our loop. Break will make your code a lot more efficient.
Let's imagine for a moment that you have 20,000 customer records, and we're looking for one record that matches a certain criteria. If we find the open that we want, when we get to the 100th loop, do we really need to keep looping for 19,900 more times? No, it's much more efficient to just break out of the loop and to say, I found it. We can stop looping and move on from here. Now, since you understand continue, I don't think we need to spend a lot more time with break. I think you get the idea behind it. But I do just want to show you that we also should take a look at what happens when you have a loop inside a loop using break.
Remember we did this with continue and we had this example or we had continue and we provided the argument of one or continue2, which told it to continue out of two loops. Well, let's look at this with break. Let's change this second one to be break. Right? Actually, let's remind ourselves, first of all, what this does. So, we'll just comment out this line and let's go back and reload the page. Okay, so that's what it looks like. That's what it does on it's own. Now, let's un-comment those lines and then when it gets to k equals to 3, it's going to break. There we go.
Notice that the 1-3, it did a break and at 3-3 it did a break. It didn't do the items that were after that but it did keep doing the first item. It did keep iterating and advancing i. Just like continue, if we provide an argument to it and say that we want to break out of two loops, it will break out of both of them. We have control over which ones we break out of by using an integer as an argument to break. Let's just try that so you see the difference. So here, we've basically said, all right, we're drilling down through one loop, we're using another loop, we finally found the item.
K equals to 3. That's it. So now I just break out of the looping structure completely. We don't need to keep doing anything else. You can go back and forth and change this number between 2 and 1, to see the difference between what it does. Now that we understand the three types of loops, and how to use continue and break with them, we'll be able to write very efficient looping structures. Before we move on, I want us to look at one more thing which is how array pointers work, and we'll do that in the next movie.
Get unlimited access to all courses for just $25/month.Become a member