1 00:00:00,410 --> 00:00:03,530 As I mentioned before, arrays and loops are like siblings. 2 00:00:05,440 --> 00:00:09,880 If you want to run through the elements of a normal array, you can use a simple for loop. 3 00:00:20,610 --> 00:00:25,030 But what about a tutee array in this case, you need a nested for loop? 4 00:00:25,470 --> 00:00:28,350 Oh, yes, we finally get to use nested loops. 5 00:00:30,300 --> 00:00:33,840 In this lesson, you're going to use a nested loop to run through a tutera. 6 00:00:36,630 --> 00:00:39,750 I want to start by looping through a single arrow in a tutera. 7 00:00:41,880 --> 00:00:47,070 We know that each row is an array with a list of values, I want the array that holds Her Majesty's 8 00:00:47,070 --> 00:00:47,550 grades. 9 00:00:47,790 --> 00:00:53,070 So the first index needs to be to the first bracket indexes of the row that we're interested in. 10 00:00:53,490 --> 00:00:59,250 The second bracket indexes, the element in that row, as you can see, the first index is fixed that 11 00:00:59,250 --> 00:01:03,210 Rotu and the second index needs to start at zero and go up until three. 12 00:01:05,000 --> 00:01:08,930 So I can use a for loop that runs through every element inside row to. 13 00:01:11,180 --> 00:01:13,110 This for a loop started, J equals zero. 14 00:01:13,160 --> 00:01:18,420 The loop is going to keep running as long as J is smaller than the length of Rotu and during each run. 15 00:01:18,470 --> 00:01:23,960 Notice that the first bracket is fixed, that Rotu and the second bracket is going to vary with J. 16 00:01:41,760 --> 00:01:46,650 So remove these print statements and using loops, we can print the grades in row zero, one and two. 17 00:01:50,540 --> 00:01:53,090 So this far loop is going to start at Jakiel zero. 18 00:01:57,900 --> 00:02:02,250 The loop is going to keep running as long as Jay is smaller than the length of Rosero. 19 00:02:15,420 --> 00:02:19,050 And during each run, we're going to print every value inside a Rosero. 20 00:02:22,150 --> 00:02:27,510 So the first bracket is going to be fixed that Rosero and the second index is going to vary with Jay. 21 00:02:43,390 --> 00:02:47,950 We can apply the same logic to run in her mind these grades, we can make another for loop that Sartaj 22 00:02:47,950 --> 00:02:48,820 equals zero. 23 00:02:49,150 --> 00:02:53,200 The loop is going to keep running as long as J is smaller than the length of a one. 24 00:03:05,080 --> 00:03:08,980 And during each run, we're going to print every value inside a real one. 25 00:03:09,340 --> 00:03:13,840 So the first bracket is fix that row one and the second bracket is going to vary with Jay. 26 00:03:30,900 --> 00:03:35,580 And for the sake of expediency, I'm just going to copy the Sloup and change it so that the loop is 27 00:03:35,580 --> 00:03:37,080 running through the length of Rotu. 28 00:03:43,020 --> 00:03:46,230 And during each run, we're going to print every value inside wrote to. 29 00:03:50,380 --> 00:03:50,880 Sweet. 30 00:03:50,950 --> 00:03:52,540 I think it's time to run the code. 31 00:04:02,940 --> 00:04:08,310 And everything works out, it prints every value from the tiara, but instead of using print line, 32 00:04:08,310 --> 00:04:10,400 I'm just going to print everything in one line. 33 00:04:23,180 --> 00:04:28,070 OK, now it's really hard to tell the values apart from the lack of space, so after printing each row 34 00:04:28,070 --> 00:04:29,840 of data, I'm going to add a new line. 35 00:04:49,210 --> 00:04:54,160 Much better now, it's even more evident that each loop is running through an entire row and printing 36 00:04:54,160 --> 00:04:57,790 every grade in that row, but the output is still a bit cluttered. 37 00:04:57,800 --> 00:05:00,790 So I'm going to add a space after printing each grade. 38 00:05:14,090 --> 00:05:15,970 And we're making a lot of progress. 39 00:05:17,010 --> 00:05:22,230 Before we continue, I prepared an animation so that you can visualize how the code is working so far, 40 00:05:22,530 --> 00:05:23,970 this animation is a bit long. 41 00:05:23,970 --> 00:05:26,370 So if you feel comfortable what we talked about so far. 42 00:05:26,400 --> 00:05:27,420 Feel free to skip ahead. 43 00:06:31,460 --> 00:06:36,230 OK, I hope this will clarify any doubts you were having, but it seems like we need to write a Falu 44 00:06:36,230 --> 00:06:37,970 for each row and that can't be right. 45 00:06:38,330 --> 00:06:41,630 If there were 100 hundred rows, are you going to write 100 loops? 46 00:06:42,050 --> 00:06:42,770 Of course not. 47 00:06:43,250 --> 00:06:45,180 This is why we use nested loops. 48 00:06:45,200 --> 00:06:50,090 It's not a good idea to have a loop for every row because the loops are pretty much identical. 49 00:06:50,970 --> 00:06:55,950 So instead of creating one loop for every row, we need to use a nested loop or the outer loop runs 50 00:06:55,950 --> 00:07:01,530 through every row and the inner loop runs through every item in that role in a nested loop. 51 00:07:01,560 --> 00:07:07,470 I typically represents the RO index and J represents the element in the current row. 52 00:07:11,010 --> 00:07:15,660 This outer loop is going to keep running as long as the euro index eye is smaller than the length of 53 00:07:15,660 --> 00:07:20,970 our TD array, which is the number of rows, the outer loop starts with their own index of zero. 54 00:07:21,210 --> 00:07:27,420 And then as we hit the inner loop, the index eye is fixed at zero and the inner loop, Conergy is indexing 55 00:07:27,420 --> 00:07:28,650 every value in that row. 56 00:07:45,080 --> 00:07:49,220 After the inner loop runs to completion, the ROE index increases by one. 57 00:07:51,880 --> 00:07:57,000 And so once again, we hit the inner loop, the ROE index is fixed, that one and the inner loop coverage 58 00:07:57,010 --> 00:07:58,990 is indexing every value in that row. 59 00:08:23,050 --> 00:08:28,990 And finally, the counter increases to two, and as we hit the inner loop, the ROE index is fixed that 60 00:08:28,990 --> 00:08:33,039 two and the inner loop coverage is going to index every value in that row. 61 00:08:50,660 --> 00:08:55,280 And then the outer loop breaks when I is no longer smaller than the length of our tutera, when it can 62 00:08:55,280 --> 00:08:56,810 no longer index the rose. 63 00:09:02,300 --> 00:09:06,290 So now we can fix our code by printing the grades in every row using nested loops. 64 00:09:13,620 --> 00:09:16,140 The outer loop is going to start with the ROE index of zero. 65 00:09:23,190 --> 00:09:27,510 It's going to keep running until the ROE index traverses the length of our 2D array. 66 00:09:38,710 --> 00:09:44,050 And during each run, the outer loop is going to index a specific row, so I is going to start by index 67 00:09:44,050 --> 00:09:49,960 finger of zero zero one, row two, and once I equals three, this condition is going to turn false. 68 00:09:52,050 --> 00:09:56,490 And so the inner loop is going to start at the first element in that row, J equals zero. 69 00:10:01,100 --> 00:10:05,570 And it's going to keep running until Jay indexes every element in the current row. 70 00:10:11,800 --> 00:10:18,760 And J plus plus an inside the inner loop, we can print the contents of our two DRÉ, the first bracket 71 00:10:18,760 --> 00:10:23,860 is going to index each row and the second bracket is going to index every element in that row. 72 00:10:34,840 --> 00:10:36,710 OK, we're making a lot of progress. 73 00:10:36,850 --> 00:10:39,010 Once again, I'm going to print everything in one line. 74 00:10:48,350 --> 00:10:53,330 And now we can add a new line after running through each row, so after the inner loop runs to completion, 75 00:10:53,330 --> 00:10:55,620 we know that we've just printed an entire row. 76 00:10:55,790 --> 00:10:57,650 So here we can add a new line. 77 00:11:08,750 --> 00:11:09,730 That is perfect. 78 00:11:13,470 --> 00:11:17,190 And now every time we index a great value, we can add some space. 79 00:11:21,830 --> 00:11:25,880 And that's pretty good, I'm going to wrap up this code by printing the name next to each great value. 80 00:11:26,150 --> 00:11:30,470 We know that Harry's grades are in row zero, runs in row one, and her mind is in a row, too. 81 00:11:30,980 --> 00:11:32,570 So we can add a switch statement. 82 00:11:39,100 --> 00:11:42,310 And it's going to compare the Otterloo counter, the ROWAND next I. 83 00:11:45,500 --> 00:11:46,910 Against three cases. 84 00:11:51,100 --> 00:11:52,870 If it's the first run of the outer loop. 85 00:11:55,330 --> 00:12:00,130 We're going to print, not print line print, and then we're going to add a tab of space. 86 00:12:02,460 --> 00:12:05,640 Harry, and don't forget the breaker switch statements. 87 00:12:09,690 --> 00:12:12,390 If it's the second round of the outer loop, we're going to print run. 88 00:12:17,460 --> 00:12:20,640 And if it's the third run of the outer loop, we're going to print Terminix. 89 00:12:25,700 --> 00:12:27,740 And I think we're ready to run the code. 90 00:12:33,340 --> 00:12:34,300 And that is a. 91 00:12:36,820 --> 00:12:41,800 Based on the Rome index, let's assume it's the first run in I zero, the Swiss statement is going to 92 00:12:41,800 --> 00:12:47,440 print, Terry, the Swiss statement breaks and then the inner loop runs, says that I is fixed, that 93 00:12:47,440 --> 00:12:53,290 the first row is zero, and Jay is going to vary with the inner loop and runs through every element 94 00:12:53,290 --> 00:12:55,930 in zero, printing all of Harry's grades. 95 00:12:56,230 --> 00:13:01,270 And then this is going to repeat two more times for Ron and Hermione, OK? 96 00:13:05,260 --> 00:13:08,290 In this lesson, you ran through a tutelary with a nested for loop. 97 00:13:10,140 --> 00:13:16,170 The outer loop runs through every row, the inner loop runs through every item in that row, I represents 98 00:13:16,170 --> 00:13:19,670 the Rowand ducks and Jay represents the element in the current row. 99 00:13:20,250 --> 00:13:24,770 The outer loop starts with the Arrow index of zero and then the inner loop runs through every element. 100 00:13:24,810 --> 00:13:25,620 Zero zero. 101 00:13:32,700 --> 00:13:39,060 After the inner loop runs to completion, the outer loop increases the ROE index IBAI one, the second 102 00:13:39,060 --> 00:13:44,400 pass starts with Arrow index of one and once again, the inner loop runs through every element in that 103 00:13:44,400 --> 00:13:44,790 row. 104 00:13:53,300 --> 00:13:58,150 And then the third part starts with zero index of two and runs the inner loop again.