Structured Programming
Structured Programming
Structured Programming
Background
Since the invention by Von Neumann of the stored program computer, computer
scientists have known that a tremendous power of computing equipment was the
ability to alter its behavior, depending on the input data. Calculating machines had, for
some time, been able to perform fixed arithmetic operations on data, but the potential
of machines capable of making decisions opened up many new possibilities. Machines
that could make decisions were capable of sorting records, tabulating and
summarizing data, searching for information, and many more advanced operations
that could not even be imagined at the time.
In early programming languages, like Fortran (first invented in 1954) and various low
level machine languages, the goto statement allowed the computer to deviate from
the sequential execution of the program instructions. The goto statement was
recognized to be a very powerful construction, and soon, programs of increasing
complexity and power were developed.
However, the increasingly complex code that resulted from goto statements became
harder and harder to maintain. Dijkstra, in 1966, was one of the first persons to
recognize that this run away complexity of programs was due to the overuse of the
goto statement (Dijkstra, E. W., "Go To Considered Harmful," Communications of the
ACM, March 1966). In fact, it was determined shortly thereafter, that the goto
statement is not needed at all. Dijkstra showed that any program construction that
could be created with goto statements could be created more simply with the
sequence, repetition and decision constructions that are discussed in the following
sections. This was the birth of the discipline of Structured Programming.
Add flour.
Add salt.
Add yeast.
Mix.
Add water.
Knead.
Let rise.
Bake.
1. Sequence. Lines or blocks of code are written and executed in sequential order.
Example:
x = 5
y = 11
z = x + y
WriteLine(z)
While condition
action
End While
Example:
x = 2
While x < 100
WriteLine(x)
x = x * x
End
If condition Then
action
End If
Example:
x = ReadLine()
If x Mod 2 = 0
WriteLine("The number is even.")
End If
To make programs easier to read, some additional constructs were added to the basic
three original structured programming constructs:
Example:
For i = 1 To 20
WriteLine(i)
Next i
Example:
For i = 20 To 1 Step -1
WriteLine(i)
Next
If condition1 Then
action1
ElseIf condition2 Then
action2
ElseIf condition3 Then
action3
Else
defaultAction
End If
Example:
If n = 1 Then
WriteLine("One")
ElseIf n = 2 Then
WriteLine("Two")
ElseIf n = 3 Then
WriteLine("Three")
Else
WriteLine("Many")
End If
Example:
Select Case n
Case 1
WriteLine("One")
Case 2
WriteLine("Two")
Case 3
WriteLine("Three")
Case Else
WriteLine("Many")
End Select