To Documents

Structured Programming Details

Background

Since the invention by Von Neumann of the stored program computer, it was recognized that a tremendous potential 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 possibililities. 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 increasing complex code 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. This was the birth of the discipline of Structured Programming.

Python does not have a goto statement.

Structured Programming in Everyday Life

  1. Sequence

    Example: Baking Bread
    Add flour
    Add salt
    Add yeast
    Mix
    Add water
    Knead
    Let rise
    Bake
    
  2. Repetition

    Example: Washing Dishes
    stack dishes by sink
    fill sink with hot soapy water
    while there are more dishes
       get dish from counter
       wash dish
       put dish in drain rack
    end
    wipe off counter
    rinse out sink
    
  3. Selection

    Example: Computing Course Grade
    compute course_score
    if course_score is greater than or equal to 90
       assign course grade to A
    elsif course_score is greater than or equal to 75
       assign course grade to B
    elsif course_score is greater than or equal to 60
       assign course grade to C
    elsif course_score is greater than or equal to 50
       assign course grade to D
    else
       assign course grade to D
    end
    submit course grade to registrar
     
    
  4. Repetition and Selection

    Example: Sorting Mail
    get mail from mailbox
    put mail on table
    while more mail to sort
       get piece of mail from table
       if piece is personal
          read it
       elsif piece is magazine
          put in magazine rack
       elsif piece is bill
          pay it
       elsif piece is junk mail
          throw it in wastebasket
       end
    end
    

Structured Programming in Python

Structured programming is a program written with only the three constructions sequence, decision (if..elif statements), and repetition (while or for statements). Important: the body of a Python if, elif, while, or for statement is indicated by indenting four spaces.  Python does not use end statements.

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

    Example:
    x = 56
    y = 11
    z = x + y
    print(z)
    
  2. Decision.   Execute a block of code (action) if a condition is true. The block of code is executed at most once.
    if condition:
        action
    
    Example:
    if x % 2 == 0:
        print("The number is even.")
    
  3. 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
    
    Example:
    n = 1
    while n < 100:
        print(n)
        n = n * n
    

Extensions

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

  1. Definite Repetition   Execute a block of code once for each element in an array:
    for element in a range
      action
    end
    
    Example:
    for x in range(1, 5):
        print(x, end="")
    
    Important: A Python range is inclusive for the lower limit, but exclusive for the upper limit. range(1, 5) actually goes from 1 to 5 - 1 = 4.  This range consists of the values [1, 2, 3, 4].
  2. if..elif Statements  Execute the first action whose corresponding condition is true. Here is the general form:
    if condition1:
        action1
    
    elif condition2:
        action2
    
    elif condition3:
        action3
    
    else
        default_action:
    
    Example:
    n = int(input("Enter a positive integer: "))
    
    if n == 1:
        print "one"
    
    elsif n == 2
        print "two"
    
    elsif n == 2
        print "three"
    
    else:
        print "many"