1 00:00:00,150 --> 00:00:02,790 A nested loop is a loop inside of another loop. 2 00:00:05,820 --> 00:00:11,630 This for a loop is going to repeat code three times, but what if I placed another loop inside of it, 3 00:00:11,760 --> 00:00:14,220 one that also runs code three times? 4 00:00:15,270 --> 00:00:17,280 How many times is hello going to Prince? 5 00:00:19,120 --> 00:00:24,430 Before I break it down for you, I encourage you to pause, take out a pen and paper and try to trace 6 00:00:24,430 --> 00:00:26,590 the runtime of the code that I'm about to re display. 7 00:00:27,790 --> 00:00:29,070 I hope you figure it out. 8 00:00:36,000 --> 00:00:40,140 Hopefully that went well in this lesson, you're going to learn to use nested loops. 9 00:00:41,420 --> 00:00:44,990 First thing you need to do is create a new class inside your Section five folder. 10 00:00:45,350 --> 00:00:50,030 This file is going to be called nested loops and make sure the class has a main method. 11 00:00:55,340 --> 00:00:58,040 The nested loops as an outer loop and an inner loop. 12 00:01:00,540 --> 00:01:04,530 The outer loop is the big loop and the inner loop is the smaller loop inside. 13 00:01:06,830 --> 00:01:12,110 This print statement is going to run nine times because each pass from the outer loop is going to run 14 00:01:12,110 --> 00:01:13,490 the inner loop to completion. 15 00:01:15,020 --> 00:01:17,990 The first pass is going to run the inner loop three times. 16 00:01:21,330 --> 00:01:24,210 The second pass is going to run the inner loop three times. 17 00:01:33,570 --> 00:01:36,720 And the third pass is going to run the inner loop three times. 18 00:01:44,680 --> 00:01:47,350 In total, the nested loop ran nine times. 19 00:01:56,550 --> 00:02:01,890 So a nested loops Java, I'm going to start by making a loop that runs exactly three times the loop 20 00:02:01,890 --> 00:02:07,200 starts with the counter, I equaling zero, the loop is going to keep running as long as is less than 21 00:02:07,200 --> 00:02:07,670 three. 22 00:02:08,070 --> 00:02:11,250 And after each run, we're going to increase the counter value by one. 23 00:02:15,480 --> 00:02:20,370 And so every time this loop runs, we're going to print the value of the counter during that run, so 24 00:02:20,370 --> 00:02:25,980 system that out the print line run and I'll connect the counter value during that run, I. 25 00:02:38,950 --> 00:02:43,850 And great, we printed the counter during the first run, second run and third run. 26 00:02:44,560 --> 00:02:47,020 Now I'm going to write a for loop inside the current one. 27 00:02:53,420 --> 00:02:56,250 The loop starts with the counter J equalling zero. 28 00:02:56,720 --> 00:03:01,940 The loop is going to keep running as long as J is less than three and after each run, we're going to 29 00:03:01,940 --> 00:03:04,120 increase the counter J value by one. 30 00:03:04,880 --> 00:03:08,210 And every time this loop runs, I'm going to print the counter value. 31 00:03:13,850 --> 00:03:17,420 And before I explain what's going to happen, it might be worth it to just run the code and see the 32 00:03:17,420 --> 00:03:17,810 output. 33 00:03:27,260 --> 00:03:31,730 And as I expect, every pass from the outer loop runs the inner loop to completion. 34 00:03:33,780 --> 00:03:39,510 The outer loop started equals zero, which is less than three, so the outer loop runs Prince, the 35 00:03:39,510 --> 00:03:45,360 counter value for that run, eventually reaching the inner loop inside and now the inner loop is going 36 00:03:45,360 --> 00:03:45,960 to start a J. 37 00:03:45,960 --> 00:03:47,630 Equals zero, which is less than three. 38 00:03:48,090 --> 00:03:50,670 It's going to run print the counter value for that run. 39 00:03:50,940 --> 00:03:55,260 And the inner loop is just going to keep running until Jay isn't less than three anymore. 40 00:03:59,040 --> 00:04:04,200 And once that happens, the inner breaks and in turn that erupts up all the code for the first run of 41 00:04:04,230 --> 00:04:04,950 the outer loop. 42 00:04:05,550 --> 00:04:06,960 Now I equals one. 43 00:04:07,080 --> 00:04:11,280 And this process is just going to keep repeating until the outer loop breaks. 44 00:04:13,290 --> 00:04:18,420 The big picture here is that each run from the outer loop is going to run the inner loop to completion. 45 00:04:19,410 --> 00:04:22,830 Now, I'm going to let this play in case you need more time to visualize what's going on. 46 00:04:22,830 --> 00:04:26,940 But if you feel comfortable what we talked about so far, then feel free to skip ahead in the video. 47 00:05:04,170 --> 00:05:09,120 OK, so what are some applications of nested loops, nested loops are useful when you're working with 48 00:05:09,120 --> 00:05:14,550 2D arrays, you haven't learned about arrays yet, so you're not going to see nested loops for the remainder 49 00:05:14,550 --> 00:05:17,890 of this section, but they are going to come back in the next section. 50 00:05:17,910 --> 00:05:18,930 I can promise you that. 51 00:05:21,110 --> 00:05:25,010 All righty, let's recap in this lesson, you learned how nested for loops are run. 52 00:05:27,330 --> 00:05:32,820 And a nested loop has an outer loop and an inner loop, each pass from the outer loop runs the inner 53 00:05:32,820 --> 00:05:33,750 loop to completion. 54 00:05:45,950 --> 00:05:51,010 And that's really it, good job you covered everything there is to know about loops in Java. 55 00:05:51,770 --> 00:05:54,570 It's time to build the sections Project Pocari Areto. 56 00:05:54,620 --> 00:05:57,380 It's almost poker if you feel confident. 57 00:05:57,400 --> 00:06:01,010 But we talked about so far, then you can feel free to skip to the next video. 58 00:06:01,310 --> 00:06:06,740 But if you want to visualize the nested loop one more time slowed down without me speaking over it, 59 00:06:07,040 --> 00:06:08,940 then I'll play it for the next two minutes. 60 00:06:09,380 --> 00:06:10,130 All right. 61 00:06:10,130 --> 00:06:11,120 See you soon.