Nested Loops

In the last chapter, you saw how control structures such as decisions and loops could be nested inside one another to produce sophisticated algorithms. One particularly useful, but somewhat tricky technique is the nesting of loops.

Let's take a look at an example program. How about one last version of our number averaging problem? Honest, I promise this is the last time I'll use this example.1 Suppose we modify the specification of our file averaging problem slightly. This time, instead of typing the numbers into the file one-per-line, we'll allow any number of values on a line. When multiple values appear on a line, they will be separated by commas.

At the top level, the basic algorithm will be some sort of file-processing loop that computes a running sum and count. For practice, let's use an end-of-file loop. Here is the code comprising the top-level loop.

line = infile.readline() while line != "":

# update sum and count for values in line line = infile.readline() print "\nThe average of the numbers is", sum / count

Now we need to figure out how to update the sum and count in the body of the loop. Since each individual line of the file contains one or more numbers separated by commas, we can split the line into substrings, each of which represents a number. Then we need to loop through these substrings, convert each to a number, and add it to sum. We also need to add 1 to count for each number. Here is a code fragment that processes a line:

for xStr in string.split(line,","): sum = sum + eval(xStr) count = count +l

Notice that the iteration of the for loop in this fragment is controlled by the value of line, which just happens to be the loop-control variable for the file-processing loop we outlined above.

Knitting these two loops together, here is our program.

# average7.py import string def main():

fileName = raw_input("What file are the numbers in? ") infile = open(fileName,'r') sum = 0.0 count = 0

line = infile.readline() while line != "":

1 until Chapter 11...

# update sum and count for values in line for xStr in string.split(line):

sum = sum + eval(xStr) count = count + 1 line = infile.readline() print "\nThe average of the numbers is", sum / count

As you can see, the loop that processes the numbers in a line is indented inside of the file processing loop. The outer while loop iterates once for each line of the file. On each iteration of the outer loop, the inner for loop iterates as many times as there are numbers on that line. When the inner loop finishes, the next line of the file is read, and the outer loop goes through its next iteration.

The individual fragments of this problem are not complex when taken separately, but the final result is fairly intricate. The best way to design nested loops is to follow the process we did here. First design the outer loop without worrying about what goes inside. Then design what goes inside, ignoring the outer loop(s). Finally, put the pieces together, taking care to preserve the nesting. If the individual loops are correct, the nested result will work just fine; trust it. With a little practice, you'll be implementing double-, even triple-nested loops with ease.

Was this article helpful?

0 0

Post a comment