0:01

The bodies of loops can contain any statement including other loops.

Â We'll explore that in this lecture. Here we have a loop inside another loop.

Â The outer loop has I range over the values ten, eleven, and twelve, And for each of

Â those iterations j is going to range through one, two, three, and four.

Â When I visualize this I starts off referring to the value of ten.

Â J doesn't exist yet because we have just reached the four loop the first time.

Â This inner four loop. J is going to take on the value one in the

Â first iteration. I still refers to ten, And we're about to

Â print IJ, so we should see ten and one up here.

Â Notice that as soon as we printed tenant one, we went back to line two, not to line

Â one. And the reason for that is we are going to

Â completely execute this inner four loop until J has gone through the numbers 1,2,3

Â and four. So that means that J will take on the

Â value two next. And we're going to print tenant two.

Â Now J is going to take on the value three, J now refers to three, and we'll print

Â tenant three and four. Now we are done with this inner four loop, and so we are

Â going to jump back out to line one because we have completed execution of the body of

Â the outer four loop. Now, I is going to take on the value

Â eleven. J is going to take on the values one, two,

Â three, and four again. And now we're done with this inner loop,

Â so we'll go back out to the outer one. Now, we will do the last iteration of the

Â outer loop. Alright, it starts off as twelve and J is

Â going to go through the numbers one two three and four again.

Â And I will not change again. Now we are done with this inner loop.

Â We go back and check the outer loop once more.

Â And we are done with the program. Here is another example.

Â We will keep track of a list of list of numbers.

Â 2:25

These are going to represent grades so for perhaps a student will have received a 70

Â a 75 and an 80 in one class. And in another class that their are taking

Â they have a 70 an 80 a 90 and a 100. And in the third class they have an 80 and

Â a 100. And, they want to know what the average is

Â for each of these list of grades. We are going to write a function to

Â calculate this list of averages. But before we do that we're going to start

Â with a similar problem. Just find the average of a list of

Â numbers. So.

Â 3:30

There's our accumulator, and then for each mark in English, what we're going to do is

Â we're going to add the mark to the total. Now,

Â The total is 225. We need to divide that by the length of

Â the list. Now that we know how to calculate the

Â average of a list of numbers, we would like to do this for each of the lists in

Â our grades list, and we would like to assemble the result into a list of these

Â averages. As we're developing this function, we need

Â examples. The first example we're going to do is

Â this list of grades that we have started with over on the left pane.

Â The average of 70, 75 and 80 is 75. The average of 70, 80, 90 and 100 is 85.

Â The average of 80 and 100 is 90. All of these are floats.

Â Let's make this window just a little bit wider.

Â 4:44

The pipe contract is next. What we have is a list of list of numbers.

Â Now, these are all integers. But we can certainly imagine giving

Â floating point results for scores on a piece of work.

Â So, The pipe contract is going to be a list of

Â lists of number. And that is the, the type of the

Â parameter. And we are going to return a list of

Â float. We're fine with averages as the name, so

Â we leave it as such, and we're going to take in maybe grades as the name of our

Â parameter. .

Â In our description, this is going to return a new list in which each item is

Â the average of the grades in the inner list at the corresponding position.

Â . Of grades. Well, we know we're building a

Â new list. And we're going to be doing this nested

Â list by nested list. So we're going to be accumulating this

Â result. So we'll have maybe averages as the name

Â of our variable, which is going to be the list of averages at the end.

Â And when we're done, we know we want to return averages.

Â 6:31

For each of the sub lists in the grades list, what we want to do is calculate the

Â average of grades list and append it to averages.

Â Well, we have code that does that over on the left.

Â We know that we want start total off at zero.

Â And then for each mark in this particular sub grade list,

Â We would like to add it to total. And then once we are done with that,

Â We will summon up the values on the grades list.

Â We would like to append to averages the average.

Â And we're done. We will now call this in the visualizer

Â and watch it execute. We have added two statements at the bottom

Â of this file. The first one creates the, the nested

Â list, And the second one calls the averages

Â function passing in that list. Let's visualize this.

Â 7:55

We first define our function. Next we are going to create about ten or

Â so objects. Grades refers to a list with three items

Â in it. The first one is a list with three items

Â in it, 70, 75 and 80. The second one refer this to a list that

Â has 70, 80, 90 and 100 in it. And the third item in index two in this

Â outer list refers to a list with two items in it, 80 and 90.

Â 8:30

This first nested list contains 70, 75, and 80.

Â The second nested list contains 70,80,90. Where is it?

Â 90 and 100. This third nested list contains 80, and

Â 100. So indeed, the list structure that is

Â built in computer memory is a representation of what we have done on

Â this line. Now we're going to pass that list into the

Â averages function. We'll get a stack frame for function

Â averages containing parameter grades referring to that same list.

Â 9:23

We will start, here's a fun one, we will start a new variable averages that is an

Â empty list. Notice we also have a function averages

Â outside. They're obviously two very different

Â things. Next we're going to go through this lists

Â inside this outer list. So grades list is initially going to refer

Â to the list that index at memory address x3.

Â 9:53

Then we're going to go through 70, 75, and 80, add them up and divide by the length

Â of that list. We'll start total off at zero and for

Â every mark in the grades list, well mark is going to start off at 70 and the 75 and

Â then 80 each iteration here. We going to take that 70 here on line

Â seventeen and we're going to add it to the total which is zero.

Â So we'll get 70 of course 70 plus zero is zero.

Â We will assign that to total. On this next iteration, we're going to

Â have the marquee as 75. Now we're going to take 75 and add it to

Â 70. 70 plus 75 is 145.

Â And we will assign 145 to total. On the next iteration we're going to

Â assign the 80 to mark. That's what this four loop does.

Â Grabs the last item in the list, mark now refers to 80.

Â On line seventeen we have an assignment statement. The right hand side evaluates

Â to 145 plus 80 or 225. We'll assign 225 to total.

Â We have now gone through all the items in that first nested list.

Â We've done the 70, the 75, and the 80, and so this for loop will now terminate.

Â 11:38

Now averages refers to a list containing 75.

Â 225 divided by three is 75. We have completed execution of the first

Â iteration of the outer four loop. We will now, on this next iteration, get

Â the second list, with four items in it. Grades list now refers to that second

Â list, with 70, 80, 90 and 100 in it. We will initialize total to zero,

Â Reset it, and then we're going to go through the four numbers,

Â Two, three, four and we're done. And now total is 340. The sum of 70 plus

Â 80 plus 90 plus 100. We will append 340 divided by four to our

Â averages list. And that happens to be 85.

Â And now for the third nested list. Grades list.

Â The first of a list of two items in it, that's 80 and 100.

Â We'll reset total to zero. There's the 80 that we're dealing with.

Â Mark it now 80. We'll add 80 to zero, giving us 80, of

Â course. We will then get the 100.

Â And we'll add the two together. And then we will append 180 divided by the

Â length of that nested list, or two. So 180 divided by two is 90.

Â 13:23

We have now gone through all three of our nested lists.

Â So this outer loop will terminate. And we're done.

Â We will now return, this averages list which as a reminder now contains 75, 85

Â and 90. We have to return that value.

Â We don't do anything with it in the main program, so when I click forward, the

Â program will just end.

Â