0% found this document useful (0 votes)
85 views21 pages

Prog FlowchartTechniques

The document discusses programming techniques and flowcharting. It defines a flowchart as a graphical representation of the sequence of operations in a program. It notes that flowcharts use standardized symbols like terminals, inputs/outputs, processes, and decisions to represent the logic and flow of a program. The document also provides examples of common flowchart symbols and their meanings. It emphasizes that flowcharts help programmers reduce errors and guide the writing of computer programs.

Uploaded by

Mahesh Kadam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views21 pages

Prog FlowchartTechniques

The document discusses programming techniques and flowcharting. It defines a flowchart as a graphical representation of the sequence of operations in a program. It notes that flowcharts use standardized symbols like terminals, inputs/outputs, processes, and decisions to represent the logic and flow of a program. The document also provides examples of common flowchart symbols and their meanings. It emphasizes that flowcharts help programmers reduce errors and guide the writing of computer programs.

Uploaded by

Mahesh Kadam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

Programming Techniques & Flowchart

PROGRAMMING & Flowchart Techniques


Programming Aids
Programmers use different kinds of tools or aids which help them in developing programs faster and better. Such aids are studied in the following paragraphs. Important aids available to a programmer are flowcharting and pseudocode which help him in constructing programs very fast and very easily.

Flowcharts
A flowchart is a graphical representation of the sequence of operations in an information system or program. Program flowcharts show the sequence of instructions in a single program or subroutine. Flowchart uses boxes of different shapes to denote different types of instructions. The actual instructions are written within these boxes using clear and concise statements. These boxes are connected by solid lines having arrow marks to indicate the flow of operation, that is, the exact sequence in which the instructions are to be executed. Since a flowchart shows the flow of operations in pictorial form, any error in the logic of the procedure can be detected easily. Once the flowchart is ready, the programmer can forget about the logic and can concentrate only on coding the operations in each box of the flowchart in terms of the statements of the programming language. This will normally ensure an error-free program. A flowchart is basically the plan to be followed when the program is written. It acts like a road map for a programmer and guides him in proceeding from the starting point to the final point while writing a computer program.For a beginner it is strongly recommended that a flowchart be drawn first in order to reduce the number of errors and omissions in the program. Moreover, it is a good practice to have a flowchart along with a computer program as it is very helpful during the testing of the program and also in incorporating any modifications in the program.

Flowchart Symbols
Only a few symbols are needed to indicate the necessary operations in a flowchart. These symbols have been standardized by the American National Standards Institute (ANSI).

Terminal The terminal symbol, as the name implies, is used to indicate the starting (BEGIN), stopping (END), and pause (HALT) in the program logic flow. It is the first symbol and the last symbol in the program logic. In addition, if the program logic calls for a pause in the program, that also is indicated with a terminal symbol. A pause is normally used in the program logic under some error conditions or in case the forms had to be changed in the computers line printer during the processing of that program. Input/Output The input/output symbol is used to denote any function of an input/output device in the program. If there is a program instruction to input data from a disk, tape, card reader, terminal, or any other type of input device, that step will be indicated in the flowchart with an input/output symbol. Similarly, all output instructions, whether it is output on a printer, magnetic tape, magnetic disk, terminal screen, or any output device, are indicated in the flowchart with an input/output symbol.

Programming Techniques & Flowchart

Flowchart Symbols
The various symbols used in flowcharts are shown in the chart below along with their meaning.

Symbol

Name

Use

Terminal

Indicates start and stop of the process.

Input/output

Indicates an input or output task.

Process

Contains Arithmetic or Assignment task.

Decision

Indicates a condition and two or more alternatives.

Flow lines

Shows direction of flow of control.

Processing A processing symbol is used in a flowchart to represent arithmetic and data movement instructions. Thus, all arithmetic processes such as adding, subtracting, multiplying and dividing are shown by a processing symbol. The logical process of moving data from one location of the main memory to another is also denoted by this symbol. When more than one arithmetic and data movement instructions are to be executed consecutively, they are normally placed in the same processing box and they are assumed to be executed in the order of their appearance.

