To Documents

Structured Programming Details

Background

Since the invention by Von Neumann of the stored program computer, a tremendous potential of computing equipment was recognized: the ability to alter its behavior, depending on the input data. Calculating machines had, in the early 1900s, 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.

 

Structured Programming in Everyday Life

1. Sequence

2. Repetition

3. Selection

 

Structured Programming in JavaScript

Structured programming is a program written with only the three constructions sequence, repetition, and decision.

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

      Example:

      x = 5;
      y = 11;
      z = x + y;
      document.write(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
      }
      
      Example:

      var x = 2;
      while(x < 100)
      {
          document.write(x +  );
          x = x * x;
      }
      
     

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

      if (condition)
      {
          action
      }

      Example:

      var x = 5, y = 2;
      if (x > y)
      {
          document.write(x is greater than y.);
      }
      
 

Extension: If-else Statements

To make programs easier to read, an additional construct was added to the basic three original structured programming constructs:

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

      if (condition1)
      {
          action1
      }
      else if (condition2)
      {
          action2
      }
      else if (condition3)
      {
          action3
      }
      else
      {
          defaultAction
      }
      
      Example:

      var n;
      n = window.prompt(Enter a positive integer.);
      if (n == 1)
      {
          document.write(one);
      }
      else if (n == 2)
      {
          document.write(two);
      }
      else if (n == 3)
      {
          document.write(three);
      }
      else if (n == 4)
      {
          document.write(four);
      }
      else
      {
          document.write(many);
      }