Вы находитесь на странице: 1из 65

Structured COBOL Programming

Nancy Stern Hofstra University Robert A. Stern

Copyright @ 2000 John Wiley & Sons, In. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express permission of the copyright owner is unlawful. Request for further information should be addressed to the permissions Department , John Wily & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.

Nassau Community College


PowerPoint Presentation: Richard H. Baum, Ph.D.
DeVry Institute of Technology

9th Edition

CHAPTER 5 DESIGNING and DEBUGGING BATCH and INTERACTIVE COBOL PROGRAMS

Structured COBOL Programming, Stern & Stern, 9th Edition

OBJECTIVES
To Familiarize You With:
1. The way structured programs should be designed. 2. Pseudocode and flowcharts as planning tools used to map out the logic in a structured program. 3. Hierarchy or structure charts as planning tools used to illustrate the relationships among modules in a top-down program.
Structured COBOL Programming, Stern & Stern, 9th Edition

OBJECTIVES
4. The logical control structures of sequence, selection, iteration, and case. 5. Techniques used to make programs easier to code, debug, maintain, and modify. 6. Interactive processing.

Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
What Makes a Well-Designed Program? Designing Programs before Coding Them

Illustrating Logical Control Structures Using Pseudocode and Flowcharts


Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
Hierarchy Charts for Top-Down Programming Naming Modules or Paragraphs

Modularizing Programs

Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
A Review of Two Coding Guidelines An Introduction to Interactive Processing Debugging Programs

Structured COBOL Programming, Stern & Stern, 9th Edition

WHAT MAKES A WELLDESIGNED PROGRAM?

Structured COBOL Programming, Stern & Stern, 9th Edition

Program Logic Should Be Mapped Out Using a Planning Tool


The term program design means the development of a program so that its elements fit together logically and in an integrated way.

If programs are systematically planned before they are coded, they will be better designed.
Planning tools such as pseudocode, flowcharts, and hierarchy charts help map out program logic.
Structured COBOL Programming, Stern & Stern, 9th Edition

PROGRAMS SHOULD BE STRUCTURED Well-designed, structured programs are those that have a series of logical constructs.
Thus, the order in which these instructions are executed is standardized.

Structured COBOL Programming, Stern & Stern, 9th Edition

PROGRAMS SHOULD BE STRUCTURED


Each set of instructions that performs a specific function is defined in a module or program segment.
A module is also called a routine or, in COBOL, a paragraph. Each module is executed in its entirety from specific places in a program.

In COBOL, modules are executed using a PERFORM statement.


Structured COBOL Programming, Stern & Stern, 9th Edition

PROGRAMS SHOULD USE A TOPDOWN APPROACH


The modules are coded in a hierarchical order, with main modules written first followed by secondary modules that include the detailed code.
The coding of modules in a hierarchical manner is called top-down programming.
This top-down approach is some times called stepwise refinement.

Structured COBOL Programming, Stern & Stern, 9th Edition

DESIGNING PROGRAMS BEFORE CODING THEM

Structured COBOL Programming, Stern & Stern, 9th Edition

HOW PROGRAMS ARE DESIGNED


In addition to learning syntax programmers must learn how to design a program so that it functions effectively as an integrated whole.
The techniques for developing welldesigned programs are applicable to all languages.
Once you know how to design programs efficiently and effectively, you need only learn the syntax rules of a specific language to implement these design elements.
Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS


Two tools for planning the logic to be used in a program are pseudocode and flowcharts.
These should be used before the program is coded.

Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS

Pseudocode
A pseudocode is a set of statements that specifies the instructions and logical control structures that will be used in a program.

Flowcharts
A flowchart is a diagram or pictorial representation of the instructions and logical control structures that will be used in a program.
Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS

Pseudocode
Pseudocode has been designed specifically for representing the logic in a structured program.

The pseudocode for a program that reads in two numbers, adds them, and prints the total is as follows:
START
READ AMT1, AMT2 COMPUTE TOTAL = AMT1 + AMT2 WRITE TOTAL

STOP
Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS

Flowcharts
The following symbols are the ones most frequently used.
Symbol Name
Input/Output