Programming Techniques & Flowchart

Flow lines Flowlines with arrowheads are used to indicate the flow of operation, that is, the exact sequence in which the instructions are to be executed. The normal flow of flowchart is from top to bottom and left to right. Arrowheads are required only when the normal top to bottom flow is not to be followed. However, as a good practice and in order to avoid confusion, flow lines are usually drawn with an arrowhead at the point of entry to a symbol. Good practice also dictates that flow lines should not cross each other and that such intersections should be avoided whenever possible. Decision The decision symbol is used in a flowchart to indicate a point at which a decision has to be made and a branch to one of two or more alternative points is possible. Connector If a flowchart becomes very long, the flow lines start crisscrossing at many places that causes confusion and reduces the clarity of the flowchart. Moreover, there are instances when a flowchart becomes too long to fit in a single page and the use of flow lines becomes impossible. Thus, whenever a flowchart becomes too complex that the number and direction of flow lines is confusing or it spreads over more than one page, it is useful to utilize the connector symbol as a substitute for flow lines. This symbol represents an entry from, or an exit to another part of the flowchart. A connector symbol is represented by a circle and a letter or digit is placed within the circle to indicate the link. A pair of identically labeled connector symbols are commonly used to indicate a continued flow when the use of a line is confusing. So two connectors with identical labels serve the same function as a long flow line. That is, they show an exit to some other chart section, or they indicate an entry from another part of the chart. How is it possible to determine if a connector is used as an entry or an exit point? It is very simple: if an arrow enters but does not leave a connector, it is an exit point and program control is transferred to the identically labeled connector that does have an outlet. It may be noted that connectors do not represent any operation and their use in a flowchart is only for the sake of convenience and clarity.

Programming Techniques & Flowchart


Name Use

Symbol

Module Call

Used foe calling an external module.

Initialization
Action

Loop

This indicates a loop structure.

Condition

Connector

It is used to connect different flowlines or used to show continuity of flowchart on separate pages.

Block

Indicates a block of statements.

Multiway selection

Allows selection of one out of many alternatives.

1.

External Module

In many cases, a program may invoke or call external sub routines to perform certain operations. The module can be called using this symbol. The name of the module is written within this symbol. DISPLAY MAT

CALL FACTORIAL

Programming Techniques & Flowchart

Initialization Action Condition

2.

Loop Statement

This symbol is used to indicate a loop or a respective structure.

It consists of three parts - the initial value, the termination condition and the action to be performed. This is followed by the block statements are to be repeated. The block will be repeated as long as the condition is True.

The block of statements is enclosed in the symbol:

Example:

n=1 n=n+1 n < = 10

False

True Display n

Sum = Sum + n

3.

Multiway Selection

Programming Techniques & Flowchart


We have seen the two-way selection symbol earlier. If we have to choose between one of many alternatives, then we can use this symbol.

All the choices are finally joined using the connector symbol:

Example:

A B SUM PROD

Choic e

DIFF

POWER

1. Flowchart to calculate sum of first numbers i.e 1 + 2 + 3+..+n


Start

Read N

SUM = 0

NUM = 1

SUM = SUM + NUM

NUM = NUM + 1

NUM < = N

Display SUM

6
Stop

Programming Techniques & Flowchart

Example 1.1
Draw a flowchart for adding marks in ten subjects obtained by a student in an examination. The output should print the percentage of marks of the student in the examination. The flowchart for this problem is given in Figure 1.2 The first symbol is a terminal labeled START. It shows that this is the starting point or beginning of our flowchart logic. The second symbol is an input/output (I/O) symbol that is labeled specifically to show that this step is to read input data. This step will input the roll number, name, and the marks obtained by the student from an input device into the main storage of the computer system. The third symbol is a processing symbol which is suitably labeled to indicate that at this step, the computer will add the marks obtained by the student in various subjects and then store the sum in a memory location which has been given the name TOTAL. The fourth symbol is again a processing symbol. The label inside it clearly indicates that the percentage marks obtained by a student is calculated at this stage by dividing TOTAL by 10 and the result is stored in a memory location which has been given the name PERCENTAGE. The fifth symbol is an input/output (I/O) symbol and is labeled WRITE OUTPUT DATA.

