Structured programming can be seen as a subset or subdiscipline of procedural programming, one of the major programming paradigms. It is most famous for removing or reducing reliance on the GOTO statement (also known as "go to").
Historically, several different structuring techniques or methodologies have been developed for writing structured programs. The most common are
Most people mean one of the two latter when they use the term structured programming, and that is what this article will discuss.
"Concatenation" refers to a sequence of statements executed in order.
In "selection", one of a number of statements is executed depending on the state of the program.
This is usually expressed with keywords such as
if..then..else..endif, switch, or case.
In "repetition" a statement is executed until the program reaches a certain state
or applied to every element of a collection.
This is usually expressed with keywords such as
while, repeat, for or do..until.
Often it is recommended that each loop should only have one entry point (and in the original structural programming, also only one exit point), and a few languages enforce this.
Some languages, such as Dijkstra's original Guarded Command Language, emphasise the unity of these structures with a syntax which completely encloses the structure, as in if..fi.
In others, such as C, this is not the case,
which increases the risk of misunderstanding and incorrect modification.
In 1967 a letter from Dijkstra appeared in Communications of the ACM with the heading "Go to statement considered harmful." The letter, which cited the Böhm and Jacopini proof, called for the abolishment of GOTO from high-level languages in the interest of improving code quality. This letter is usually cited as the beginning of the structured programming debate.
Although, as Plauger mentioned, many programmers unfamiliar with the theorem doubted its claims, the more significant dispute in the ensuing years was whether structured programming could actually improve software's clarity, quality, and development time enough to justify training programmers in it. Dijkstra claimed that limiting the number of structures would help to focus a programmer's thinking, and would simplify the task of ensuring the program's correctness by dividing analysis into manageable steps. In his 1969 Notes on Structured Programming, Dijkstra wrote:
Donald Knuth accepted the principle that programs must be written with provability in mind, but he disagreed (and still disagrees) with abolishing the GOTO statement. In his 1974 paper, "Structured Programming with Goto Statements," he gave examples where he believed that a direct jump leads to clearer and more efficient code without sacrificing provability. Knuth proposed a looser structural constraint: It should be possible to draw a program's flow chart with all forward branches on the left, all backward branches on the right, and no branches crossing each other. Many of those knowledgeable in compilers and graph theory have advocated allowing only reducible flow graphs.
Structured programming theorists gained a major ally in the 1970s after IBM researcher Harlan Mills applied his interpretation of structured programming theory to the development of an indexing system for the New York Times research file. The project was a great engineering success, and managers at other companies cited it in support of adopting structured programming, although Dijkstra criticized the ways that Mills's interpretation differed from the published work.
As late as 1987 it was still possible to raise the question of structured programming in a computer science journal. Frank Rubin did so in that year with a letter, "'GOTO considered harmful' considered harmful." Numerous objections followed, including a response from Dijkstra that sharply criticized both Rubin and the concessions other writers made when responding to him.
As a programmer gains experience, he or she may find it easier to understand certain violations of the strict structured programming idea, and several programming languages in widespread use provide restricted jump statements and exception handling for use in these situations. The major industry languages (with the major exception of Java) also retain the GOTO statement for jumps within a procedure, and it remains widely used. Although Dijkstra succeeded in making structured programming the educational standard, he did not succeed in making it a strict requirement.
Although there is almost never a reason to have multiple points of entry to a subprogram, multiple exits are often used to reflect that a subprogram may have no more work to do, or may have encountered circumstances that prevent it from continuing.
A typical example of a simple procedure would be reading data from a file and processing it: open file; while (reading not finished) { read some data; if (error) { stop the subprogram and inform rest of the program about the error; } } process read data; finish the subprogram;
The "stop and inform" may be achieved by throwing an exception, second return from the procedure, labelled loop break, or even a goto. As the procedure has 2 exit points, it breaks the rules of Dijkstra's structured programming. Coding it in accordance with single point of exit rule would be very cumbersome. If there were more possible error conditions, with different cleanup rules, single exit point procedure would be extremely hard to read and understand, very likely even more so than an unstructured one with control handled by goto statements. On the other hand, structural programming without such a rule would result in very clean and readable code.
Most languages have adapted the multiple points of exit form of structural programming. C allows multiple paths to a structure's exit (such as "continue", "break", and "return"), newer languages have also "labelled breaks" (similar to the former, but allowing breaking out of more than just the innermost loop) and exceptions.
Strukturované programování | Strukturierte Programmiersprache | Programación estructurada | 구조적 프로그래밍 | Strukturalno programiranje | Struktūrinis programavimas | Pengaturcaraan Berstruktur | Gestructureerd programmeren | 構造化プログラミング | Programowanie strukturalne | Структурное программирование | Yapısal programlama | Структурне програмування
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Structured programming".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world