Thursday, 13 June 2013

Structured Programming Language



STRUCTURED PROGRAMMING LANGUAGE

 A set of quality standards that make programs more verbose but more readable, reliable, and easily maintained. The goal of structured programming is to avoid spaghetti code(Spaghetti code is a pejorative term for source code that has a complex and tangled control structure, especially one using many GOTOs, exceptions, threads, or other "unstructured" branching constructs) caused by overreliance on GOTO statements, a problem often found in BASIC and FORTRAN programs. Structured programming-such as that promoted by C, Pascal Modula-2, and the dBASE software command language - insists that the overall program structure reflect what the program is supposed to do, beginning with the first task and proceeding logically. Indentations help make the logic clear, and the programmer is encouraged to use loops and branch control structures and named procedures rather than GOTO statements.
STRUCTURED PROGRAMMING is the foundation of Modular programming and Object Oriented Programming.
Structured programming frequently employs a top-down design model, in which developers map out the overall program structure into separate subsections. A defined function or set of similar functions is coded in a separate module or submodule, which means that code can be loaded into memory more efficiently and that modules can be reused in other programs. After a module has been tested individually, it is then integrated with other modules into the overall program structure.
Program flow follows a simple hierarchical model that employs looping constructs such as "for," "repeat," and "while." Use of the "Go To" statement is discouraged.
Structured programming was first suggested by Corrado Bohm and Guiseppe Jacopini. The two mathematicians demonstrated that any computer program can be written with just three structures: decisions, sequences, and loops.
·         "Sequence" refers to an ordered execution of statements.
·         In "selection" one of a number of statements is executed depending on the state of the program. This is usually expressed with keywords such as if..then..else..end ifswitch, or case. In some languages keywords cannot be written verbatim, but must be stropped.
·         In "repetition" a statement is executed until the program reaches a certain state, or operations have been applied to every element of a collection. This is usually expressed with keywords such as whilerepeatfor or do..until. Often it is recommended that each loop should only have one entry point (and in the original structural programming, also only one exit point, and a few languages enforce this).

 Edsger Dijkstra's subsequent article, Go To Statement Considered Harmful was instrumental in the trend towards structured programming. The most common methodology employed was developed by Dijkstra. In this model (which is often considered to be synonymous with structured programming, although other models exist) the developer separates programs into subsections that each have only one point of access and one point of exit.
Almost any language can use structured programming techniques to avoid common pitfalls of unstructured languages. Unstructured programming must rely upon the discipline of the developer to avoid structural problems, and as a consequence may result in poorly organized programs. Most modern procedural languages include features that encourage structured programming. Object-oriented programming (OOP) can be thought of as a type of structured programming, uses structured programming techniques for program flow, and adds more structure for data to the model.

Rules for Structured Programming

Lines  
1.  Put only one statement on a line.
2.  Split long lines so that they fit on the page without running over. Indent line continuation.

Identifiers
3.  Use meaningful variable names and function names.
4.  Use all CAPITALS for names of constants. Use lowercase or mixed capital and lowercase for names of variables and other identifiers.
5.  Some instructors prefer a capital letter at the beginning of a class name—for example, class Book.

Comments

