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.