To Documents

Structured Programming

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 C#

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:
      int x, y, z;
      x = 5;
      y = 11;
      z = x + y;
      txtOutput.Text = 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 action can be executed.
     
      General form of a while loop:
      while(condition)
      {
          action
      }
      
      Example of a while loop:
      int x = 2;
      string s = ;
      while(x < 100)
      {
          s = s +   + x;
          x = x * x;
      }
      txtOutput.Text = s;
      
      General form of a for loop:
      for(initialization; condition; iteration)
      {
          action
      }
      
      Example of a for loop:
      string s = ;
      for(int x = 2; x < 100; x = x * x)
      {
          s = s +   + x;
      }
      txtOutput.Text = s;
      
  3. Selection.   Execute a block of code (action) if a condition is true. The block of code is executed zero or one times.
     
      General form of an if statement:
      if (condition)
      {
          action
      }
      
      Example of an if statement:

      var x = 5, y = 2;
      if (x > y)
      {
          txtOutput.Text = x is greater than y.;
      }
      
  4. If-else Statement   To make programs easier to read, if statements can be modified to contain more than one condition and action:

      General form of an if-else statement:
      if (condition1)
      {
          action1
      }
      else if (condition2)
      {
          action2
      }
      else if (condition3)
      {
          action3
      }
      else
      {
          defaultAction
      }
      
      Example of an if-else statement:
      int n = 3;
      if (n == 1)
      {
          txtOutput.Text = one;
      }
      else if (n == 2)
      {
          txtOutput.Text = two;
      }
      else if (n == 3)
      {
          txtOutput.Text = three;
      }
      else
      {
          txtOutput.Text = many;
      }
      
  5. Switch Statement   Execute the first action whose value is found. Otherwise execute the default action. Here is the general form:

      General form of a switch statement:
      switch (value)
      {
          case constant1: 
              action1
              break;
         
          case constant2:
              action2
              break:
      
          case constant3:
              action3
              break:
      
          default:
              defaultAction
      }
      
      Example of a switch statement:
      int n = 3;
      switch(n)
      {
          case 1:
              txtOutput.Text = one;
              break;
      
          case 2:
              txtOutput.Text = two;
              break;
      
          case 3:
              txtOutput.Text = three;
              break;
      
          default:
              txtOutput.Text = many;
      }