ch05_97
-
Upload
vinod-malla -
Category
Documents
-
view
221 -
download
0
Transcript of ch05_97
-
8/4/2019 ch05_97
1/65
PowerPoint Presentation:
Richard H. Baum, Ph.D.DeVry Institute of Technology
9th Edition
Structured COBOLProgramming
Nancy Stern
Hofstra University
Robert A. Stern
Nassau Community
College
Copyright @ 2000 John Wiley & Sons, In. All rightsreserved. Reproduction or translation of this work beyond
that permitted in Section 117 of the 1976 United StatesCopyright Act without the express permission of thecopyright owner is unlawful. Request for further informationshould be addressed to the permissions Department , JohnWily & Sons, Inc. The purchaser may make back-up copiesfor 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 programsor from the use of the information contained herein.
-
8/4/2019 ch05_97
2/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER 5DESIGNING and DEBUGGINGBATCH and INTERACTIVECOBOL PROGRAMS
-
8/4/2019 ch05_97
3/65
Structured COBOL Programming, Stern & Stern, 9th Edition
OBJECTIVES
To Familiarize You With:
1. The way structured programsshould be designed.
2. Pseudocode and flowcharts asplanning tools used to map out thelogic in a structured program.
3. Hierarchy or structure charts asplanning tools used to illustrate therelationships among modules in atop-down program.
-
8/4/2019 ch05_97
4/65
Structured COBOL Programming, Stern & Stern, 9th Edition
OBJECTIVES
4. The logical control structures ofsequence, selection, iteration, andcase.
5. Techniques used to make programseasier to code, debug, maintain, andmodify.
6. Interactive processing.
-
8/4/2019 ch05_97
5/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
What Makes a Well-DesignedProgram?
Designing Programs beforeCoding Them
Illustrating Logical Control
Structures Using Pseudocodeand Flowcharts
-
8/4/2019 ch05_97
6/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
Hierarchy Charts for Top-DownProgramming
Naming Modules or Paragraphs
Modularizing Programs
-
8/4/2019 ch05_97
7/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
A Review of Two CodingGuidelines
An Introduction to InteractiveProcessing
Debugging Programs
-
8/4/2019 ch05_97
8/65
Structured COBOL Programming, Stern & Stern, 9th Edition
WHAT MAKES A WELL-DESIGNED PROGRAM?
-
8/4/2019 ch05_97
9/65
Structured COBOL Programming, Stern & Stern, 9th Edition
Program Logic Should BeMapped Out Using a PlanningTool The term program design means the
development of a program so that itselements fit together logically and in
an integrated way.
If programs are systematicallyplanned before they are coded, they
will be better designed.
Planning tools such as pseudocode,flowcharts, and hierarchy charts help
map out program logic.
-
8/4/2019 ch05_97
10/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PROGRAMS SHOULD BE STRUCTURED
Well-designed, structured programsare those that have a series oflogical constructs.
Thus, the order in which theseinstructions are executed isstandardized.
-
8/4/2019 ch05_97
11/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PROGRAMS SHOULD BE STRUCTURED
Each set of instructions that performs aspecific function is defined in a module orprogram 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 aPERFORM statement.
-
8/4/2019 ch05_97
12/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PROGRAMS SHOULD USE A TOP-DOWN APPROACH
The modules are coded in a hierarchicalorder, with main modules written firstfollowed 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 calledstepwise refinement.
-
8/4/2019 ch05_97
13/65
Structured COBOL Programming, Stern & Stern, 9th Edition
DESIGNING PROGRAMSBEFORE CODING THEM
-
8/4/2019 ch05_97
14/65
Structured COBOL Programming, Stern & Stern, 9th Edition
HOW PROGRAMS ARE DESIGNED
In addition to learning syntax
programmers must learn how todesign a program so that it functionseffectively as an integrated whole.
The techniques for developing well-designed programs are applicable toall languages.
Once you know how to design programsefficiently and effectively, you need onlylearn the syntax rules of a specificlanguage to implement these design
elements.
-
8/4/2019 ch05_97
15/65
Structured COBOL Programming, Stern & Stern, 9th Edition
Two tools for planning the logic to beused in a program are pseudocodeandflowcharts.
These should be used before the program iscoded.
PSEUDOCODE AND FLOWCHARTS
-
8/4/2019 ch05_97
16/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PseudocodeA 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 logicalcontrol structures that will be used in aprogram.
PSEUDOCODE AND FLOWCHARTS
-
8/4/2019 ch05_97
17/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PSEUDOCODE AND FLOWCHARTS
Pseudocode Pseudocode has been designed
specifically for representing the logic in astructured program.
The pseudocode for a program thatreads in two numbers, adds them,and prints the total is as follows:
START
READ AMT1, AMT2
COMPUTE TOTAL = AMT1 + AMT2
WRITE TOTAL
STOP
-
8/4/2019 ch05_97
18/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PSEUDOCODE AND FLOWCHARTS
Flowcharts
The following symbols are the ones mostfrequently used.
Symbol Name Use
Input/Output Usedfor all I/Ooperations
Processing Used for allarithmeticand datatransfer
operations.
-
8/4/2019 ch05_97
19/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PSEUDOCODE AND FLOWCHARTS
Flowcharts
The following symbols are the ones mostfrequently used.
Symbol Name Use
Decision Used totest for acondition.
Terminal Used toindicate the
beginning and end ofa program or module.
-
8/4/2019 ch05_97
20/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PSEUDOCODE AND FLOWCHARTS
Flow Charts (continued)Symbol Name Use
Connector Used to
indicate thepoint at which a
transfer of controloperation occurs.
Predefined Used toProcess indicate the
name of a module
to be executed.
-
8/4/2019 ch05_97
21/65
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 tobottom unless a specific condition is met thatalters the path.
5. A sequence of operations is performed until a
terminal symbol designates the sequence'send or the end of the program.
6. Sometimes several steps or statements arecombined in a single processing symbol forease of reading.
-
8/4/2019 ch05_97
22/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PSEUDOCODE AND FLOWCHARTS
Flow Chart
Example:
STARTREAD
AMT1,
AMT2COMPUTE
TOTAL =
AMT1 + AMT2
WRITETOTAL
STOP
-
8/4/2019 ch05_97
23/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PSEUDOCODE ANDFLOWCHARTS
Flow Chart Example
This sequence of instructions is called amodule.
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 twonumbers will be read, added together,and the sum printed.
THE FOUR LOGICAL CONTROL
-
8/4/2019 ch05_97
24/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
Structured programs use logical controlstructures to specify the order in whichinstructions are executed.
These structures are the same for alllanguages.
LOGICAL CONTROL STRUCTURES
1. Sequence.
2. Selection.
3. Iteration.
4. Case Structure.
THE FOUR LOGICAL CONTROL
-
8/4/2019 ch05_97
25/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
SEQUENCE A sequence is instructions executed in the
order they appear.
The following pseudocode examplerepresents a sequence.
The ellipses mean that each statement hasother components.
We use a sequence to depict the logicwhen data is to be processed in a step-by-step order.
-
8/4/2019 ch05_97
26/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
SEQUENCE: A PSEUDOCODE EXAMPLE
START (OR ENTRY)
.
.
.
ADD ...WRITE
STOP (OR RETURN)
-
8/4/2019 ch05_97
27/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
SEQUENCE: Beginning and Ending Modules
All modules or sequences in a pseudocodeand a program flowchart should be clearly
delineated.
The pseudocode could use START and STOPas code words to delimit a sequence ormodule, particularly the main module.
Each instruction in a structured program isexecuted in sequence unless another logicalcontrol structure is specified.
-
8/4/2019 ch05_97
28/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
Selection
Selection is a logical control constructthat 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), thestatement or statements following theTHEN statement are executed.
THE FOUR LOGICAL CONTROL
-
8/4/2019 ch05_97
29/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
Selection
If the condition does not exist (or isfalse), we execute the statement orstatements following the ELSE statement.
Later we will see that a COBOL 85program can look just like pseudocode.
THE FOUR LOGICAL CONTROL
-
8/4/2019 ch05_97
30/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
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.
THE FOUR LOGICAL CONTROL
-
8/4/2019 ch05_97
31/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
Iteration: a COBOL 85 examplePERFORM UNTIL
PERFORM
UNTILARE-THERE-MORE-RECORDS='NO
.
.
.
END-PERFORM
THE FOUR LOGICAL CONTROL
-
8/4/2019 ch05_97
32/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
Case Structure
The case structure is a special control
structure used when there are numerouspaths depending on the contents of agiven field.
It is used when we wish to perform one of
several possible procedures depending onsome condition or value.
THE FOUR LOGICAL CONTROL
-
8/4/2019 ch05_97
33/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
Case Structure The case structure is animportant construct for processing menusinteractively and for helping to minimize
errors through data validation. The procedure or module to be executed
depends on the actual value of the entrymade by the user.
THE FOUR LOGICAL CONTROL
-
8/4/2019 ch05_97
34/65
Structured COBOL Programming, Stern & Stern, 9th Edition
THE FOUR LOGICAL CONTROLSTRUCTURES
Case Structure: A Pseudocode Example
EVALUATE UPDATE-CODE
WHEN 1
PERFORM UPDATE
WHEN 2
PERFORM NEW-HIRE
WHEN OTHERPERFORM ERROR
END-EVALUATE
-
8/4/2019 ch05_97
35/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PSEUDOCODE RULES
1. Pseudocode is written and read from topto bottom.
2. The logical control structure is definedwith the use of key terms:
PERFORM . . . END-PERFORM
IF-THEN-ELSE . . . END-IF
CASE . . . END-CASE.
3. The operations to be executed within aPERFORM, IF-THEN-ELSE, or CASE(EVALUATE) can be coded in-line or in aseparate module.
-
8/4/2019 ch05_97
36/65
Structured COBOL Programming, Stern & Stern, 9th Edition
FLOWCHART RULES
1. A flowchart is drawn and read from topto bottom unless a specific conditionalters the path.
2. The symbol itself denotes the type ofoperation such as input/output orprocessing.
3. An explanatory note within the symboldescribes the specific operation to beperformed.
-
8/4/2019 ch05_97
37/65
Structured COBOL Programming, Stern & Stern, 9th Edition
HIERARCHY CHARTSFOR TOP-DOWN
PROGRAMMING
HIERARCHY CHARTS
-
8/4/2019 ch05_97
38/65
Structured COBOL Programming, Stern & Stern, 9th Edition
HIERARCHY CHARTS
A hierarchy or structure chart is a graphic
method for segmenting a program intomodules.
Its main purpose is to provide a visual
overview of the modules in a program.
You will need to plan the logic in twoways:
(1) with pseudocode (or a flowchart) toillustrate the logical structure, and
(2) with a hierarchy chart to illustrate howmodules should relate to one another.
-
8/4/2019 ch05_97
39/65
Structured COBOL Programming, Stern & Stern, 9th Edition
HIERARCHY CHARTS
1. A hierarchy chart represents programmodules as rectangular boxes andillustrates the interrelationships among
these modules with the use of connectedlines.
2. A module is a well-defined program
segment that performs a specificfunction.
-
8/4/2019 ch05_97
40/65
Structured COBOL Programming, Stern & Stern, 9th Edition
EXAMPLE OF A HIERARCHY CHART
A
B C
D EF G H
HIERARCHY CHART
-
8/4/2019 ch05_97
41/65
Structured COBOL Programming, Stern & Stern, 9th Edition
HIERARCHY CHARTEXPLANATION (1 of 2)
The letters A through H representparagraph-names that are executed withthe use of a PERFORM as follows:
A.PERFORM B.
PERFORM C.
B.
PERFORM D.
PERFORM E.
HIERARCHY CHART
-
8/4/2019 ch05_97
42/65
Structured COBOL Programming, Stern & Stern, 9th Edition
HIERARCHY CHARTEXPLANATION (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.
HIERARCHY CHARTS
-
8/4/2019 ch05_97
43/65
Structured COBOL Programming, Stern & Stern, 9th Edition
HIERARCHY CHARTS
ADVANTAGES OF A HIERARCHY OR
STRUCTURE CHART
1. It helps programmers, systemsanalysts, and users see how modulesinterrelate.
2. It helps programmers debug and
modify programs.
3. It helps programming managersassess the efficiency of programs.
NAMING MODULES OR
-
8/4/2019 ch05_97
44/65
Structured COBOL Programming, Stern & Stern, 9th Edition
NAMING MODULES ORPARAGRAPHS
A module or set of relatedinstructions is equivalent to aparagraph.
Use a standard method for namingparagraphs in all programs.
Choose meaningful names.
-
8/4/2019 ch05_97
45/65
Structured COBOL Programming, Stern & Stern, 9th Edition
MODULARIZING PROGRAMS
We have seen that top-down programsare written with main units or modulesplanned and coded first, followed by more
detailed ones.
Structure or hierarchy charts illustratethe relationships among these modules.
Statements that together achieve a given taskshould be coded as a module.
-
8/4/2019 ch05_97
46/65
Structured COBOL Programming, Stern & Stern, 9th Edition
A REVIEW OF TWOCODING GUIDELINES
CODE EACH CLAUSE ON A
-
8/4/2019 ch05_97
47/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CODE EACH CLAUSE ON ASEPARATE LINE
Coding one clause per line makesprograms easier to read and debug.
Words and clauses can be separated withany number of blank spaces.
Having only one clause on each line helps
to isolate errors.
INDENT CLAUSES WITHIN A
-
8/4/2019 ch05_97
48/65
Structured COBOL Programming, Stern & Stern, 9th Edition
INDENT CLAUSES WITHIN ASTATEMENT
Indentation makes programs easierto read.
In general, we indent four spaces oneach line.
Indentation is used to clarify the
logic.Note, however, that indentation does
not affect the program logic at all.
-
8/4/2019 ch05_97
49/65
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP
COBOL 85 programmers should alwaysuse scope terminators with the READ andIF statements.
When scope terminators are coded, periodsare not used to end statements except for thelast statement in a paragraph.
Scope terminators ensure that all clauseswithin a statement will be associatedwith the appropriate instruction, therebyminimizing logic errors.
-
8/4/2019 ch05_97
50/65
Structured COBOL Programming, Stern & Stern, 9th Edition
AN INTRODUCTION TOINTERACTIVE
PROCESSING
INTERACTIVE PROCESSING
-
8/4/2019 ch05_97
51/65
Structured COBOL Programming, Stern & Stern, 9th Edition
INTERACTIVE PROCESSING
COBOL was originally developed toprocess files of data and is still widelyused for that purpose.
Many organizations, however, are usingCOBOL for interactive processing where--
the user enters data using a keyboard on a PCor a terminal, and
output is displayed on the monitor at theusers desk.
-
8/4/2019 ch05_97
52/65
Structured COBOL Programming, Stern & Stern, 9th Edition
INTERACTIVE PROCESSING
We use theACCEPT verb for enteringinput from a keyboard and the DISPLAYverb for displaying output on a screen.
The instructionACCEPTidentifier enables the
user to enter input data directly from akeyboard rather than from a disk file.
The identifier is likely to be a WORKING-
STORAGE entry.
When input is entered using theACCEPTverb, there is no need to establish a file.
INTERACTIVE PROCESSING
-
8/4/2019 ch05_97
53/65
Structured COBOL Programming, Stern & Stern, 9th Edition
INTERACTIVE PROCESSING
To enter as input a DISCOUNT-AMT, forexample, you can code:
ACCEPTDISCOUNT-AMT
The format is determined by the PIC clausefor DISCOUNT-AMT.
WORKING-STORAGE SECTION.
01 DISCOUNT-AMT PIC 9(3).
Using a keyboard, the use would enter threeintegers into DISCOUNT- AMT.
-
8/4/2019 ch05_97
54/65
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING PROGRAMS
After you design a program you areready to code it.
Programs must be fully tested toensure that there are no errors.
The process of eliminating errorsfrom a program is called debugging.
SYNTAX ERRORS
-
8/4/2019 ch05_97
55/65
Structured COBOL Programming, Stern & Stern, 9th Edition
SYNTAX ERRORS
After a program has been planned andcoded, it is keyed into a computer. Then itis ready to be compiled or translated intomachine language.
During this translation/compilation process,the compiler will list any violations inprogramming rules that may have occurred.
These rule violations are called syntaxerrors; they must be corrected before theprogram can be executed.
-
8/4/2019 ch05_97
56/65
Structured COBOL Programming, Stern & Stern, 9th Edition
SYNTAX ERRORS
When diagnostics appear in a sourcelisting (either at the end or right after theline in question), they typically have thefollowing format:
Line No. Error Code Error Message
-
8/4/2019 ch05_97
57/65
Structured COBOL Programming, Stern & Stern, 9th Edition
PC COMPILERS
Micro Focus and RM/COBOL-85 havecompiler-generated messages similar tothose just discussed.
Micro Focus will highlight the word anddisplay the error messages.
On-line HELP is available to further explainthe meaning of the messages. These errorcodes are documented in its On-Line-Reference Guide.
-
8/4/2019 ch05_97
58/65
Structured COBOL Programming, Stern & Stern, 9th Edition
LOGIC ERRORS
Syntax errors are detected by thecompiler and, except for warnings, theyshould all be corrected before you run theprogram.
However, even after a program has beencompiled so that it has no syntax errors itis not yet fully debugged.
The program must be executed with testdata to ensure that there are no logicerrors.
-
8/4/2019 ch05_97
59/65
Structured COBOL Programming, Stern & Stern, 9th Edition
LOGIC ERRORS
Some logic errors result in a programinterrupt.
These are called run-time errors, and must be
corrected before execution can continue.
Other logic errors result in erroneousoutput.
These will be detected only if the test data iscomplete and the program is carefullychecked by the programmer.
C S S
-
8/4/2019 ch05_97
60/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SLIDES END HERE
CHAPTER SUMMARY COMES NEXT
CHAPTER SUMMARY
-
8/4/2019 ch05_97
61/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
I. Program Design
A. Logical Control Structures
The logical control structures are asfollows:
1. Sequence
2. IF-THEN-ELSE or Selection
3. Iteration Using a PERFORM UNTIL . . .END-PERFORM loop
4. Case Structure
CHAPTER SUMMARY
-
8/4/2019 ch05_97
62/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
B. Program Planning Tools
1. To structure a program, use pseudocode or aflowchart.
2. To illustrate the top-down approach showing
how modules interrelate, use a hierarchychart.
C. Naming Modules
Use descriptive names along with numericprefixes that help locate the paragraphsquickly (e.g., 200-PRINT-HEADING,
500-PRINT-FINAL- TOTAL).
CHAPTER SUMMARY
-
8/4/2019 ch05_97
63/65
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 andparagraphs.
5. One clause per line and indentedclauses within a sentence.
CHAPTER SUMMARY
-
8/4/2019 ch05_97
64/65
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
II. Interactive Processing
A. You can useACCEPT to input
data from a keyboard.B. You can use DISPLAYto output
information to a screen.
CHAPTER SUMMARY
-
8/4/2019 ch05_97
65/65
CHAPTER SUMMARY
III. Debugging
A. Correct all syntax errors or ruleviolations that are listed by thecompiler.
B. Test your program carefully with testdata that includes all possible values
that the program might encounterduring a normal production run.