Programming Techniques & Flowchart

Flowcharting Rules
(a) First formulate the main line of logic, then incorporate the details. (b) Maintain a consistent level of detail for a given flowchart. (c) Do not give every detail on the flowchart. A reader who is interested in greater details can refer to the program itself. (d) Words in the flowchart symbols should be common statements and easy to understand. (e) Be consistent in using names and variables in the flowchart. (f) Go from left to right and top to bottom in constructing the flowchart. (g) Keep the flowchart as simple as possible. The crossing of flow lines should be avoided as far as possible. (h) If a new flowcharting page is needed, it is recommended that the flowchart be broken at an input or output point.Moreover, properly labeled connectors should be used to link the portions of the flowchart on different pages.

Advantages of Flowcharts
Conveys Better Meaning Since a flowchart is a pictorial representation of a program, it is easier for a programmer to understand and explain the logic of the program to some other programmer. Analyses the Problem Effectively A macro flowchart that charts the main line of logic of a software system becomes a system model that can be broken down into detailed parts for study and further analysis of the system. Effective Joining of a Part of a System A group of programmers are normally associated with the design of large software systems. Each programmer is responsible for designing only a part of the entire system. So initially, if each programmer draws a flowchart for his part of design, the flowcharts of all the programmers can be placed together to visualize the overall system design. Any problem in linking the various parts of the system can be easily detected at this stage and the design can be accordingly modified. Flowcharts can thus be used as working models in the design of new programs and software systems. Efficient Coding Once a flowchart is ready, programmers find it very easy to write the concerned program because the flowchart acts as a roadmap for them. It guides them in proceeding from the starting point of the program to the final point ensuring that no steps are omitted. The ultimate result is an error free program developed at a faster rate. Systematic Debugging Even after taking full care in program design, some errors may remain in the program because the designer might have never thought about a particular case. These errors are detected only when we start executing the program on a computer. Such type of program errors are called bugs and the process of removing these errors is known as debugging. A flowchart is very helpful in detecting, locating, and removing mistakes (bugs) in a program in a systematic manner.

Programming Techniques & Flowchart

Systematic Testing Testing is the process of confirming whether a program will successfully do all the jobs for which it has been designed under the specified constraints. For testing a program, different sets of data are fed as input to that program to test the different paths in the program logic.

Pseudocode or Metacode or PDL


Pseudocode is another program analysis tool that is used for planning program logic. "Pseudo" means imitation or false and "Code" refers to the instructions written in a programming language. Pseudocode, therefore, is an imitation of actual computer instructions. These pseudo instructions are phrases written in ordinary natural language (e.g., English, French, German, etc.). Instead of using symbols to describe the logic steps of a program, as in flowcharting, pseudocode uses a structure that resembles computer instructions. Because it emphasises the design of the program, pseudocode is also called Program Design Language (PDL) Pseudocode is made up of the following basic logic structures that have been proved to be sufficient for writing any computer program : 1. Sequence 2. Selection (IF...THEN...ELSE or IF....THEN) 1. Iteration (DO...WHILE or REPEAT...UNTIL)

Sequence
Sequence logic is used for performing instructions one after another in sequence. Thus, for sequence logic, pseudocode instructions are written in the order, or sequence, in which they are to be performed. The logic flow of pseudocode is from the top to the bottom.

Selection
Selection logic, also known as decision logic, is used for making decisions. It is used for selecting the proper path out of the two or more alternative paths in the program logic. Selection logic is depicted as either an IF...THEN...ELSE or IF.....THEN structure. The flowcharts shown in Figures illustrate the logic of these structures.

Programming Techniques & Flowchart

