Three Basic Structures of Structured Programming
By: Wendy • Essay • 1,073 Words • November 29, 2009 • 2,124 Views
Essay title: Three Basic Structures of Structured Programming
INTRODUCTION
Structured programming is one of the several different ways in which a programming language can be constructed. “It was originally introduced as a means of getting away from the 'spaghetti' code that was used in the early days and to provide some means by which programmers could more easily follow code written by other programmers.” (Hendren, 1998) Structured programming is a procedure-oriented method of designing and coding a program.
At a low level, structured programs are composed of simple, hierarchical program flow structures. “These are regarded as single statements, and are the same time ways of combining simpler statements, which may be one of these structures, or primitive statements such as assignments or procedure calls.” (Dijkstra, 1990) The three basic types of structure identified by Dijkstra were concatenation, selection, and repetition.
LITERATURE REVIEW
Simple sequential code is most easily expressed by concatenation i.e. listing the pieces of code in the correct order. The main related question is how to indicate the boundary between statements. “Some languages just use the end of line but then usually require an explicit continuation symbol for statements too long to fit on a single line. Others languages use an explicit character such as, with some of these treating it as a separator and the rest treating it as the statement terminator.” (Birrell, 1995)
To turn a code sequence into a single entity, usually known as a compound statement, some languages use an explicit pair of brackets, such as {} or begin end, as a general form useable with any structure. Some languages have specific forms for each structure, such as if fi (or if end if) and while od (or while end while). Some languages have both general and specific forms, and others have neither. Because structures often end up deeply nested, specific keywords can be much easier to pair up than general pairs.
“The classic selector is the conditional or if statement/expression, and almost all languages have it, usually in two slightly different forms such as if then and if then else.” (Agresti, 2000) Some languages place restrictions on the then and else parts, but most modern languages have no such restrictions, allowing nested if statements.
One common pattern of use is:
If ... then...
Else if ... then...
Else if ... then...
. . .
else ...
And this has given rise to a different kind of selection construct, the case or switch. This first arose as a modified goto:
goto label_array[n]
But this was even more dangerous than normal gotos, and was also replaced. A switch construct is essentially a list of (value, action) pairs. We can generalize the switch into a block of guarded statements, where at most one of the statements is obeyed e.g.:
if
I < j then j -= I
j < I then I -= j
end
We can even allow for more than one of the guard expressions being valid, either by picking the first such in the list, or by non-deterministically picking a valid guard at random.
“Repetition comes in two forms: recursion and loops. Some languages have both; some only have one or the other. Loops come in two forms: indeterminate, where the number of repetitions is unknown until the loop terminates, and determinate, where the number of repetitions is known when the loop starts. Again, some languages with iteration have both forms, some only one and the other.” (Jackson, 2002)
Dijkstra's (indeterminate) repetition construct, which involves a test in the middle of the loop, is not often directly implemented in programming languages. However, special cases of it are common: a version with the first box missing so the test is at the start is found in most languages, and a version with the last box missing so the test is at the end is often found as well.
“Almost