"Play It Again Sam"

The conditional statements we saw in Part 3 allowed us to determine at run-time whether or not to execute a block of code. What they don't do is allow us to execute a block more than once. Indeed, a computer would not be of much use if we could not repeat code. After all, if the computer can only solve only a part of the problem only once, we'd often be better off doing it ourselves. In this part, we will examine the features of C that support repetition.

The Pre-Test Loop

Just as we designed abstractions to express the decision structures earlier, here we will construct repetition structures. The first of these structures is the pre-test loop. This flowchart illustrate the pre-test loop structure.

[Pre-test loop flow chart]

The key feature of the pre-test loop is that we test to see whether or not to continue before executing the body of the loop. The effect of this is that we might not execute the body at all. So, if we're designing a program that has a section which might be executed 0 or more time (that is we're not guaranteed that it will be executed at all), a pre-test structure is what we want.

The Post-Test Loop

Just as we have a structure that tests the continuation condition at the top, we have on that tests it at the bottom. This is called the post-test loop and is illustrated in this flowchart:

[Post-test loop flow chart]

As you might expect, if we don't test to determine whether or not to continue until after we've executed the block, the loop body will always be executed at least once. This distinguishes it from the pre-test loop.

The Mid-Test Loop

There is a loop structure that generalizes both the pre-test and the post-test loop. Here we test in the middle of the loop as in this flowchart:

[Mid-test loop flow chart]

Notice that if we just leave Block 1 empty, then we have the same as a pre-test loop. Similarly, if we leave Block 2 empty, then we have a post-test loop.

Furthermore, the mid-test loop allows us to do things that we can't do with the other loops. Suppose we're reading some data from the user until the user types some signal to quit. We can solve this most naturally, by putting the read from the user into Block 1, and put the processing of that datum into Block 2. You should give some thought to how you would do this using only the pre-test or the post-test loops. You'll find that it's not nearly as clean a solution as you'll have to introduce new variables or repeat code.

Counted Loops

There's another looping construct that is supported in most languages, one that causes the loop to be repeated a certain number of times. We refer to such loops as counted or iterated loops. We can certainly construct other types of loops that will execute a certain number of times by setting a counter to 0 before the loop. Letting the loop test to see if we've gotten to the place to stop and then adding one to the counter each time through the loop. Because this is so common, though, many languages give us this as a convenience feature.

The next part begins the discussion of how we implement these loop structures in C.