Their corresponding pseudocode is also given in these figures. The IF...THEN...ELSE construct says that if the condition is true, then do process 1, else (if the condition is not true) do process 2. Thus, in this case either process 1 or process 2 will be executed depending on whether the specified condition is true or false. However, if we do not want to choose between two processes and we simply want to decide if a process is to be performed or not, then the IF...THEN structure is used. The IF...THEN structure says that if the condition is true, then do process 1; and if it is not true, then skip over process 1. In both the structures, process 1 and process 2 can actually be one or more processes. They are not limited to a single process. END IF is used to indicate the end of the decision structures. The following pseudocode describes the policy of a company to award the bonus to an employee. Input Employee number, pay, position code & years. IF position code = 1 THEN set bonus to 1 weeks pay ELSE IF position code = 2 THEN IF 2 weeks pay > 700 THEN set bonus to 700 ELSE 10

Programming Techniques & Flowchart set bonus to 2 weeks pay END IF ELSE set Bonus to 1.5 weeks pay END IF END IF IF year greater then 10 THEN Add 100 to bonus ELSE IF years less than 2 THEN cut bonus to half ELSE bonus stays the same END IF Print employee number & bonus This example illustrates the policy of a bank or a financial institution for giving a loan to an individual. Input mortgage amount IF amount < 25,000 THEN down payment = 3% of amount ELSE payment1 = 3% of 25,000 payment2 = 5% of (amount - 25,000) down payment = payment1 + payment2 END IF print down payment

Example
The following program in pseudocode form illustrates the policy of a company to give the commission to a sales person. Input sales IF sales < 500 THEN Commission = 2% of sales. ELSE IF sales < 5000 THEN Commission = 5% of sales ELSE Commission = 10% of sales END IF Print Commission

11

Programming Techniques & Flowchart

Advantages of Pseudocodes
Pseudocode has three main advantages: (a) Converting a pseudocode to a programming language is much more easier as compared to converting a flowchart or a decision table. (b) As compared to a flowchart, it is easier to modify the pseudocode of a program logic when program modifications are necessary. (c) Writing of pseudocode involves much less time and effort than drawing an equivalent flowchart. Pseudocode is easier to write than an actual programming language because it has only a few rules to follow, allowing the programmer to concentrate on the logic of the program.

Loop
Many jobs that are required to be done with the help of a computer are repetitive in nature. For example, calculation of salary of different workers in a factory is given by the (No. of hours worked) (wage rate). This calculation will be performed by an accountant for each worker every month. Such types of repetitive calculations can easily be done using a program that has a loop built into the solution of the problem .

What is a Loop ? A loop is defined as a block of processing steps repeated a certain number of times. An endless loop repeats infinitely and is always the result of an error. Terms Used in Looping:
Initialisation

It is the preparation required before entering a loop.


Incrementation

It is the numerical value added to the variable each time one goes round the loop.
The Loop Variable It is an active variable in a loop. Loop Exit Test There must be some method of leaving the loop after it has revolved the requisite

number of times.

Counting
Counting is an essential technique used in the problem solving process. It is mainly for repeating a procedure for a certain number of times or to count the occurrences of specific events or to generate a sequence of numbers for computational use. computer cannot count by itself, the user has to send the necessary instruction to do so.

Procedure for Problem Solving


Though every problem is an entity in itself, there are a few basic steps that should be understood and followed for effectively solving a problem using computer techniques. On following these steps, your problem solving capacity will improve. There are basically six steps in solving a problem. These are: 1. First, spend sometime in understanding the problem thoroughly. In this, you are not required to use a computer. Instead try to answer and solve the problem manually. 2. Now construct a list of variables that are needed to solve the problem. 3. Once you have completed step 2, you have to decide the layout for the output format. 4. Next, select the programming technique which is best suited to solve the problem and carryout the coding. 12

Programming Techniques & Flowchart 5. Test your program. Choose some test data so that each part of the program is checked for correctness. 6. Finally use the data validation program to guard against processing of wrong data.

Algorithm
The sequence of instructions for solving a particular problem is known as algorithm. Constructing an algorithm to solve a given problem requires a high degree of ingenuity. But once an algorithm is laid out, it can be used by a person who does not even know its purpose.