class=Section2>
6.  Every program and every function must have a comment at the beginning that tells what it does.
7.  Do not comment closing braces  (// end while).  [However, some instructors like this type of comment.]

Functions
8.  Divide program into functions.
9.  Each function should perform one task.
10.  Each function must have a comment.
11. Put all prototypes above main.  [Some instructors want a comment labeling the prototype section.  For example, // here are the function prototypes ]
12. Put all user-defined functions after main().  [Some instructors may suggest putting user-defined functions before main(), but most prefer that these functions are all after main().]
13. Do not use global items except for file declarations and class definitions. [Some instructors do not permit any global variables at all, including filenames.]

Blank Lines
14. Skip a line between functions.
15. Skip a line after the declaration section in main() and in each function.
16. Skip lines when useful for clarity, but do not double-space your program.

Braces  (there are some alternative rules shown below)
17. Never put more than one closing } on a line.
18. Put each opening { on a line by itself.
19. For loops and if statements, you may put { on the line with the keyword and put } on a line by itself aligned under the keyword.
20. With a few exceptions (such as following rule 18), align { and }.
21. For functions, put { and } on separate lines. 

Indenting
22. Follow rules of indenting.

         How to indent:
               a. Each indent must be at least 5 spaces (some instructors say 3 spaces are enough).
               b. Each time a construct (loop, if statement) occurs inside another construct, indent
                   5 (or 3) more spaces.
              c. Move back out at the end of the construct.
              d. Align statements that are indented at the same level.


         What to indent:
              a. Indent body of main and body of a function (not the header or comment).
              b. Indent body of a loop.
              c. Indent body of an if statement or an else statement.
              d. Indent statements in a switch statement (indent each case, and further indent
                  each action).
              e. Indent items within a class definition.
              f. Indent continued lines.


Example

// Program gives examples of structured programming techniques
// follows brace style identified in rules 17-21
#include <iostream>
using namepace std;
void readarr(int &,int &);
void changearr(int &,int);
void printarr(int arr,int n);
const int SIZE=50;
class justtoshowone {
     int firstitem;
     string seconditem;
     double thirditem;
};

int main()
{
     int n,arr[SIZE];
     justtoshowone example;

     readarr(arr,n);
     changearr(arr,n);
     sum = sumarray(arr,n);
     cout << "the sum of the array is" << sum << endl;
     printarr(arr,n);
     return 0;
}

// reads n and then reads n values into an array, printing the values read in
void readarr(int &n,int arr[])
{
     cin >> n;
     while (n < 0 || n >= SIZE)
          cin >> n;
     for(int i = 0; i < n; i++) {
          cin >> arr[i];
          cout << arr[i] << endl;
     }
     return;
}

// changes array values based on sign of original value;
// counts changes
void changearr(int arr[],int n)
{
      int increased=0, decreased=0,negated=0;

      for(int i = 0; i < n; i++) {
          if (arr[i] == 0) {
               arr[i]++;
               increased++;
          }
          else if (arr[i] < 0) {
              arr[i] = -arr[i];
              negated++;
          }
          else {
              arr[i]--;
              decreased++;
          }
     }
     cout << "# increased is " << increased <<
          << "# decreased is " << decreased <<
          << "# negated is " << negated << endl;
     return;
}

// prints resulting array
void printarr(int arr[],int n)
{
     for(int i = 0; i < n; i++)
          cout << arr[i];
     return;
}


23. There are alternate acceptable forms for indenting and placement of braces, such as the following. Whatever method you choose, be consistent.

      a. Alternate indenting forms and brace placement for if else:

          i. Cascading if statement
 
          if (arr[i] == 0) {
               arr[i]++;
               increased++;
          }
          else if (arr[i] < 0) {
                    arr[i] = -arr[i];
                    negated++;
               }
               else {
                    arr[i]--;
                    decreased++;
               }

          if (arr[i] == 0) {
               arr[i]++;
               increased++;
          }
          else if (arr[i] < 0) {
                    arr[i] = -arr[i];
                    negated++;
               }
               else {
                    arr[i]--;
                    decreased++;
               }


     ii. Braces on same lines with else:

           if (arr[i] == 0) {
               arr[i]++;
               increased++;
           } else if (arr[i] < 0) {
                      arr[i] = -arr[i];
                      negated++;
                 } else {
                        arr[i]--;
                        decreased++;
                 }

     iii. Braces on separate lines (this can be combined with various indenting styles):

           if (arr[i] == 0)
          {
               arr[i]++;
               increased++;
          }
          else if (arr[i] < 0)
               {
                    arr[i] = -arr[i];
                    negated++;
               }
               else
                    {
                         arr[i]--;
                         decreased++;
                    }

b. Alternate brace placement for a loop header:

     for(int i = 0; i < n; i++)
     {
          cin >> arr[i];
          cout << arr[i] << endl;
     }

No comments:

Post a Comment