Structured Programming Languages
Structured Programming Languages come in all flavours, each with their own style and layout, from BASIC to C#. A structured programming language follows a methodology where the logic of a program is composed of simpler sub-structures, like building blocks.
This methodology is commonly called Dijkstra's structured programming. Edsger Wybe Dijkstra (1930 - 2002) was a Dutch computer scientist. He is known for his low opinion of the GOTO statement and wrote a paper in 1965, which is regarded as a major step towards the widespread depreciation of the GOTO statement. He also famed for coining the programming phrase "2 or more, use a for", alluding to the fact that when you find yourself processing more than one instance of a data structure, it is time to encapsulate that logic inside a loop.
Structured programming is often, but not always, associated with a top-down approach to design where an overview of the system is first formulated, specifying but not detailing any first-level subsystems. Each subsystem is then refined in yet greater detail until the entire specification is reduced to base elements. This idea is the basis for structured design methodologies such as SSADM and UML.
Structured programming usually involves a procedural language, rather that a linear language such as BASIC, although any programming language can be structured.
The first example of first generation BASIC illustrates linear style programming, where the program executes instructions from the top to the bottom. This can be considered as structured as the flow of control does not break out of the linear path.
- 10 INPUT "What is your name: ", U$
- 20 PRINT "Hello "; U$
- 30 INPUT "How many stars do you want: ", N
- 40 S$ = ""
- 50 FOR I = 1 TO N
- 60 S$ = S$ + "*"
- 70 NEXT I
- 80 PRINT S$
- 90 INPUT "Do you want more stars? ", A$
- 100 IF LEN(A$) = 0 THEN 90
- 110 A$ = LEFT$(A$, 1)
- 120 IF A$ = "Y" OR A$ = "y" THEN 30
- 130 PRINT "Goodbye ";
- 140 FOR I = 1 TO 200
- 150 PRINT U$; " ";
- 160 NEXT I
- 170 PRINT
- 180 END
The next example of BASIC illustrates a non-structured program, commonly called spaghetti code. In this example it is easy to see what the code is doing, however, a large program of hundreds of lines would be almost impossible to debug.
- 10 GOTO 40
- 20 PRINT "line number 20"
- 30 GOTO 60
- 40 PRINT "line number 40"
- 50 GOTO 20
- 60 END
This code executes statements in a seemingly random order. It does not appear to execute code in a linear fashion. The result of this code would be:
line number 40 line number 20
It is also possible while using the goto statement to skip over lines of code so that they are never executed:
- Print "An example of a GOTO statement"
- Sleep 1000
- Goto program_continue
- Print "This line of code will never be executed"
- Print "We just skipped some code"
For these reasons the goto statement is frowned upon and is not permitted within structured programming languages. Procedures (subs, functions, methods and so on...) are permitted as they still retain a linear flow of control.
In the next example, a sub procedure the program executes in a linear order but jumps down to the procedure when the line is invoked. After the procedure has completed, again in a linear fashion, the flow returns to the same line it jumped out of and continues to the end of the code. This is known as procedural programming.
- Print "This Program reads all lines of code"
- Gosub program_continue
- Print "This line isn't left out"
- Print "We like programs that talk to us!"
Another requirement of a structured programming language is the use of block statements. A language is described as "block-structured" when it has syntax for enclosing structures between bracketed keywords. For example in Pascal, blocks of code are contained within BEGIN and END statements:
- for i := 0 to 10 do
and C uses curly brackets (braces) to denote blocks of code:
- for (i=0; i<=10; i++)
Another type of block structure is the comb. These have more than two keywords to denote block structure, for example, the ADA language uses IF, THEN, ELSE, END IF as separate "teeth" on the comb.
- if <em>condition</em> then
- -- statements;
- elseif condition then
- -- more statements;
- elseif condition then
- -- more statements;
- else condition then
- -- other statements;
- end if;
As you gain experience you may find it easier to understand certain violations of the strictly 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 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.