Use
Used for all I/O operations Used for all arithmetic and data transfer operations.

Processing

Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS

Flowcharts
The following symbols are the ones most frequently used.
Symbol Name Decision Use Used to test for a condition.

Terminal

Used to indicate the beginning and end of a program or module.

Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS

Flow Charts (continued)


Symbol Name Use

Connector

Used to indicate the point at which a transfer of control operation occurs.


Used to indicate the name of a module to be executed.

Predefined Process
Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS


FLOWCHARTING CONVENTIONS
1. Each symbol denotes a type of operation. 2. A note is written inside each symbol to indicate the specific function to be performed. 3. The symbols are connected by flowlines. 4. Flowcharts are drawn and read from top to bottom unless a specific condition is met that alters the path. 5. A sequence of operations is performed until a terminal symbol designates the sequence's end or the end of the program. 6. Sometimes several steps or statements are combined in a single processing symbol for ease of reading.
Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS


Flow Chart
Example:
START
READ AMT1, AMT2
COMPUTE TOTAL = AMT1 + AMT2

WRITE TOTAL

STOP
Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE AND FLOWCHARTS


Flow Chart Example

This sequence of instructions is called a module.


The flowchart is read from top to bottom. Since there is no need to repeat instructions or to test for any conditions, this simple flowchart indicates that two numbers will be read, added together, and the sum printed.
Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


Structured programs use logical control structures to specify the order in which instructions are executed.
These structures are the same for all languages.

LOGICAL CONTROL STRUCTURES


1. Sequence.

2. Selection.
3. Iteration. 4. Case Structure.
Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


SEQUENCE A sequence is instructions executed in the order they appear.
The following pseudocode example represents a sequence.
The ellipses mean that each statement has other components.

We use a sequence to depict the logic when data is to be processed in a stepby-step order.
Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


SEQUENCE: A PSEUDOCODE EXAMPLE

START (OR ENTRY) . . . ADD ... WRITE STOP (OR RETURN)


Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


SEQUENCE: Beginning and Ending Modules
All modules or sequences in a pseudocode and a program flowchart should be clearly delineated. The pseudocode could use START and STOP as code words to delimit a sequence or module, particularly the main module. Each instruction in a structured program is executed in sequence unless another logical control structure is specified.
Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


Selection

Selection is a logical control construct that executes instructions depending on the existence of a condition.
It is sometimes called an IF-THEN-ELSE logical control structure.

If the condition is true (or exists), the statement or statements following the THEN statement are executed.
Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


Selection

If the condition does not exist (or is false), we execute the statement or statements following the ELSE statement.
Later we will see that a COBOL 85 program can look just like pseudocode.

Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


Iteration

The structure that makes use of the PERFORM UNTIL is called iteration. Iteration or looping is a logical control structure used for specifying the repeated execution of a series of steps.
An example follows.

Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


Iteration: a COBOL 85 example

PERFORM UNTIL
PERFORM
UNTIL ARE-THERE-MORE-RECORDS = 'NO

. .

.
END-PERFORM
Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


Case Structure

The case structure is a special control structure used when there are numerous paths depending on the contents of a given field.
It is used when we wish to perform one of several possible procedures depending on some condition or value.

Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


Case Structure The case structure is an important construct for processing menus interactively and for helping to minimize errors through data validation.
The procedure or module to be executed depends on the actual value of the entry made by the user.

Structured COBOL Programming, Stern & Stern, 9th Edition

THE FOUR LOGICAL CONTROL STRUCTURES


Case Structure: A Pseudocode Example
EVALUATE UPDATE-CODE
WHEN 1
PERFORM UPDATE

WHEN 2
PERFORM NEW-HIRE

WHEN OTHER
PERFORM ERROR

END-EVALUATE
Structured COBOL Programming, Stern & Stern, 9th Edition

PSEUDOCODE RULES
1. Pseudocode is written and read from top to bottom.

2. The logical control structure is defined with the use of key terms:
PERFORM . . . END-PERFORM IF-THEN-ELSE . . . END-IF CASE . . . END-CASE.

