An Introduction To Structured Programming
An Introduction To Structured Programming
An Introduction To Structured Programming
net/publication/225724365
CITATION READS
1 39,623
1 author:
SEE PROFILE
All content following this page was uploaded by Karl Phillip Hunt on 11 February 2015.
Structured programming (SP) is a technique devised to improve the reliability and clarity
of programs. In SP, control of program flow is restricted to three structures, sequence,
IF THEN ELSE, and DO WHILE, or to a structure derivable from a combination of the
basic three. Thus, a structured program does not need to use GO TOs or branches (unless
it is written in a language that does not have statement forms corresponding to the SP
structures, in which case, GO TOs may be used to simulate the structures). The result is
a program built of modules that are highly independent of each other. In turn, this allows
a programmer to be more confident that the code contains fewer logic errors and will be
easier to debug and change in the future. However, SP may be less efficient than an
unstructured counterpart. Another disadvantage is the relative difficulty of using SP with a
language that doesn't support it, although this situation is changing as languages are up-
dated (e.g., FORTRAN 77).
Copyright 1979 Psychonomic Society, Inc. 229 0005- 7878/79/020229-05 $00. is/a
230 HUNT
detennine if feedback is to be given; the instructions for the experiment. The DO WHILE
input number of pairs;
DO UNTIL all pairs are input; below it is responsible for repeating the study and
input stimul; and responses to file;
END 00; recall phases as long as the subject has not reached
DO WHILE subjects remain to be run;
present instructions; criterion, which in the present case is 100% correct
DO WHILE present subject is still below criterion;
generate random permutation of pairs and of stimuli;
on one trial. The extent of this DO WHILE's control
DO WHILE there are still pairs to be presented on this trial;
present next pair;
is defined by the END DO which is the third statement
END DO; from the bottom. Again, note that the END DO is
pause for delay interval j
reset cri terion-nat-reached switch; indented the same amount as the DO WHILE.
DO WHILE pairs renain to be tested;
present stimulus; Next, the stimulus-response pairs and a separate
input response;
IF response ; 5 wrong; set of stimuli are arranged in a new random order for
turn on criterion-nat-reached switch;
IF feedback should be given;
the current trial's study and test phases, respectively.
print "wrong"; The next two DO WHILEs control the study phase,
END IF;
ELSE; during which the pairs are shown individually, and the
IF feedback should be given;
print "good"; recall phase, in which only the stimuli are shown and
END IF;
END IF; the subject's responses are input. Each of these DO
END DO;
pause for intertrial interval;
WHILEs is followed by pause statements that insert
END DO;
dismiss subject;
intervals between study and test phases and trials.
END 00; Within the range of the DO WHILE for the test
Figure 1. Program for an on-line paired associate experiment, phase is the third kind of control structure, IF THEN
written in pseudocode. ELSE. The first of three of these begins after the
statement for response input and ends its control at
is drawn first, since it approximates the actual code the END IF directly below it, several statements down.
itself. It is fairly easy to translate a pseudocoded At the beginning, it tests the response to determine
program into final code, especially when the language if it is correct. If this condition is true (i.e., the response
used contains statements that correspond to the control is wrong), the next set of statements down to the
structures of SP. Unfortunately, not all languages do ELSE will be performed, and the set after the ELSE
this. will be bypassed. On the other hand, if the response is
Looking at the example, one can see that the first correct, only the statements under the ELSE will be
two statements are housekeeping chores in which the performed. Thus, the IF THEN ELSE structure allows
experimenter sets up the experiment. These illustrate selection of one of two alternatives depending on the
control by sequence. Here, the sequence is trivial since truth of some condition. More alternatives can be
it could have been reversed with no adverse effect; introduced by nesting additional IFs within the first.
elsewhere, of course, it will often be more critical, This has been done here to allow feedback to be given
especially in the overall sequencing of the major if the experimenter has requested it. Within each
elements of the program. alternative of the first IF is another that causes either
The next statement is a DO UNTIL, a variation on "wrong" or "good" to be displayed to the subject if
DO WHILE. Its effect is to repeat the statement that it has been indicated at the beginning of the program
follows it until the condition is met and then to allow that feedback should be given. Neither IF has an
control to proceed to the statement following the accompanying ELSE since there is nothing to be done
END DO. Specifically, the program will ask the if no feedback is desired. In this case, the ELSE clause
experimenter to continue entering the stimuli and is null, which describes the kind of IF statement one
responses for the experiment until all n pairs are input. finds in FORTRAN, for example.
The input statement itself is indented to show that it The rest of the example consists of END statements
is under the control of the DO UNTIL. for control structures, the intertrial interval pause
The rest of the program is a more elaborate demon- already mentioned, and a statement for subject
stration of the use of indentation. The statement "DO dismissal. The next two sections of this paper examine
WHILE subjects remain to be run" exerts control over the hierarchical nature of a structured program and the
all statements that follow it down to the END DO at control structures in more detail.
the bottom that lines up at the margin with the DO
WHILE. These statements control the experiment. THE HIERARCHY OF MODULES
There are several levels of indentation here, each
representing the extent of control of some control The hierarchy in a structured program should be
structure. Indenting is not a necessary part of SP, but apparent from the way that the control structures in
it is almost universally used to make the structure of the example are completely nested. Another device
a program clearer, not just in pseudocode or formal often used with SP that shows the hierarchy is a
algorithms but in program code also, when possible. top-down chart, pictured in Figure 2 (Hughes &
Following the "DO WHILE subjects remain ..." is Michton, 1977; Miller & Lindamood, 1973).
a statement that provides for giving the next subject The boxes in the chart represent modules in the
STRUCTURED PROGRAMMING 231
8
(8)
The lines of control in a structured program, then,
are restricted to the hierarchy. One does not get
the common pattern of many programs where any
module might be connected to any other if it seems
convenient, a pattern that has often been called "a bowl
of spaghetti" or "a rat's nest." This is primarily what
is meant by saying that the modules are minimally
dependent on each other in SP. In a nonstructured
program, it is often difficult to tell when a particular
section of code will be performed because there are
several different paths to it, occurring under a different
set of conditions. While there may be a number of
conditions that cause a module to be performed in a
structured program, the path to that module will be
easier to trace because of the hierarchy.
The use of the term module in the literature is
often vague and contradictory. In some cases, it means
an entire program; in others, it may be restricted to a
certain number of lines of code (e.g., a maximum of
50 or so that fit onto a single page of the program
listing). Still other meanings are associated with it, Figure 3. Control structures used in structured programming:
such as the assignment given to a programmer working (A) sequencing, (B) IF THEN ELSE, (C) DO WHILE, (D) DO
as a part of a system-development team. UNTIL, (E) CASE.
232 HUNT
the flowcharts is the fact that decisions are made and ADVANTAGES AND DISADVANTAGES OF SP
branches are taken in a structured program, even though
there may be no statements that explicitly say GO TO The ability to check each module independently is
or something equivalent. Thus, branches are not an obvious advantage of SP. In general, the clarity and
eliminated in a structured program but are restricted to systematic nature of the lines of control and the
those that implement the control structures. independence of the modules are responsible for
The only structure in Figure 3 that has not been the superiority of SP over a more unorganized approach.
covered is the CASE structure. This is essentially an It is much easier to tell when a module is being
expansion of the IF THEN ELSE for the situation in performed in a structured program, as there is only one
which there are more than two possible courses of way of getting into it and only one way of getting out,
action; there are several different cases to be taken and both the entry and exit connect to the same higher
care of. The CASE structure, as well as the DO WHILE level module. Thus, the logic is more easily followed
and other proposed additional structures, can be both within and between modules.
simulated with the proper combination of the three The result is code that is much more likely to be free
basic structures. The advantage of such additional of logic bugs (i.e., more reliable code). In addition,
structures is that one can avoid some fairly clumsy once the programmer has learned to use SP, programs
combinations of code. For example, one has to either are written more quickly and with less pain. It is much
nest a lot of IF THEN ELSEs or use a lot of switches less difficult, on the average, to rid the program of bugs
to handle a large number of cases without using CASE after it is written. Changes to the program at a later
or going through a long, inefficient series of separate date can be made more easily because fewer modules
IFs. However, it is also possible to build a new structure will be affected; often new modules, if necessary, can
that is so complex that it defeats the purposes of SP. be directly inserted into the program with very little
But are there programs that cannot be written with modification of existing code. This process is further
these structures? That is, are there programs that require facilitated by the ease of following the logic. Anyone
some form of stand-alone GO TO? The answer is no. who has programmed knows that a program that has
Bohm and Jacopini (1966) and Mills (Note 2) have been ignored for a while can appear to be almost
proven the theorem that any proper program can be unintelligible when first reexamined.
written with just the three basic structures. Since a But SP also has its disadvantages. Two are its relative
proper program has one entry and one exit and at least inefficiency in use of memory and speed of execution.
one path between them, and any functioning program If Module A is to be performed immediately after
can be written as a proper program, the theorem applies Module B under certain circumstances, a structured
to all programs worth considering. program may route the control path through one or
The major objective of the structure theorem, as it more higher level modules rather than directly from
is called, is based on the hierarchical structure that A to B. Also, SP may, upon occasion, force a test to
results from using SP. Because the modules are highly be made more often than might be the case in an
independent of each other, it is possible to prove the unstructured program. The extra instructions, then,
correctness of a program (establish that it performs take more memory and more time. The latter factor
the appropriate function) by proving the correctness may be a major reason for not employing a strict SP
of each separate module. The alternative is to prove approach in time-critical applications. However, one
the correctness of the program as a whole. Those who should probably avoid SP in only those program sections
remember trying to prove theorems in math or elsewhere that are time critical. A case could be made that
can appreciate the enormity of this task, which is attempts to make the entire program more efficient
tantamount to proving a large set of complexly related with direct GO TOs could make the program more
theorems. inefficient overall, even though there might be improve-
Proving program correctness is still a tedious and ments in relatively small segments of code.
difficult exercise with a structured program. However, While the potential inefficiency of SP is a major
there is a parallel between going through a correctness reason that opposition to it exists, there are other
proof and the design and desk-checking of a program. disadvantages that are greater, at least at present. One
While the programmer may not attempt a rigorous is the apparent lack of flexibility in SP. While any
proof, she or he can examine each module separately program can be written using SP, most programmers
and be fairly well satisfied that, if each module seems find this hard to believe and resist having the freedom
to work properly, the program should also (Mills, to branch anywhere in a program taken away from
Note 2). Parenthetically, it should be emphasized that them. A related factor is the emphasis in SP on the
for this process, as well as for correctness proofs, one human element in programming rather than the logical.
should be careful to specify the states of all important Most programmers, probably including psychologists
variables as the module is entered. who program, seem to think of themselves as strictly
STRUCTURED PROGRAMMING 233
REFERENCES
How does one learn to use SP? First, an individual
should read as much about SP and related techniques BAKER, F. T. Chief programmer team management of production
as possible. An especially good starting point is the programming. IBM Systems Journal, 1972, 9, 366-371.
book by Yourdon (1975). Second, if it is possible, BAKER, F. T., & MILLS, H. D. Chief programmer teams. Datama-
tion, December 1973, pp. 55-57.
one should work through a good text on writing BOHM, C.. & JACOPINI, G. Flow diagrams, Turning machines,
structured programs in a specific language, even if the and languages with only two formation rules. Communications
language is not likely to be used in the near future. o(theACM, 1966,9,366-371.
Changing from the usual nonstructured programming CO~WAY, R., & GRIES, D. An introduction to programming: A
structured approach. Cambridge, Mass: Winthrop, 1973.
style to SP is almost like learning to program all over DAHL, O. J., DUKSTRA, E. W .. & HOARE, C. A. R. Structured
again. A text on a language that incorporates the SP programming. New York: Academic Press, 1972.
approach, such as PASCAL, is a good choice. DIJKSTRA, E. W. Programming considered as a human activity.
The problem of "unstructured" languages can be Proceedings of IFlP Congress 65. Washington, D. C: Spartan
overcome by simulating the structures, as already Books, 1965.
DUKSTRA, E. W. GOTO statement considered harmful. Communi-
noted, or by using a preprocessor or maverick compiler, cations of the ACM. 1968, 11, 147-148; 538; 541. "*
if either is available, that includes statement forms for DIJKSTRA, E. W. Structured programming. In P. Naur & B.
the structures. (A preprocessor runs before the compiler/ Randell (Eds.), Software engineering techniques. Brussels:
assembler and translates the special statements into NATO Scientific Affairs Division, 1969.
HUGHES, 1. K.. & MICHTON, 1. I. A structured approach to pro-
simulating code; IBM supplies one to allow SP in
gramming. Englewood Cliffs. N. J: Prentice-Hall. 1977.
FORTRAN.) Since virtually all assembly languages MCCRACKEN, D. D. Revolution in programming. Datamation,
have a macro capability, it should be possible to write December, 1973. Pp. SO-52.
macros to handle the structures. This should be done, McGOWAN. C. L.. III, & KELLY, J. R. Top-down structured pro-
however, by someone who is experienced in the language gramming techniques. New York: Petrocelli-Charter, 1975.
MILLER. E. F., & LINDAMOOD. G. E. Structured programming:
and in address determination schemes, since a method Top-down approach. Datamation, December 1973. pp. 55-57.
will have to be found for determining the exit and, in YOURDON, E. Techniques ofprogram structure and design. Engle-
some cases, entry addresses. wood Cliffs, N.J: Prentice-Hall, 1975.