Structured Programming

To Documents

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.

 

Structured Programming in Everyday Life

1. Sequence   Execute a list of statements in order.

 

2. Repetition   Repeat a block of statements while a condition is true.

 

3. Selection   Choose at most one action from several alternative conditions.

 

Structured Programming in Visual Basic

Structured programming is a program written with only the structured programming constructions: (1) sequence, (2) repetition, and (3) selection.

 

  1. Sequence.   Lines or blocks of code are written and executed in sequential order.

    Example:

    x = 5
    y = 11
    z = x + y
    WriteLine(z)

     

  2. Repetition.   Repeat a block of code (Action) while a condition is true. There is no limit to the number of times that the block can be executed.

    While condition
        action
    End While

    Example:

    x = 2
    While x < 100
        WriteLine(x)
        x = x * x
    End
     

  3. Selection.   Execute a block of code (Action) if a condition is true. The block of code is executed at most once.

    If condition Then
        action
    End If

    Example:

    x = ReadLine()
    If x Mod 2 = 0
        WriteLine("The number is even.")
    End If

 

Extensions to Structured Programming

To make programs easier to read, some additional constructs were added to the basic three original structured programming constructs:

 

  1. Definite Repetition (For Loop)   Combine initialization, checking a condition, and incrementing a counter in a single statement called a for statement. Here is the general form:

    For i = 1 To n Step k   ' Step k is optional
         action
    Next

    Example:

    For i = 1 To 20
        WriteLine(i)
    Next i

    Example:

    For i = 20 To 1 Step -1
        WriteLine(i)
    Next

     

  2. If-Then-Else Statements   Execute the first action whose corresponding condition is true. Here is the general form:

    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

     

  3. Select Statement   Execute the action corresponding to the value of the expression.

    Select Case value1
        Case value1
           action1
        Case value2
           action2
        Case value3
           action3
        Case Else
           defaultAction
    End Select

    Example:

    Select Case n
        Case 1
           WriteLine("One")
        Case 2
           WriteLine("Two")
        Case 3
           WriteLine("Three")
        Case Else
           WriteLine("Many")
    End Select