3. The operations to be executed within a PERFORM, IF-THEN-ELSE, or CASE (EVALUATE) can be coded in-line or in a separate module.
Structured COBOL Programming, Stern & Stern, 9th Edition

FLOWCHART RULES
1. A flowchart is drawn and read from top to bottom unless a specific condition alters the path. 2. The symbol itself denotes the type of operation such as input/output or processing. 3. An explanatory note within the symbol describes the specific operation to be performed.
Structured COBOL Programming, Stern & Stern, 9th Edition

HIERARCHY CHARTS FOR TOP-DOWN PROGRAMMING

Structured COBOL Programming, Stern & Stern, 9th Edition

HIERARCHY CHARTS
A hierarchy or structure chart is a graphic method for segmenting a program into modules.
Its main purpose is to provide a visual overview of the modules in a program.

You will need to plan the logic in two ways:


(1) with pseudocode (or a flowchart) to illustrate the logical structure, and (2) with a hierarchy chart to illustrate how modules should relate to one another.
Structured COBOL Programming, Stern & Stern, 9th Edition

HIERARCHY CHARTS
1. A hierarchy chart represents program modules as rectangular boxes and illustrates the interrelationships among these modules with the use of connected lines. 2. A module is a well-defined program segment that performs a specific function.
Structured COBOL Programming, Stern & Stern, 9th Edition

EXAMPLE OF A HIERARCHY CHART


A

Structured COBOL Programming, Stern & Stern, 9th Edition

HIERARCHY CHART EXPLANATION (1 of 2)


The letters A through H represent paragraph-names that are executed with the use of a PERFORM as follows: A.

PERFORM B.
PERFORM C.

B.
PERFORM D. PERFORM E.
Structured COBOL Programming, Stern & Stern, 9th Edition

HIERARCHY CHART EXPLANATION (2 of 2)


C.
PERFORM F.
PERFORM G. PERFORM H.

The hierarchy chart only illustrates modules executed from other modules.
Each block or box in a hierarchy chart represents a module.
Structured COBOL Programming, Stern & Stern, 9th Edition

HIERARCHY CHARTS
ADVANTAGES OF A HIERARCHY OR STRUCTURE CHART

1. It helps programmers, systems analysts, and users see how modules interrelate.
2. It helps programmers debug and modify programs. 3. It helps programming managers assess the efficiency of programs.
Structured COBOL Programming, Stern & Stern, 9th Edition

NAMING MODULES OR PARAGRAPHS


A module or set of related instructions is equivalent to a paragraph. Use a standard method for naming paragraphs in all programs. Choose meaningful names.

Structured COBOL Programming, Stern & Stern, 9th Edition

MODULARIZING PROGRAMS
We have seen that top-down programs are written with main units or modules planned and coded first, followed by more detailed ones.

Structure or hierarchy charts illustrate the relationships among these modules.


Statements that together achieve a given task should be coded as a module.

Structured COBOL Programming, Stern & Stern, 9th Edition

A REVIEW OF TWO CODING GUIDELINES

Structured COBOL Programming, Stern & Stern, 9th Edition

CODE EACH CLAUSE ON A SEPARATE LINE


Coding one clause per line makes programs easier to read and debug. Words and clauses can be separated with any number of blank spaces. Having only one clause on each line helps to isolate errors.

Structured COBOL Programming, Stern & Stern, 9th Edition

INDENT CLAUSES WITHIN A STATEMENT


Indentation makes programs easier to read.
In general, we indent four spaces on each line.

Indentation is used to clarify the logic.


Note, however, that indentation does not affect the program logic at all.
Structured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING TIP
COBOL 85 programmers should always use scope terminators with the READ and IF statements.
When scope terminators are coded, periods are not used to end statements except for the last statement in a paragraph.

Scope terminators ensure that all clauses within a statement will be associated with the appropriate instruction, thereby minimizing logic errors.
Structured COBOL Programming, Stern & Stern, 9th Edition

AN INTRODUCTION TO INTERACTIVE PROCESSING

Structured COBOL Programming, Stern & Stern, 9th Edition

