1 00:00:00,700 --> 00:00:04,390 Before we start, did you try solving the workbook yourself? 2 00:00:04,420 --> 00:00:10,230 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:10,920 --> 00:00:15,500 In this solution, we're going to revisit Workbook 6.13. 4 00:00:15,510 --> 00:00:20,520 Instead of printing our rows one by one, we're going to use a nested loop. 5 00:00:20,520 --> 00:00:24,990 So here it tells us the outer loop needs to define this code. 6 00:00:24,990 --> 00:00:30,090 Simple enough, we'll say four and I is equal to zero. 7 00:00:30,630 --> 00:00:34,920 I is smaller than Price's dot length, so price is not. 8 00:00:34,920 --> 00:00:40,110 Length is going to represent the number of rows three plus plus. 9 00:00:42,180 --> 00:00:46,030 Where I is going to end up equaling zero one and two. 10 00:00:46,050 --> 00:00:47,700 So let's copy this over. 11 00:00:48,550 --> 00:00:51,940 When I equals zero, we're going to print baking. 12 00:00:51,940 --> 00:00:57,730 When I equals one, we print a beverage, one, I equals two, we print cereals. 13 00:00:58,540 --> 00:01:04,930 And now every time the outer loop runs through a particular row, we need an inner loop to run through 14 00:01:04,930 --> 00:01:07,030 every single element in that row. 15 00:01:07,030 --> 00:01:10,360 So here we'll say int j is equal to zero. 16 00:01:10,450 --> 00:01:13,990 J is smaller than prices. 17 00:01:13,990 --> 00:01:16,270 I length. 18 00:01:19,420 --> 00:01:20,650 J plus plus. 19 00:01:21,870 --> 00:01:27,930 So when the outer loop runs for one row, the inner loop is going to run through every single element 20 00:01:27,930 --> 00:01:28,710 in that row. 21 00:01:28,710 --> 00:01:32,550 And for every element we run through, we can print it. 22 00:01:34,060 --> 00:01:37,810 And after the inner loop runs to completion, we print a new line. 23 00:01:37,840 --> 00:01:40,300 Now, this might seem a little complex. 24 00:01:40,300 --> 00:01:46,270 It might seem a bit scary, but don't worry, once we visualize the runtime, it's all going to make 25 00:01:46,270 --> 00:01:47,590 a lot of sense. 26 00:01:50,630 --> 00:01:52,640 So here we've got a two RD array. 27 00:01:52,670 --> 00:01:58,250 The two RD array has three rows and each row has five elements. 28 00:01:58,640 --> 00:02:04,550 The outer loop is going to start with equaling zero, and it's going to keep running so long as I is 29 00:02:04,550 --> 00:02:06,530 smaller than the number of rows. 30 00:02:06,710 --> 00:02:10,139 So we start at equaling zero case zero. 31 00:02:10,160 --> 00:02:13,160 That means we're printing prices for baking. 32 00:02:14,840 --> 00:02:15,770 All right. 33 00:02:15,770 --> 00:02:20,270 And now each run from the outer loop is going to run the inner loop to completion. 34 00:02:20,270 --> 00:02:27,110 So this one is going to start off at J equaling zero, and it will keep running so long as J is smaller 35 00:02:27,110 --> 00:02:30,230 than the length of a row zero row I. 36 00:02:30,260 --> 00:02:32,450 So J is going to start at zero. 37 00:02:32,480 --> 00:02:35,180 Here I is zero and J is zero. 38 00:02:35,180 --> 00:02:37,220 So we're printing 1299. 39 00:02:38,670 --> 00:02:39,180 Here. 40 00:02:39,240 --> 00:02:42,150 I still zero, of course, but J is one. 41 00:02:42,150 --> 00:02:44,490 So we print 899. 42 00:02:45,410 --> 00:02:48,530 I still zero Jace to 999. 43 00:02:49,980 --> 00:02:53,130 I still zero J is three 1049. 44 00:02:54,410 --> 00:02:57,650 i0j equals four 1199. 45 00:02:57,650 --> 00:03:03,160 So I hope you can now see that each run from the outer loop runs the inner loop to completion. 46 00:03:03,170 --> 00:03:08,960 And now that we've printed every single element in row zero, we can print a new line. 47 00:03:09,470 --> 00:03:10,010 All right. 48 00:03:10,010 --> 00:03:16,700 And now this is the second run of the outer loop I now equals one, which means now we're going to print 49 00:03:16,700 --> 00:03:19,040 all of the prices for beverage. 50 00:03:20,300 --> 00:03:24,470 And one run of the outer loop is going to run the inner loop to completion. 51 00:03:24,470 --> 00:03:27,890 Once again, the inner loop starts off with J equaling zero. 52 00:03:27,950 --> 00:03:34,820 The loop is going to keep running so long as J is smaller than the length of row one because here I 53 00:03:34,820 --> 00:03:35,600 is one. 54 00:03:37,290 --> 00:03:41,310 Here we index row 1j0 $0.99. 55 00:03:41,940 --> 00:03:45,150 Row one element one 199. 56 00:03:46,400 --> 00:03:49,730 Row one element 2 to 49. 57 00:03:51,120 --> 00:03:51,480 Role. 58 00:03:51,480 --> 00:03:54,510 One element three 149. 59 00:03:55,720 --> 00:03:58,780 Row one element 4 to 99. 60 00:04:00,050 --> 00:04:00,470 All right. 61 00:04:00,470 --> 00:04:04,640 So now, after the inner loop runs to completion, we print a line of space. 62 00:04:04,940 --> 00:04:08,120 And this is the third time that our outer loop runs. 63 00:04:08,690 --> 00:04:14,510 This time I equals two, which means we're going to be printing all of the elements for row two. 64 00:04:17,779 --> 00:04:21,709 Once again, each run of the outer loop is going to run the inner loop to completion. 65 00:04:21,740 --> 00:04:23,780 J starts off at zero. 66 00:04:23,870 --> 00:04:29,860 The loop is going to keep running so long as J is smaller than the length of RO two. 67 00:04:29,870 --> 00:04:32,090 So long as J is smaller than five. 68 00:04:32,120 --> 00:04:34,160 J starts off at zero. 69 00:04:35,340 --> 00:04:40,290 Here we are indexing element zero 0 to 899. 70 00:04:41,940 --> 00:04:46,350 Zero two element to one 790 902. 71 00:04:46,350 --> 00:04:48,480 Element two 949. 72 00:04:48,600 --> 00:04:54,990 Row two element three 999 and RO two element four 1099. 73 00:04:54,990 --> 00:05:03,030 And after the outer loop runs the inner loop to completion, then we print a new line and now I a plus 74 00:05:03,030 --> 00:05:05,160 plus increases I to three. 75 00:05:05,160 --> 00:05:11,850 Three is not smaller than the number of rows three so our outer loop is going to break. 76 00:05:13,940 --> 00:05:18,290 Press continue terminating the runtime and we are done. 77 00:05:19,050 --> 00:05:21,330 I hope you enjoyed this breakpoint session. 78 00:05:21,330 --> 00:05:27,330 No matter how complex something may seem once you put your breakpoints and you start visualizing the 79 00:05:27,330 --> 00:05:32,190 runtime, it gives you a very clear picture of how your code is running.