Characteristics of the Instructions in an Algorithm


The sequence of instructions must possess the following characteristics for qualifying as an algorithm : 1. Each and every instruction should be precise and unambiguous. 2. Each instruction should be such that it can be performed in a finite time. 3. One or more instructions should not be repeated indefinitely .This ensures that the algorithm will ultimately terminate. 4. After performing the instructions, that is, after the algorithm terminates, the desired results must be obtained.

Example
Let us consider another case where there are 50 students in a class who have appeared in their final examination. Their marksheets have been given. We are required to write an algorithm to calculate and print the total number of students who have passed in first division.

Solution
Step 1 Initialize TOTAL, FIRST_DIVISION and TOTAL MARKSHEETS_ CHECKED to zero. Step 2 Take the marksheet of the first student. Step 3 Check the division column of the marksheet to see if it is FIRST_DIVISION : if no, go to step 5. Step 4 Add 1 to TOTAL FIRST_DIVISION. Step 5 Add 1 to TOTAL MARKSHEETS_CHECKED. Step 6 Is TOTAL MARKSHEETS_CHECKED = 50? : if no, go to step 2. Step 7: Print TOTAL FIRST_DIVISION. Step 8: Stop.

13

Programming Techniques & Flowchart

Programming
If we compare structured programming to building a house, then structured programming is to build the house after the plan for the house is drawn. We would like to build a house using standard bricks, window-frames, doors etc., so that the house so constructed is appealing and comfortable. Thus, structured programming also contains three standard control structure. These are: Simple Sequence Simple Selection Simple Repetition The above three structures are simple to use as they can be recognised easily. They have one entry and one exit point and they are free from any programming language used for coding a solution of a problem.

Modular (Top-Down) Program Design


You must have realised that an effective approach to follow in the programming analysis stage of program development is to break down a large problem into a series of smaller and more understandable tasks. Thus, the programmer may first develop a main-control program that is used to outline the major segments, or modules, that are in turn needed to solve a problem. The main-control program specifies the order in which each subordinate module in the program will be processed. The programming analysis stage continues until every module has been reduced to the point that the programmer is confident that he or she has a solution method that will solve the task. When this modular (top-down) program design practice is used, an instruction in the main-control program branches control to a subordinate program routine (subroutine) or module.When the specific processing operation performed by the module is completed, another branch instruction may transfer program control to another module or return it to the main-control program. Thus, the modules or subroutines are really programs within a program. Each module typically has only one entry point and only one exit point. Some of the advantages of using this construction option are: (a) Complex programs may be divided into simpler and more manageable elements. (b) Simultaneous coding of modules by several programmers is possible. (c) A library of modules may be created, and these modules may be used in other programs as needed. (d) The location of program errors may be more easily traced to a particular module, and thus debugging and maintenance may be simplified. (e) Effective use can be made of tested subroutines prepared by software suppliers and furnished to their customers.

Pseudo Code Revisited


Pseudo code is an abbreviated form of expression that makes use of both the English language and certain programming language control words such as IF - THEN - ELSE & END - IF. The user describes in plain English language, the sequence of steps necessary to solve a particular problem. Sentences are generally written one per line. Indentation is used in the IF statement to outline which actions are to be taken if a condition is true and which are to be taken if the condition is not true.

14

Programming Techniques & Flowchart

Differences between PseudoCode and Flowchart


Pseudocode differs from the flowchart in the following ways: (a) Pseudocode is self-explanatory and does not require a separate documentation. This is so because it is written in plain English language. (b) Pseudocode has a structure similar to that of a programme written in BASIC or PASCAL language. But flowcharts have a tendency to extend flow lines in all directions and thus their paths of instructions do not parallel the final BASIC code. The following example of flowcharts and their equivalent pseudocode.