INTERACTIVE PROCESSING
COBOL was originally developed to process files of data and is still widely used for that purpose. Many organizations, however, are using COBOL for interactive processing where- the user enters data using a keyboard on a PC or a terminal, and

output is displayed on the monitor at the users desk.


Structured COBOL Programming, Stern & Stern, 9th Edition

INTERACTIVE PROCESSING
We use the ACCEPT verb for entering input from a keyboard and the DISPLAY verb for displaying output on a screen.
The instruction ACCEPT identifier enables the user to enter input data directly from a keyboard rather than from a disk file. The identifier is likely to be a WORKINGSTORAGE entry.

When input is entered using the ACCEPT verb, there is no need to establish a file.
Structured COBOL Programming, Stern & Stern, 9th Edition

INTERACTIVE PROCESSING
To enter as input a DISCOUNT-AMT, for example, you can code: ACCEPT DISCOUNT-AMT
The format is determined by the PIC clause for DISCOUNT-AMT.

WORKING-STORAGE SECTION. 01 DISCOUNT-AMT PIC 9(3).


Using a keyboard, the use would enter three integers into DISCOUNT- AMT.
Structured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING PROGRAMS
After you design a program you are ready to code it. Programs must be fully tested to ensure that there are no errors. The process of eliminating errors from a program is called debugging.

Structured COBOL Programming, Stern & Stern, 9th Edition

SYNTAX ERRORS
After a program has been planned and coded, it is keyed into a computer. Then it is ready to be compiled or translated into machine language.
During this translation/compilation process, the compiler will list any violations in programming rules that may have occurred.

These rule violations are called syntax errors; they must be corrected before the program can be executed.
Structured COBOL Programming, Stern & Stern, 9th Edition

SYNTAX ERRORS
When diagnostics appear in a source listing (either at the end or right after the line in question), they typically have the following format: Line No. Error Code Error Message

Structured COBOL Programming, Stern & Stern, 9th Edition

PC COMPILERS
Micro Focus and RM/COBOL-85 have compiler-generated messages similar to those just discussed.
Micro Focus will highlight the word and display the error messages. On-line HELP is available to further explain the meaning of the messages. These error codes are documented in its On-LineReference Guide.
Structured COBOL Programming, Stern & Stern, 9th Edition

LOGIC ERRORS
Syntax errors are detected by the compiler and, except for warnings, they should all be corrected before you run the program. However, even after a program has been compiled so that it has no syntax errors it is not yet fully debugged.

The program must be executed with test data to ensure that there are no logic errors.
Structured COBOL Programming, Stern & Stern, 9th Edition

LOGIC ERRORS
Some logic errors result in a program interrupt.
These are called run-time errors, and must be corrected before execution can continue.

Other logic errors result in erroneous output.


These will be detected only if the test data is complete and the program is carefully checked by the programmer.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SLIDES END HERE


CHAPTER SUMMARY COMES NEXT

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
I. Program Design A. Logical Control Structures
The logical control structures are as follows:
1. Sequence 2. IF-THEN-ELSE or Selection

3. Iteration Using a PERFORM UNTIL . . . END-PERFORM loop


4. Case Structure
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
B. Program Planning Tools
1. To structure a program, use pseudocode or a flowchart. 2. To illustrate the top-down approach showing how modules interrelate, use a hierarchy chart.

C. Naming Modules
Use descriptive names along with numeric prefixes that help locate the paragraphs quickly (e.g., 200-PRINT-HEADING, 500-PRINT-FINAL- TOTAL).
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
D. A Well-Designed Program Uses: 1. Structured programming techniques.

2. A modularized organization.
3. A top-down approach:
Code main modules first, followed by minor ones.

4. Meaningful names for fields and paragraphs. 5. One clause per line and indented clauses within a sentence.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
II. Interactive Processing
A. You can use ACCEPT to input data from a keyboard.

B. You can use DISPLAY to output information to a screen.

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
III. Debugging
A. Correct all syntax errors or rule violations that are listed by the compiler. B. Test your program carefully with test data that includes all possible values that the program might encounter during a normal production run.
Structured COBOL Programming, Stern & Stern, 9th Edition

Вам также может понравиться