To Documents

Structured Programming

Background

Since the invention by Von Neumann of the stored program computer in 1948, electrical engineers 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. Edsger 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, which uses three constructions: sequence, repetition, and decision.

JavaScript 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 courseScore
    if courseScore is greater than or equal to 90
       assign course grade to A
    else if courseScore is greater than or equal to 80
       assign course grade to B
    else if courseScore is greater than or equal to 70
       assign course grade to C
    else if courseScore is greater than or equal to 60
       assign course grade to D
    else
       assign course grade to F
    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
       else if piece is magazine
          put in magazine rack
       else if piece is bill
          pay it
       else if piece is junk mail
          throw it in wastebasket
       end
    end
    

Structured Programming in JavaScript

Structured programming is a program written with only the three constructions sequence, decision (if..else if statements), repetition (while or for statements). Important: (1) the condition of a JavaScript if, else if, while, or for statement is placed in parentheses, (2) the body of a JavaScript if, else if, while, or for statement is enclosed in braces and indented four spaces.

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

    Example:
    // Sequence
    var x = 56;
    var y = 11;
    var z = x + y;
    document.writeln(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:
    // Decision Example
    if (x % 2 == 0) {
        document.writeln("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:
    // Repetition Example
    var n = 1;
    while (n < 100) {
        document.writeln(n + "<br>");
        n = 2 * 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 multiple times with different values of an index variable, which is n in the Definite Repetition Example.
    for (initialization; condition; iteration) {
        action;
    }
    
    Example:
    // Definite Repetition Example
    for (var n = 1; n <= 30; n++) {
        document.writeln(n + " ");
    }
    
    This form of the for statement is used by several languages, including C, C++, Java, C#, and of course JavaScript. There are four parts to this for statement:
    (a) Initialization: var n = 1
    (b) Condition: n < 30
    (c) Body: document.writeln(n + " ");
    (d) Iteration: n++
    Perform the initialization step once, then repeatedly: check the condition, perform the action (body), and perform the iteration. Stop when the condition is false.
  2. if..else if 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:
    // ElseIf Example
    var n = prompt("Enter a positive integer: ", "0")
    if (n == 1) {
        document.writeln("one");
    }
    else if (n == 2) {
        document.writeln("two");
    }
    else if (n == 3) {
        document.writeln("three");
    }
    else if (n > 3) {
        document.writeln("many");
    }
    else if (isNaN(n)) {
        document.writeln("Input is not a number");
    }
    else if (n == null || input == undefined) {
        document.writeln("Input canceled");
    }
    
  3. An alternative to if..else statements is the switch statement:
    // Switch Example
    var n = parseInt(
        prompt("Enter a positive integer: ", ""));
    switch(n) {
        case 1:  document.writeln("one");
                 break;
        case 2:  document.writeln("two");
                 break;
        case 3:  document.writeln("three");
                 break;
        default: document.writeln("other");
                 break;
    }
    
    The switch statements are more compact than the corresponding if..else statements. However, the switch statements are more limited in the comparisons that they can perform.