Example
Figure illustrates the multichoice selection or the CASE structure. In CASE structure selection can be made out of many choices using the word CASE. CASE structures are used in evaluating situations that can have a number of different results. CASE in this sense refers to a refinement of a basic IF-THEN-ELSE type of conditional structure (IF A is true, then do B), but a CASE structure functions more like a series of nested IFS (IF A, then do this; if B then do that...). For example if you have three types of drinks, then the flowchart shown in Figure gives a method of selecting any one of them. This can also be written in pseudocode form using CASE statement. An equivalent Pseudocode of the flowchart shown in Figure is given below:

15

Programming Techniques & Flowchart

CASE drink Lemon Pour juice into glass from Lemon jug. Orange Pour juice into glass from Orange jug. Pineapple Pour juice into glass from Pineapple jug. END CASE REPEAT - UNTIL LOOP Structure

Programming Methods
Top-down Design
Top-down design is the technique of breaking down a problem into the major tasks to be performed. Each of these tasks is then further broken down into separate subtasks, and so on until each subtask is sufficiently simple to be written as a self contained module or procedure. The program then consists of a series of simple modules.In top-down design we initially describe the problem we are working on at the highest, most general level. The description of the problem at this level will usually be concerned with what must be done not how it must be done. The description will be in terms of complex, higher-level operations. We must take all of the operations at this level and individually break them down into simpler steps that begin to describe how to accomplish the tasks. If these simple steps can be represented as acceptable algorithmic step, we need not refine them any further. If not, we refine each of these second level operations individually into still simpler steps. This stepwise refinement continues until each of the original top-level operations has been described in terms of acceptable shortest (primitive) statements.

Advantages of Top-down approach (a) It allows a programmer to remain "on top of" a problem and view the developing solution in context. The solution always proceeds from the highest levels down. With other techniques we may find ourselves bogged down with very lowlevel decisions at a very early stage. It will be difficult to make these decisions if it is not clear how they may affect the remainder of the problem. (b) It is a very good way to delay decisions on problems whose solution may not be readily apparent. At each stage in the development, the individual operation will be refined into a number of more elementary steps. If we are not sure how to proceed with step 1 we can still work on step 2. (c) By dividing the problem into a number of subproblems, we have made it easier to share problem development. For example, one person may solve part 2 of the problem and the other person may solve part one of the problem. (d) Since debugging time grows so quickly, it will be to our advantage to debug a large program as a number of smaller units rather than one big chunk. The top-down development process specifies a solution in terms of a group of smaller, individual subtasks. These subtasks thus become the ideal unit of program testing and debugging. By testing the program in small pieces, we greatly simplify the debugging process. In addition, we will have the satisfaction of knowing that everything we have coded so far is correct. When we add a new piece of code, say "p" to the overall program "P", and an error condition occurs, we can definitely state that the error must either be in "p" itself or in the interface between "p" or "P", because "P" has been previously checked and certified.

16

Programming Techniques & Flowchart (e) Another advantage of the top-down development process is that it becomes an ideal structure for managing the implementation of a computer program using teams of programmers. A senior programmer can be responsible for the design of a high-level task and the decomposition into subtasks. Each of those subtasks can then be "farmed out" to a more junior programmer who work under the direction of the senior staff. Since almost all software projects are done by teams of two or more programmers, this topdown characteristic is very important. In summary, top-down programming is a program design technique that analyses a high-level problem in terms of more elementary subtasks. Through the technique of stepwise refinement we then expand and define each of these separate subtasks until the problem is solved. Each subtask is tested and verified before it is expanded further. The advantages of this technique are: (a) Increased intellectual manageability and comprehension. (b) Abstraction of unnecessary lower-level detail. (c) Delayed decisions on algorithms and data structures until they are actually needed. (d) Reduced debugging time.

Bottom-up Design and Implementation


When faced with a large and complex problem, it may be difficult to see how the whole thing can be done. It may be easier to attack parts of the problem individually, taking the easier aspects first and thereby gaining the insight and experience to tackle the more difficult tasks, and finally to try and bolt them all together to form the complete solution. This is called a bottom-up approach. It suffers from the disadvantage that the parts of the program may not fit together very easily. There may be a lack of consistency between modules, and considerable re-programming may have to be done.

