Vous êtes sur la page 1sur 2

Control Structures

by: David Harel

Sequence control is usually carried out with the aid of various combinations of
instructions called control-flow structures, or simply control structures. Even the
chocolate mousse recipe contains several typical ones, such as the following: Direct
sequencing, of the form “do A followed by B,” or “do A and then B.” (Every semicolon
or period in the recipe hides an implicit “and then” phrase, for example, “gently fold in
chocolate; [and then] reheat slightly ...”) Conditional branching, of the form“if Q then
do A otherwise do B,” or just “if Q then do A,”where Q is some condition. (For
example, in the recipe “reheat slightly to melt chocolate, if necessary,” or “serve with
whipped cream, if desired.”) As it happens, these two control constructs, sequencing
and branching, do not ex- plain how an algorithm of fixed—maybe even short—length
can describe processes that can grow increasingly long, depending on the particular
input. An algorithm containing only sequencing and branching can prescribe processes
of some bounded length only, since no part of such an algorithmis ever executedmore
than once. Control constructs that are responsible for prescribing ever-longer processes
are indeed hidden even in the mousse recipe, but they are far more explicit in algorithms
that deal with many inputs of different sizes, such as the salary summation algorithm.
They are generically called iterations,or looping constructs, and come in many

flavors. Here are two: Bounded iteration, of the general form “do A exactly N times,”
where N is a number. Conditional iteration, sometimes called unbounded iteration, of
the form “repeat A until Q,” or “while Q do A,” where Q is a condition. (For example,
in the recipe “beat egg whites until foamy.”) When describing the salary summation
algorithm in Chapter 1, we were quite vague about the way the main part of the
algorithm was to be carried out; we said “proceed through the list, adding each
employee’s salary to the noted number,” and then “having reached the end of the list,
produce the noted number as output.” We should really have used an iteration construct,
that not onlymakes precise the task of the processor proceeding through the list, but also
signals the end of the list. Let us assume then that the input to the problem includes not
only the list of employees, but also its length; that is, the total number of employees,
designated by the letter N.It is nowpossible to use a bounded iteration construct,
yielding the following algorithm:

(1) make a note of 0; point to the first salary on the list;

(2) do the following N − 1 times:

(2.1) add the salary pointed at to the noted number;

(2.2) point to the next salary;

(3) add the salary pointed at to the noted number;

(4) produce the noted number as output.


The phrase “the following” in clause (2) refers to the segment consisting of

subclauses (2.1) and (2.2). This convention, coupled with textual indentation to

emphasize the “nested” nature of (2.1) and (2.2), will be used freely in the sequel.

You are encouraged to seek the reason for using N − 1 and adding the final salary
separately, rather than simply using N and then producing the output and halting. Notice
that the algorithm fails if the list is empty (that is, if N is 0), since the second part of
clause (1) makes no sense. If the input does not include N, the total number of
employees, we must use a conditional iteration that requires us to provide a way by
which the algorithm can sense when it has reached the end of the list. The resulting
algorithm would look very much like the version given, but would use the form “repeat
the following until end of list reached” in clause (2). You should try writing down the
full algorithm for this case. Notice how iteration constructs make it possible for a short
portion of an algorithm’s text to prescribe very long processes, the length being dictated
by the size of the inputs—in this case the length of the employee list. Iteration,
therefore, is the key to the seeming paradox of a single, fixed algorithm performing
tasks of ever-longer duration.

Reference: Algorithmics spirit of computing

Date: 26-08-2008

Vous aimerez peut-être aussi