1 00:00:00,170 --> 00:00:05,730 You've learned three different loop types, while, do while and for loops. 2 00:00:05,730 --> 00:00:09,430 In each of those types, there's some kind of exit condition. 3 00:00:09,430 --> 00:00:13,100 In other words, when a certain condition is no longer true, 4 00:00:13,100 --> 00:00:17,940 like a counter variable is no longer less than ten, then the loop ends. 5 00:00:17,940 --> 00:00:21,060 But that's not the only way you can exit a loop. 6 00:00:21,060 --> 00:00:26,160 JavaScript includes a break statement, which when encountered inside a loop, 7 00:00:26,160 --> 00:00:28,540 immediately exits that loop. 8 00:00:28,540 --> 00:00:30,690 Let me show you how it works. 9 00:00:30,690 --> 00:00:34,890 Let's take a look at the do while example from video four. 10 00:00:34,890 --> 00:00:38,990 If you want to follow along, click the Launch Workspace button on this page, and 11 00:00:38,990 --> 00:00:41,030 open the script.js file. 12 00:00:42,470 --> 00:00:47,470 Remember that do while loops run at least once because the test condition only 13 00:00:47,470 --> 00:00:50,350 happens after the loops code block. 14 00:00:50,350 --> 00:00:51,540 In this example, 15 00:00:51,540 --> 00:00:56,250 I used a do while loop to make sure that the prompt appeared at least once, but 16 00:00:56,250 --> 00:01:00,280 could appear multiple times until the player got the answer correct. 17 00:01:00,280 --> 00:01:04,690 Here the test condition checks to make sure that the correct guess variable is 18 00:01:04,690 --> 00:01:06,210 not true. 19 00:01:06,210 --> 00:01:10,900 Remember this exclamation point is called a not operator which inverts the meaning 20 00:01:10,900 --> 00:01:12,120 of the condition. 21 00:01:12,120 --> 00:01:15,660 So true is turned into false and false into true. 22 00:01:15,660 --> 00:01:20,060 In other words, this loop repeats until the correct guess value is set to true, 23 00:01:20,060 --> 00:01:23,150 which happens here as part of a conditional statement. 24 00:01:23,150 --> 00:01:25,700 But there's another way I could have written this. 25 00:01:25,700 --> 00:01:28,260 I'll first change this to a simple while loop. 26 00:01:29,850 --> 00:01:32,030 For the condition I'll just type true. 27 00:01:33,430 --> 00:01:35,850 In general, this is a really bad idea. 28 00:01:35,850 --> 00:01:40,680 As I mentioned in an earlier video, this creates what's called an infinite loop. 29 00:01:40,680 --> 00:01:45,650 True is always true, so there's no way that this condition will ever become not 30 00:01:45,650 --> 00:01:51,720 true, and the code inside here will run on and on and on and on. 31 00:01:51,720 --> 00:01:56,180 However, I can use a break statement to break out of this loop. 32 00:01:56,180 --> 00:01:59,180 I just need to add it inside this conditional statement. 33 00:02:00,610 --> 00:02:02,830 Now there's a way to escape the loop. 34 00:02:02,830 --> 00:02:07,560 This program will keep asking the user to guess the correct question until they 35 00:02:07,560 --> 00:02:10,660 input a number that matches the random number created at 36 00:02:10,660 --> 00:02:11,670 the beginning of the script. 37 00:02:12,740 --> 00:02:17,010 When it does match, they've got the answer and it is time to break out of the loop. 38 00:02:17,010 --> 00:02:18,470 Let's see if it works. 39 00:02:18,470 --> 00:02:21,440 I'll save the file and preview the workspace. 40 00:02:23,190 --> 00:02:24,580 It works the same as before. 41 00:02:26,140 --> 00:02:30,020 You don't have to use a break statement in an infinite loop like this. 42 00:02:30,020 --> 00:02:34,660 In fact, most of the time you'll use break along with a regular loop. 43 00:02:34,660 --> 00:02:40,090 For example, we could change this game so that it only allowed ten guesses. 44 00:02:40,090 --> 00:02:42,820 A simple way to do that would to be to use a for loop or 45 00:02:42,820 --> 00:02:47,090 a while loop with a counter variable to keep track of the number of guesses. 46 00:02:47,090 --> 00:02:51,800 For example, I already have a counter here, the guess count variable. 47 00:02:51,800 --> 00:02:53,174 So I'll change this part to. 48 00:02:53,174 --> 00:02:56,822 [BLANK_AUDIO] 49 00:02:56,822 --> 00:02:59,995 Now the loop will either run ten times, or if the user 50 00:02:59,995 --> 00:03:04,990 guesses the correct number first, the loop ends because of the break statement. 51 00:03:06,920 --> 00:03:09,750 Then we can use a conditional statement to test whether 52 00:03:09,750 --> 00:03:12,510 the player correctly guessed the number. 53 00:03:12,510 --> 00:03:15,520 If so, we'll print out the message as it appeared before. 54 00:03:17,250 --> 00:03:21,310 Otherwise, we'll print out a sorry, you didn't guess the right number message. 55 00:03:21,310 --> 00:03:30,078 [BLANK_AUDIO] 56 00:03:30,078 --> 00:03:31,390 Let's see how this works. 57 00:03:33,950 --> 00:03:37,070 Keep in mind that the break statement works very much like 58 00:03:37,070 --> 00:03:39,540 the return statement in a function. 59 00:03:39,540 --> 00:03:44,680 Any code inside the loop that appears after the break statement will never run. 60 00:03:44,680 --> 00:03:49,120 Now you know a lot about JavaScript loops, the different types, how they work, and 61 00:03:49,120 --> 00:03:50,820 how to break out of them. 62 00:03:50,820 --> 00:03:54,110 It's time for you to put that knowledge to work. 63 00:03:54,110 --> 00:03:58,550 In the next video, you'll be tackling a program that could really use some help. 64 00:03:58,550 --> 00:04:01,520 It has a lot of repeated code that doesn't have to be there. 65 00:04:01,520 --> 00:04:05,120 In fact, you'll use a loop to make the program a lot better.