As I mentioned before, arrays and loops are like siblings.
If you want to run through the elements of a normal array, you can use a simple for loop.
But what about a tutee array in this case, you need a nested for loop?
Oh, yes, we finally get to use nested loops.
In this lesson, you're going to use a nested loop to run through a tutera.
I want to start by looping through a single arrow in a tutera.
We know that each row is an array with a list of values, I want the array that holds Her Majesty's
grades.
So the first index needs to be to the first bracket indexes of the row that we're interested in.
The second bracket indexes, the element in that row, as you can see, the first index is fixed that
Rotu and the second index needs to start at zero and go up until three.
So I can use a for loop that runs through every element inside row to.
This for a loop started, J equals zero.
The loop is going to keep running as long as J is smaller than the length of Rotu and during each run.
Notice that the first bracket is fixed, that Rotu and the second bracket is going to vary with J.
So remove these print statements and using loops, we can print the grades in row zero, one and two.
So this far loop is going to start at Jakiel zero.
The loop is going to keep running as long as Jay is smaller than the length of Rosero.
And during each run, we're going to print every value inside a Rosero.
So the first bracket is going to be fixed that Rosero and the second index is going to vary with Jay.
We can apply the same logic to run in her mind these grades, we can make another for loop that Sartaj
equals zero.
The loop is going to keep running as long as J is smaller than the length of a one.
And during each run, we're going to print every value inside a real one.
So the first bracket is fix that row one and the second bracket is going to vary with Jay.
And for the sake of expediency, I'm just going to copy the Sloup and change it so that the loop is
running through the length of Rotu.
And during each run, we're going to print every value inside wrote to.
Sweet.
I think it's time to run the code.
And everything works out, it prints every value from the tiara, but instead of using print line,
I'm just going to print everything in one line.
OK, now it's really hard to tell the values apart from the lack of space, so after printing each row
of data, I'm going to add a new line.
Much better now, it's even more evident that each loop is running through an entire row and printing
every grade in that row, but the output is still a bit cluttered.
So I'm going to add a space after printing each grade.
And we're making a lot of progress.
Before we continue, I prepared an animation so that you can visualize how the code is working so far,
this animation is a bit long.
So if you feel comfortable what we talked about so far.
Feel free to skip ahead.
OK, I hope this will clarify any doubts you were having, but it seems like we need to write a Falu
for each row and that can't be right.
If there were 100 hundred rows, are you going to write 100 loops?
Of course not.
This is why we use nested loops.
It's not a good idea to have a loop for every row because the loops are pretty much identical.
So instead of creating one loop for every row, we need to use a nested loop or the outer loop runs
through every row and the inner loop runs through every item in that role in a nested loop.
I typically represents the RO index and J represents the element in the current row.
This outer loop is going to keep running as long as the euro index eye is smaller than the length of
our TD array, which is the number of rows, the outer loop starts with their own index of zero.
And then as we hit the inner loop, the index eye is fixed at zero and the inner loop, Conergy is indexing
every value in that row.
After the inner loop runs to completion, the ROE index increases by one.
And so once again, we hit the inner loop, the ROE index is fixed, that one and the inner loop coverage
is indexing every value in that row.
And finally, the counter increases to two, and as we hit the inner loop, the ROE index is fixed that
two and the inner loop coverage is going to index every value in that row.
And then the outer loop breaks when I is no longer smaller than the length of our tutera, when it can
no longer index the rose.
So now we can fix our code by printing the grades in every row using nested loops.
The outer loop is going to start with the ROE index of zero.
It's going to keep running until the ROE index traverses the length of our 2D array.
And during each run, the outer loop is going to index a specific row, so I is going to start by index
finger of zero zero one, row two, and once I equals three, this condition is going to turn false.
And so the inner loop is going to start at the first element in that row, J equals zero.
And it's going to keep running until Jay indexes every element in the current row.
And J plus plus an inside the inner loop, we can print the contents of our two DRÉ, the first bracket
is going to index each row and the second bracket is going to index every element in that row.
OK, we're making a lot of progress.
Once again, I'm going to print everything in one line.
And now we can add a new line after running through each row, so after the inner loop runs to completion,
we know that we've just printed an entire row.
So here we can add a new line.
That is perfect.
And now every time we index a great value, we can add some space.
And that's pretty good, I'm going to wrap up this code by printing the name next to each great value.
We know that Harry's grades are in row zero, runs in row one, and her mind is in a row, too.
So we can add a switch statement.
And it's going to compare the Otterloo counter, the ROWAND next I.
Against three cases.
If it's the first run of the outer loop.
We're going to print, not print line print, and then we're going to add a tab of space.
Harry, and don't forget the breaker switch statements.
If it's the second round of the outer loop, we're going to print run.
And if it's the third run of the outer loop, we're going to print Terminix.
And I think we're ready to run the code.
And that is a.
Based on the Rome index, let's assume it's the first run in I zero, the Swiss statement is going to
print, Terry, the Swiss statement breaks and then the inner loop runs, says that I is fixed, that
the first row is zero, and Jay is going to vary with the inner loop and runs through every element
in zero, printing all of Harry's grades.
And then this is going to repeat two more times for Ron and Hermione, OK?
In this lesson, you ran through a tutelary with a nested for loop.
The outer loop runs through every row, the inner loop runs through every item in that row, I represents
the Rowand ducks and Jay represents the element in the current row.
The outer loop starts with the Arrow index of zero and then the inner loop runs through every element.
Zero zero.
After the inner loop runs to completion, the outer loop increases the ROE index IBAI one, the second
pass starts with Arrow index of one and once again, the inner loop runs through every element in that
row.
And then the third part starts with zero index of two and runs the inner loop again.