Modular Design and Programming


In industry and commerce, the problems that are to be solved with the help of computers need thousands or even more number of lines of code. The importance of splitting up the problem into a series of selfcontained modules then becomes obvious. A module should not exceed about 100 or so lines and should preferably be short enough to fit on a single page.

Advantages of modular design (a) Some modules will be standard procedures used again and again in different programs or parts of the same program. (b) Since module is small, it is simpler to understand it as a unit of code. It is therefore easier to test and debug, especially if its purpose is clearly defined and documented. (c) Program maintenance becomes easier because the affected modules can be quickly identified and changed. (d) In a very large project, several programmers may be working on a single program. Using a modular approach, each programmer can be given a specific set of modules to work on. This enables the whole program to be finished sooner. (e) More experienced programmers can be given the more complex modules to write, and the junior programmers can work on the simpler modules. (f) Modules can be tested independently, thereby shortening the time taken to get the whole program working. (g) If a programmer leaves part way through a project, it is easier for someone else to take over a set of self contained modules. (h) A large project becomes easier to monitor and control.

17

Programming Techniques & Flowchart

Structured Programming
In the 1960s in the USA a number of surveys confirmed what most data processing managers had believed for a long time that there is a substantial variation in programmer abilities and that too much time is spent on debugging programs and on maintenance activities. The surveys generated much controversy, but the effect they had was dramatic. Suddenly everyone became concerned with the programmers productivity and the way in which he actually programmed. In 1965, Professor Dijkstra of Eindhoven University in Holland presented a paper at the IFIP Congress in New York suggesting that the GOTO statement should be eliminated from programming languages altogether,since a programs quality was inversely proportional to the number of GOTO statements in it. In the following year, Bohm, and Jacopini showed that any program with single entry and exit points could be expressed in terms of three basic constructs: (a) Sequence or carrying out a process (b) Iteration or looping (c) Selection or decision taking This was the beginning of structured programming. This dramatically improved the quality of programming and of programmer productivity. There is no doubt that structured programming has been successful, but it does not solve all our problems. Poorly constructed system designs can still negate the benefits provided by structured programming. It was not surprising then that similar principles were applied to the tasks of analysis and design and a full range of structured methods came into being.

Why Structured Programming ?


In computer programming, the spaghetti code is the method of coding that confuses the program flow because of the excessive use of GOTO or jump statements. Hence one of the major improvements has been the shift from spaghetti code to TOP Down modular design and structured programming methods. The reason for the evolution to structured programming is the need for well organized programs that are ultimately easier to : (a) Design (b) Read and understand (c) Modify (d) Test and Debug (e) Combine with other programs Writing programs that are clear for any programmer to read and understand is a very important consideration, particularly if there is a change in programming personnel.

Characteristics of Structured Programs


In general terms, structured programming is the development of computer programs that are well organized. Here is a list of characteristics of structured programs. (a) The programs are based on top-down modular design. In other words, the problem at hand is analysed or broken down into major components, each of which is again broken down if necessary. Therefore, the process involves working from the most general down to the most specific. (b) Each module has one entry point and one exit point. The GOTO statement is never used to jump from one module in the program to another. (c) A rule of thumb is that the modules should not be more than one half page long. If they are longer than this, they should preferably be split into two or more submodules. (d) Two-way decision statements are based on IF..THEN, IF..THEN..ELSE, and nested IF statements. (e) Loops are not custom designed with the use of the GO TO statement, but are based on the consistent use of WHILE..WEND and FOR..NEXT. In WHILE..WEND, the loop is based on the truth of a

18

Programming Techniques & Flowchart condition, and in FOR..NEXT, on a counting process, and the number of repetitions that can easily be predicted.

Types of Program Errors


Once a program has been typed in, different types of errors may show up. These include: (a) Syntax/semantic errors (b) Logic errors (c) Runtime errors

