Any program can be written with sequencing, alternatives, and pre-tested loops. However, this statement does not merely refer to stringing these control structures along, one after the other. Each of these control structures can be embedded in the others. This embedding, or composition, of control structures is also necessary.
Composing control structures is wrapping one around another. Each of the three constructs we have seen so far has a single entry point, and a single exit point. Each of the three constructs also has component processing pieces — each with a single entry point, and a single exit point. Therefore, each of these constructs can be used as a component of a larger construct. We have already seen alternatives nested inside alternatives, which yielded an overall structure like this:
We might have a loop as one of the branches of an alternative, as follows:
We might have sequences and an alternative in the body of a loop, as follows:
In each of these cases, the single entry/single exit criterion allows the construct to be thought of as a single conceptual unit. In terms of overall function, a single processing box can replace any single entry/single exit construct. This permits thinking of the flowchart, and so the program, at a variety of levels. This is illustrated in the series of flowcharts below: each circled construct is condensed into a single shaded processing block:
This also means we can reverse the process, and decompose an overall process into constituent pieces. Any single processing block can be further refined into a single entry/single exit construct. This is illustrated in the series of flowcharts below: each single shaded processing block is elaborated into a circled single entry/single exit construct:
This process of repeated decomposition, or successive refinement, is the fundamental approach in much of programming. After all, usually we have a problem, and the goal is to refine the single box, “solve the problem,” into a series of series of constructs that we can program.
Copyright © 2002 Brian Hetrick
Page last updated 13 January 2002.
Building Blocks I
Control Flow I
Control Flow II
A First Program
Data Structures I
Building Blocks II
Data Structures II