1 00:00:00,335 --> 00:00:04,345 There's another type of loop in JavaScript that's closely related to the while loop. 2 00:00:04,345 --> 00:00:06,440 It's called the do-whileloop. 3 00:00:06,440 --> 00:00:12,411 Now open the file do-while.js and copy the getRandomNumber 4 00:00:12,411 --> 00:00:17,821 function from while.js and paste it inside this file. 5 00:00:17,821 --> 00:00:22,909 In index.html, update the script tag source attribute 6 00:00:22,909 --> 00:00:29,572 to js/do-while.js The structure of a do-while loop looks like this. 7 00:00:29,572 --> 00:00:35,667 The do statement comes first and is followed by a while condition. 8 00:00:35,667 --> 00:00:41,514 Do-while repeats an action over and over again, while a condition is true. 9 00:00:41,514 --> 00:00:45,342 In other words, the code you place inside the do code 10 00:00:45,342 --> 00:00:49,704 block here will run as long as this while condition is true. 11 00:00:49,704 --> 00:00:51,880 That sounds pretty much like a while loop, doesn't it? 12 00:00:51,880 --> 00:00:52,820 It pretty much is. 13 00:00:52,820 --> 00:00:55,176 But there's one important difference. 14 00:00:55,176 --> 00:00:58,752 Do while will always execute the code block 15 00:00:58,752 --> 00:01:02,038 once before the condition is checked. 16 00:01:02,038 --> 00:01:05,999 That's because the condition isn't tested until after the code block. 17 00:01:05,999 --> 00:01:07,780 Let's compare the two. 18 00:01:07,780 --> 00:01:12,468 Earlier, you used a while loop to log 10 random numbers to the console. 19 00:01:12,468 --> 00:01:14,109 Here's the code you wrote. 20 00:01:14,109 --> 00:01:16,156 Notice that with a while loop, 21 00:01:16,156 --> 00:01:21,371 a condition gets checked at the very beginning, before this code block runs. 22 00:01:21,371 --> 00:01:25,230 If the condition is true, then the loop runs. 23 00:01:25,230 --> 00:01:27,722 If the condition is false, the loop does not run. 24 00:01:27,722 --> 00:01:32,511 The condition might be false right at the beginning meaning the loop will never run. 25 00:01:32,511 --> 00:01:38,241 Here's how you'd write this using do while I'll again initialize 26 00:01:38,241 --> 00:01:44,399 a counter variable to 0, to keep track of the number of times the loop runs. 27 00:01:44,399 --> 00:01:46,091 Inside the do block, 28 00:01:46,091 --> 00:01:51,640 I'll add the console.log statement displaying the random number. 29 00:01:51,640 --> 00:01:55,469 Then increment the counter variable by 1. 30 00:01:55,469 --> 00:02:00,249 With counter += 1, after each pass through the loop, 31 00:02:00,249 --> 00:02:03,646 the while condition will be evaluated. 32 00:02:03,646 --> 00:02:09,504 So between the parentheses I'll add counter is less than 10. 33 00:02:09,504 --> 00:02:14,033 Remember, do while does not check the condition until 34 00:02:14,033 --> 00:02:16,405 the code block has run once. 35 00:02:16,405 --> 00:02:18,868 If this condition is true, it runs again and 36 00:02:18,868 --> 00:02:21,407 again until the condition is no longer true. 37 00:02:21,407 --> 00:02:26,797 In this case when counter is no longer less than 10 I'll save my file. 38 00:02:26,797 --> 00:02:29,438 And when I preview index.html in the browser, 39 00:02:29,438 --> 00:02:31,888 10 random numbers appear in the console. 40 00:02:31,888 --> 00:02:34,246 Good. 41 00:02:34,246 --> 00:02:38,179 So when might you use do-while over while? 42 00:02:38,179 --> 00:02:42,444 Keep in mind that in many cases you can use do-while for 43 00:02:42,444 --> 00:02:44,774 the exact purposes as while. 44 00:02:44,774 --> 00:02:49,678 Use do-while when you need your loop to execute at least one time. 45 00:02:49,678 --> 00:02:54,306 For examples, log at least one random number to the console, then check if 46 00:02:54,306 --> 00:02:59,316 counter is less than 10 before running the loop again and logging more values. 47 00:02:59,316 --> 00:03:04,508 Use while when you need to check a condition before performing an action. 48 00:03:04,508 --> 00:03:09,186 For example check if counter is less than 10, before running the loop and 49 00:03:09,186 --> 00:03:11,202 logging a value to the console. 50 00:03:11,202 --> 00:03:12,772 Either way, you'll get the same output. 51 00:03:12,772 --> 00:03:16,055 So the approach you use is mostly a matter of preference.