Syntax/semantic errors Syntax is a set of rules governing the structure of and relationship between symbols, words and phrases in a language statement. A syntax error occurs when a program cannot understand the command that has been entered. Logic errors Logic refers to a sequence of operations performed by a software or hardware. Software logic or program logic is the sequence of instructions in a program. Logic errors are the errors that have been entered in the instructions created because of the mistake made by a programmer. Suppose you wanted to do the sum of A and B and put the result in the variable C. This is accomplished by typing: C :B But while typing, the programmer has typed the following expression: C :B Such a program will run properly but will give erratic result because the value of C will not be the sum of A and B but it will be the difference of A and B which is quite different. Such errors can only be detected with the help of test data that will give the input values of A and B and the resultant value that should come out of the program as a result of the operation performed on A and B. If the result given by the computer and the result calculated manually with the help of a calculator are the same, then you can say that there is no logical error. Otherwise a logic error may be present in the program. Runtime error Runtime errors occur when a program is run on the computer and the results are not achieved due to some misinterpretation of a particular instruction. This could be some thing like dividing a number by zero which results in a very large value of quotient. It may also be any other instruction which a computer is not able to understand. To overcome this problem, there is a built-in error detector in the language nterpreter or compiler which will give the message and that will reflect the reason for the run time error.

Program Testing
It is the job of the programer to test, as far as possible, that all parts of the program work correctly. It should be realised that complete testing is not possible except in the case of the most trivial program; one can never be completely certain that all errors have been removed, but sufficient tests can be performed to give a reasonable measure of confidence in the program. The situation is analogous to testing children on multiplication tables; once a child has answered a certain number of multiplication table tests correctly, at some point or other the teacher assumes that all other tests will be answered correctly and testing ceases.

19

Programming Techniques & Flowchart

Designing a Test Plan


Good testing requires the following: (a) A thorough knowledge and understanding of what the program is supposed to do. (b) Plan out in advance what ought to be tested. (c) To work out expected results for each of the test cases. (d) Writing out the test plan. Since we cannot test everything, each test must be carefully planned to provide more information about the program. A major benefit of preparing a comprehensive test plan with expected results is that it forces the programmer to think carefully about the program and often errors are spotted even before running the test.

Methods of Testing
The objectives of testing can be stated in two basic questions: (a) Does the logic work properly? This means, answering the following: (i) Does the program work as intended? (ii) Can it be made to crash? (b) Is the necessary logic present? This means answering the following: (i) Are there any functions missing? (ii) Does the program or module do everything specified? There are two different ways of testing. These are: (a) Functional testing (b) Logical or structural testing

Functional Testing
Functional testing is carried out independently of the code used in the program. It involves looking at the program specification and creating a set of test data that covers all the inputs and outputs and program functions. This type of testing is also known as "black box testing". For example, to test the program that calculates check digits we could draw up the following test plan:
Table 1.1 Test Plan for Check Digits Serial Test for numer- Purpose Expected Actual Number ical data result result 1 Enter 1234 Test validity of data 1234 Digits 3 2 Enter 8 digits Test extreme case are printed 3 as it is 3 Enter 123W Testing invalid data Data not Error accepted

From Table 1.1 we will be able to make out whether the software is testing the data correctly or not. If it is not testing properly, then we may have to correct the program.

Logical ( Structural) Testing


Logical testing (white box testing) is dependent on the code logic, and derives from the program structure rather than its function. In other words, we study the program code and try to test each possible path in the program at least once. The problem with logical testing is that it will not detect the missing functions.

20

Programming Techniques & Flowchart One method of devising a test plan is to start with a set of functional test cases, and then add additional tests to exercise each statement in the program at least once, making sure that each decision is tested for all outcomes.

Debugging Aids
Once the presence of logic errors has been detected with the help of test runs, there are various ways of finding where the error or errors lie. These include the following: (a) A dry run through the program, building up a trace table while manually following the program steps. (b) The inclusion of extra "write" statements to examine the contents of variables at various points in the program. (c) The printouts of file contents before and after processing. (d) An On line debugger which allows a programmer to step through the program line by line and examine the values of variables at any point in the program.

21

You might also like