An Introduction To Structured Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/225724365

An introduction to structured programming

Article  in  Behavior Research Methods · March 1979


DOI: 10.3758/BF03205654

CITATION READS

1 39,623

1 author:

Karl Phillip Hunt


Dalton State College
11 PUBLICATIONS   99 CITATIONS   

SEE PROFILE

All content following this page was uploaded by Karl Phillip Hunt on 11 February 2015.

The user has requested enhancement of the downloaded file.


Behavior Research Methods & Instrumentation
1979, Vol. 11 (2), 229-233

An introduction to structured programming


KARL P. HUNT
AmericanNationalBank, Chattanooga, Tennessee 37350

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).

During the last 15 years, many people working in AN EXAMPLE OF SP


the area of computing have expressed concern with the
problem of software reliability. While the cost of While some may disagree on an exact definition of
hardware has decreased as power has increased, software SP (McCracken, 1973), there is consensus on its two
costs have increased and become more complex. The primary features: (1) a structured program is a hier-
result is that, although we can do more than before, it archical arrangement of highly independent modules,
is at the risk of encountering more serious problems and (2) flow within the program is controlled exclusively
than before, and the problems usually prove to be more by only three forms of control, selection, repetition,
resistant to solution. In response to this situation, and sequencing. The basic control structures proposed
several approaches to programming have been devised to implement these are IF THEN ELSE for selection,
to make programs more reliable and, at the same time, DO WHILE for repetition, and simply placing statements
boost the amount of relatively bug-free code a one after the other for sequencing. There are other
programmer can produce. The most popular approach possible structures, but only two are discussed here:
has been structured programming (SP). DO UNTIL (another way of controlling repetition)
Much of the development of SP can be traced to and the case structure, which allows for selection from
the work of Dijkstra (Dahl, Dijkstra, & Hoare, 1972; a large number of alternatives. This list does not include
Dijkstra, 1965, 1968, 1969). Dijkstra's objective was the GO TO (or jump or branch, whichever term suits
to define a class of programs for which correctness the language you are familiar with).
proofs can be relatively easily provided. The class is To help illustrate these characteristics, an example
that of structured programs. of an on-line program for control of a paired associate
The European efforts of Dijkstra and others were recall experiment is given in Figure 1. The number of
primarily academic. In the U.S., Mills and his colleagues stimulus-response pairs is variable, and feedback
successfully used SP in applied projects for IBM (Baker, ("wrong" or "good") mayor may not be given at
1972; Baker & Mills, 1973). In fact, Mills (Note 1) is recall. Other parameters can be introduced, such as
one of the most enthusiastic supporters of SP used in length of presentation, but are treated as constant to
conjunction with other design methods. keep the example simple.
The popularity of SP has increased such that a The example as shown in Figure 1 is written in
number of texts on it have appeared, some devoted pseudocode. Pseudocode, which is often suggested as
to teaching a specific language via a SP approach an alternative to flowcharts, is a natural-language version
(Conway & Gries, 1973; Hughes & Michton, 1977; of the code in which the program will actually be
McGowan & Kelly, 1975). Clearly, SP is believed by written. Just as in a flowchart, its primary purpose is
many to be an important development in programming. to represent the logic that controls the flow of the
program. Thus the control structures are emphasized
by being written in all uppercase letters and by the
Requests for reprints should be sent to Karl Hunt, use of indentation. Pseudocode is a good intermediate
1413 Cinderella Road, Lookout Mountain, Tennessee 37350. step to writing a structured program even if a flowchart

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

One feature is common to all of these usages:


PAIRED -
ASSOCIATE A module performs a function. If a person can describe
EXPERIMENT what a part of a program (or a whole program, or even
1 a set of programs) does in one or two sentences, then
I 1 he or she is probably talking about a module. The
function can be very general or very specific; the same
RUN
HOUSEKEEPING EXPERIMENT is true of a module. The modules in Figures 1 and 2
are fairly specific, more so than they usually are in
I a top-down chart. Although it is not usually done,
I 1 I there is no conceptual reason for not calling a single
DISMISS
statement a module, with the exception of control
PRESENT RUN
INSTRUCTIONS CURRENT SUBJECT structures that may be comprised of several statements.
SUBJECT
There is a second feature of a module as described
1 here that, while not restricted to SP, is an important
I -I part of the SP approach. A module may have only one
STUDY TEST
entry point and one exit. This, of course, fits in with
PHASE PHASE the hierarchy already described. An examination of
the hierarchy shows that each module has a single line
of control, which represents both the entry and exit
Figure 2. Top-down chart for the paired associate program
in Figure 1. points. Figures I and 2 indicate, for example, that
the module for running a particular subject starts only
program. Each module controls those immediately at the second DO WHILE, which can be arrived at only
below it to which it is connected. Thus, the module after the instructions are presented. Similarly, there is
"run experiment" has direct control over "instructions," just one way of getting out of the module: When the
"run current subject," and "dismiss subject." Similarly, subject reaches criterion, control passes back to the
"run current subject" controls "present study phase" module represented by the first DO WHILE through
and "present test phase." To say a module controls the single exit at the next-to-Iast END DO.
another means that it initiates the action of the other,
and that the other module returns control to the first THECONTROL STRUCTURES
when it is finished. This is the only control path for
the lower level module; there is no other way to perform The three basic control structures, the fourth
its function except through the one higher level module introduced in the example program, and a fifth are
that controls it. The single exception is a subroutine illustrated in flowchart form in Figure 3. The circles
that may be called by more than one module. This are collector nodes and are used to emphasize the single
has the same effect as several copies of the subroutine entry or exit point for a structure. Also emphasized by
being in the program, one copy for each calling module.

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

logical thinkers. It does not occur to them that human CONCLUSION


limitations can keep them from writing the maximally
efficient, logically streamlined programs that may be While learning to use SP is not as easy as users would
theoretically possible but practically impossible. like, the benefits are well worth it. One advantage not
Another disadvantage is the lack of statement forms noted earlier is the relative quickness with which
in most popular languages that correspond to the SP programs can be written. The time gained here and from
structures. To perform an IF THEN ELSE in BASIC, less debugging should more than compensate for the
one is forced to use GO TOs. This sort of simulation effort to learn SP. I recommend it for all but the most
can produce code that is not much clearer, and probably time-critical operations.
less clear in some cases, than unstructured code.
Nevertheless, the modularity advantage of SP is reason
enough to use it in these languages. Also, the situation REFERENCE NOTES
is improving: The new ANS FORTRAN 77 is an
example, although it will likely be some time before 1. Mills, H. D. How to write correct programs and know it
(Report No. FSC 73-5008). Gaithersburg, Md: IBM Federal Sys-
this and other language advances will be available in the
tems Division, 1973.
compilers being used by most people. 2. Mills, H. D. Mathematical foundations for structured pro-
gramming (Report No. FSC 72-(012). Gaithersburg, Md: IBM
USING SP Federal Systems Division, 1972.

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.

View publication stats

You might also like