When a loop is nested inside another loop the inner loop goes through all its iterations?
When we introduced for loops, we said that the loop body consists of one of more statements. We saw in 4.5 For Loop Variations that we could put if statements inside loop bodies. In this section, we’ll see that a for loop body can itself contain another for loop, since for loops are themselves statements. We’ll study uses of these nested for loops, and also draw comparisons between them and comprehensions from the previous chapter. Show
Nested loops and nested dataNested loops are particularly useful when dealing with nested data. As a first example, suppose we have a list of lists of integers:
Our goal is to compute the sum of all of the elements of this list:
We can start with our basic loop accumulator pattern:
The difference between this function and in
So here is one way of completing this function, by using the builtin
This implementation is structurally
similar to the
We say that the
Whew, that’s a lot of writing! We can summarize the above behaviour by creating a loop accumulation table. Note that the table below has the same structure as the ones we’ve seen before, but is more complex because its columns
include both the outer and inner loop variables and iterations. The
The Cartesian productOur next example illustrates how to use nested loops on two different collections, obtaining all pairs of possible values from each collection. If that sounds familiar, well, it should be!
Before we get to writing any loops at all, let’s remind ourselves how we would write a comprehension to compute the Cartesian product:
Now we’ll see how to write this using nested for loop:
As we saw in our first example, here the inner loop
Another way of visualizing the return value is:
Outer and inner accumulatorsBoth the As an example, suppose we have a list of lists of integers called
Notice how the list of grades for course ENG196 does not have the same length as CSC110 or MAT137. Our goal is to return a new list containing the average grade of each course. We saw in Section 4.5 how to use loops to calculate the average of a collection of numbers:
We can calculate a list of averages for each course using a comprehension: Exercise: write a precondition expression to guarantee there are no empty lists in
We can translate this into a for loop using a list accumulator variable and list concatenation for the update:
Now let’s see how to calculate the
It may be surprising to you that we can do this! Just as how in the last chapter we saw that we can take a predicate and expand it into its definition, we can do the same thing for Python functions with multiple statements in their
body. The only change we needed to make was the return statement of
One important note about the structure of this nested loop is that the inner loop accumulators are assigned to inside the body of the outer loop*, rather than at the top of the function body. This is because the accumulators Let’s take a look at our final loop accumulation table in this section, which illustrates the execution of
Summary: understanding and simplifying nested for loopsNested for loops are a powerful tool in our understanding of the Python programming language, but they are by far the most complex and most error-prone that we’ve studied so far. Just as we saw with nested expressions and nested if statements, nested loops have the potential to greatly increase the size and complexity of our code. Contrast
the implementation of While nested loops are sometimes inevitable or convenient, we recommend following these guidelines to simplify your use of nested loops to help you better understand your code:
References
When a loop is nested inside another loop the inner loop goes through?3. Summary. Nested iteration statements are iteration statements that appear in the body of another iteration statement. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue.
Can a loop be nested inside another loop?Since the code block of a loop can include any legal C++ statements, you can place a loop inside of a loop. The inner loop is nested inside the outer loop. Nested loops are useful when for each pass through the outer loop, you need to repeat some action on the data in the outer loop.
What is it called when you have a loop inside another loop?A loop within another loop is called nested loop.
How many times will a nested loop iterate?The outer loop can contain more than one inner loop. There is no limitation on the chaining of loops. In the nested loop, the number of iterations will be equal to the number of iterations in the outer loop multiplied by the iterations in the inner loop.
|