Structured COBOL Programming, Stern & Stern, 9th edition
Chapter 1INTRODUCTION TO
STRUCTURED PROGRAM DESIGN IN COBOL
Structured COBOL Programming, Stern & Stern, 9th edition
CHAPTER 1 OBJECTIVESCHAPTER 1 OBJECTIVES To familiarize you with:
1. The reasons for COBOL’s popularity as a business-oriented language.2. Some general programming practices and techniques.3. A history of COBOL’s evolution and its current ANS version.4. An overview of how a COBOL program is structure.
Structured COBOL Programming, Stern & Stern, 9th edition
Types of Computer ProgramsTypes of Computer Programs
A program is a set of instructions that enable a computer to process data.
Software is the term used to describe all types of programs.
An application program converts input data to output information.
Structured COBOL Programming, Stern & Stern, 9th edition
IPO Chart / System Flowchart
IPO Chart / System Flowchart
Identifies the inputs and outputs of the program. Basic IPO model.
EMPLOYEE FILE
PRINT PAYROL DATA PAYROLL
REPORT
Structured COBOL Programming, Stern & Stern, 9th edition
Two types of computer programs:Two types of computer programs:
1. Operating Systems Programs Control the overall operations of the computer
2. Applications ProgramsWritten by Programmers or Software DevelopersPrograms that perform tasks required by usersA set of programs that fulfill a business requirement is an Information System
Structured COBOL Programming, Stern & Stern, 9th edition
Applications ProgramsApplications Programs
Interactive vs. Batch Processing Interactive applications typically accept input data
from a PC, workstation, or terminal. Batch Processing applications process large
volumes of input at periodic intervals.DailyWeeklyMonthly etc.
Structured COBOL Programming, Stern & Stern, 9th edition
Applications Programs Applications Programs
Applications programs that are written for a specific user are called customized programs.
Programs that are relatively standard are application packages that might be purchased.
Off The Shelf (OTS) may be less costly depending on the amount of customization needed
Structured COBOL Programming, Stern & Stern, 9th edition
All programs executed by the computer must be in machine language – the only language a computer understands
Binary 10110101 Must know memory address of variables etc.
Machine Language ProgramsMachine Language Programs
Structured COBOL Programming, Stern & Stern, 9th edition
Symbolic ProgramsSymbolic Programs Symbolic Programs use English-like or
symbolic instructions.These programs must be translated or compiled into machine language.– Symbols (Variable/Data names) represent memory
addressesCOBOL is a symbolic programming language used for commercial applications.As is C++, Visual Basic etc.
A compiler coverts symbolic language into machine code
Structured COBOL Programming, Stern & Stern, 9th edition
Program Development ProcessProgram Development Process
1. Determine Program Specifications2. Design Program Using Program Planning Tools3. Code and Enter Program4. Compile Program5. Test Program6. Document Program
Structured COBOL Programming, Stern & Stern, 9th edition
PROGRAM DEVELOPMENT PROCESSPROGRAM DEVELOPMENT PROCESS
1. Determine Program SpecificationsProgram Specifications consist of the following:–Record layout forms to describe the input and
output (pg 6)–Printer spacing charts for printed output (pg 7)–Any special rules for processing
Structured COBOL Programming, Stern & Stern, 9th edition
I/O SpecificationsI/O SpecificationsThe I/O specifications establish record names, field names, data sizes and types to be used in your program. For auxiliary storage specifications can take the form of an 01 level print out or a simply chart as follows.
Used in developing the Data Division.
Payroll-Record
Type Size
Name Alphanumeric 30
Address Alphanumeric 23
Pay-Rate Numeric 5.2 or 7 with 2 decimals
Etc.
Structured COBOL Programming, Stern & Stern, 9th edition
PROGRAM DEVELOPMENT PROCESSPROGRAM DEVELOPMENT PROCESS
2. Design the Program Using Program Planning Tools using Common Tools:
Hierarchy Charts– Charts showing relationships among sections in a programFlowcharts– A block diagram providing a pictorial representation of the
logic to be used in a programPseudocode– English-like expressions to depict the logic of the program
Structured COBOL Programming, Stern & Stern, 9th edition
HIERARCHY CHARTHIERARCHY CHART
3 0 0-G E T -D A T E 9 0 0-P R IN T -H E A DE RS
2 00 - IN IT IA LIZE
3 1 0-F IND -RA T E
9 0 0-P R IN T -H E A DE RS
3 2 0-P R INT -D E T A ILS
2 1 0 -P R O CE S S -R E C O R DS
1 0 0-C A L C UL A T E -V E HIC LE -RE G IS T R A T IO N -FE E S
The first phase of problem solving is to use a top down design. Begin with a very general problem statement and break it down into smaller and smaller pieces until you know you have pieces you can solve.
The hierarchy chart establishes the Paragraph names you will use in pseudo code and in your program’s Procedure Division. It outlines a very general program flow and shows the relationship between modules/paragraphs.
Answers the question WHAT needs to be done?
REMEMBERREMEMBER
The sooner you start coding the longer it will take you to complete the project!!!!!
Structured COBOL Programming, Stern & Stern, 9th edition
To Reduce Cost of Fixing ErrorsTo Reduce Cost of Fixing Errors
Phase in which error is detected
Relative cost of repair
Requirements 0.1 – 0.2
Design 0.5
Coding 1 2X
Program testing 2 4X
Acceptance test 5 10X
Maintenance 20 40X
Structured COBOL Programming, Stern & Stern, 9th edition
PROGRAM DEVELOPMENT PROCESSPROGRAM DEVELOPMENT PROCESS
3. Code and Enter the ProgramThe programmer writes and then keys or enters the source program into the computer system using a keyboard.
4. Compile the ProgramThe COBOL compiler checks the source code to insure there are no rule violations. A syntax error is a type of error that is found if a programming rule is violated.
Source program in symbolic language
Object program in machine language
Translated by compiler
Structured COBOL Programming, Stern & Stern, 9th edition
PROGRAM DEVELOPMENT PROCESSPROGRAM DEVELOPMENT PROCESS5. Test the Program
Programs are tested or debugged to ensure they have no errors.Runtime errors–A logic error produces incorrect results–An Operating System violation –results in program
termination6. Document the Program
Procedure manuals for users and computer operators, called documentation, is prepared for use by the operating staff for regular production.
Structured COBOL Programming, Stern & Stern, 9th edition
PROGRAM DEVELOPMENT PROCESS PROGRAM DEVELOPMENT PROCESS
Remember, coding is undertaken only after the program requirements have been fully specified and the logic has been carefully planned.
Structured COBOL Programming, Stern & Stern, 9th edition
DEBUGGING TECHNIQUESDEBUGGING TECHNIQUES
Desk Checking Reviewing the program before and after keying
Correcting Syntax ErrorsUsing the printed source list of the diagnostic messages to find and correct the errors
Structured COBOL Programming, Stern & Stern, 9th edition
DEBUGGING TECHNIQUES DEBUGGING TECHNIQUES Program Walkthroughs
Manually stepping through the program with test data.
Detecting Logic Errors by Executing the Program
Often the most difficult part of debugging. The preparation of test data is an extremely critical aspect of debugging.
Structured COBOL Programming, Stern & Stern, 9th edition
THE NATURE OF COBOLTHE NATURE OF COBOL
COBOL is a Business-Oriented LanguageCOBOL, is one of the most widespread commercial applications languages in use today.
COBOL is an abbreviation for COmmon Business Oriented Language,
COBOL is a Standard LanguageCOBOL is a common programming language available on most computers.
Structured COBOL Programming, Stern & Stern, 9th edition
THE NATURE OF COBOLTHE NATURE OF COBOLCOBOL is an English-like Language
All instructions can be coded using English words.The rules conform to many rules for writing in English
COBOL is a User-Friendly LanguageBecause users are able to understand the English-like instructions it is considered a user-friendly language.
Structured COBOL Programming, Stern & Stern, 9th edition
WHEN IT BEGANWHEN IT BEGAN
Developed in 1959 by the CODASYL Committee.A committee consisting of representatives members from academia, user groups, and computer manufacturers.
Developed as a standard business-oriented language for which all major manufacturers would provide compilers.
ANS established the first standards version in 1968. COBOL ’85 the current standard http://www.ansi.org 2002+ Standard
Structured COBOL Programming, Stern & Stern, 9th edition
THE FUTURE OF COBOLTHE FUTURE OF COBOL COBOL is likely to remain an important
language in the years ahead for two reasons:1. Older, mainframe-based “legacy” systems will
need to be maintained by maintenance programmers who know COBOL.
2. COBOL is still being used by many organizations for new application development.
Structured COBOL Programming, Stern & Stern, 9th edition
Use of COBOLUse of COBOL
About 200 billion lines of COBOL source code in use
5 billion new lines added each year Used by 42.7% of application programmers in
medium to large U.S. companies $200 million in expected revenues for 2001
Structured COBOL Programming, Stern & Stern, 9th edition
Interactive vs Batch ProgramsInteractive vs Batch Programs
Cobol suited for developing both types of programs
Interactive programs Accept input data from keyboard Input data processed immediately Output (results) displayed on screen
immediately
Structured COBOL Programming, Stern & Stern, 9th edition
Interactive vs Batch ProgramsInteractive vs Batch Programs
Batch programs Process large volumes of input at periodic
intervals Input data read in from files Output written to files
Structured COBOL Programming, Stern & Stern, 9th edition
TECHNIQUES FOR IMPROVING PROGRAM DESIGN
TECHNIQUES FOR IMPROVING PROGRAM DESIGN
Structured COBOL Programming, Stern & Stern, 9th edition
Improving Program DesignImproving Program DesignTwo techniques used to develop programs that are
easier to understand, test, debug and modify
Structured Programming
Top-Down Programming
Structured COBOL Programming, Stern & Stern, 9th edition
Structured ProgrammingStructured Programming Eliminates use of GO TO statements
Allowed skipping to different sections of program without returning to starting point
Program logic easier to follow with "GO-TO-less" programming
Structured COBOL Programming, Stern & Stern, 9th edition
Structured ProgrammingStructured Programming
Program divided into paragraphs Main paragraph or module controls logic flow
using PERFORM statements Main module "performs" other modules when
instructions in that module required Each module can be written and tested
independently of others
Structured COBOL Programming, Stern & Stern, 9th edition
Top-Down ProgrammingTop-Down Programming
Another technique to make programs easier to understand, test, debug and modify
Develop program like term paperDevelop outline firstAdd details for each of main stepsAdd further refinement for more complex steps
Structured COBOL Programming, Stern & Stern, 9th edition
Top-Down ProgrammingTop-Down Programming
For COBOL program Code main modules or routines first Code intermediate modules next Details deferred to minor modules and coded
last
Structured COBOL Programming, Stern & Stern, 9th edition
Program SpecificationsProgram Specifications
System Flowchart Input/Output Specifications Hierarchy Chart Psuedo code Narrative – as comments in program
Structured COBOL Programming, Stern & Stern, 9th edition
Attendance Report
INPUT PROCESSING OUTPUT
System Flowchart
Student-File
PRINT STUDENT
ATTENDANCE
Structured COBOL Programming, Stern & Stern, 9th edition
START
OPEN FILES
READFIRST
RECORD
WRITEHEADINGS
END OFFILE
REACHED?
PROCESSRECORDS
CLOSEFILES
STOP
PROCESSRECORDS
ENGINEERINGMAJOR ANDMORE THAN110 CREDITS
WRITESTUDENT
NAME
READNEXT
RECORD
STOP
Program Flow Chart1
2
3
4
5
6
7
8
FALSE TRUE
9
10
11
12
(a) Overall Flowchart (b) Detail of PROCESS-RECORDS
Structured COBOL Programming, Stern & Stern, 9th edition
Initialization
Processing
Termination
Open files
Read first record
Write heading
DO while data remains
IF engineering major with more than 110 creditsWrite student’s name
ENDIF
ENDDO
Close files
Stop
Pseudo Code
Structured COBOL Programming, Stern & Stern, 9th edition
Hierarchy of DataHierarchy of Data Bit - binary 0 or 1 Byte / Character - 8 bits Field - a basic fact about some entity
Customer - Current-BalanceInventory - Selling-Price
Record - a collection of related facts File - a collection of related records Database - an organization’s set of files
Structured COBOL Programming, Stern & Stern, 9th edition
Figure 1.1 Fields, Records, and Files
Fields(Facts)
Records(Set of Fields)
Name: John AdamsCredits: 90Major: Political Science
Name: Amelia EarhartCredits: 120Major: Aviation
Name: Orville WrightCredits: 115Major: Engineering
Name: Georgia O’KeeffeCredits: 125Major: Art
Jphn Adams 90 Political Science
Amelia Earhart 120 Aviation
Orville Wright 115 Engineering
Georgia O’Keeffe 125 Art
Structured COBOL Programming, Stern & Stern, 9th edition
Programming StructuresProgramming Structures
Sequence Selection Iteration/Repetition
Structured COBOL Programming, Stern & Stern, 9th edition
Structured Programming Using Modular Design for Coding Paragraphs
Structured Programming Using Modular Design for Coding Paragraphs
The most important technique for improving the design of a program in any language is called structured programming.
Structured Programming is a technique using logical control constructs that make programs easier to read, debug, and modify if changes are required.
Structured COBOL Programming, Stern & Stern, 9th edition
Modular ProgrammingModular Programming
Each module/paragraph must meet the following characteristic
COMPLETENESS – taken together the modules completely satisfy the program specificationsCOHESIVE – a paragraph must perform one and only one taskCOUPLING – paragraphs are loosely coupled – that is a paragraph does not depend on other paragraphs
Structured COBOL Programming, Stern & Stern, 9th edition
Structured Programming Using Modular Design for Coding Paragraphs
Structured Programming Using Modular Design for Coding Paragraphs
Structured programming is often called GO-TO-less programming since programmatic branches do not use GO TO statements.
In COBOL this means writing programs using PERFORM statements
Structured COBOL Programming, Stern & Stern, 9th edition
Structured Programming Using Modular Design for Coding Paragraphs
Structured Programming Using Modular Design for Coding Paragraphs
The typical structured program is divided into paragraphs or modules, where a main module calls in other modules as needed.
Each block on the hierarchy chart represents a paragraphEach module can be tested independently.– Paragraph, routine, module are used interchangeably
Structured COBOL Programming, Stern & Stern, 9th edition
The Top-Down Approach for Coding ModulesThe Top-Down Approach for Coding Modules
Proper design is best achieved by developing major modules before minor ones.
Main routines are coded first and are followed by intermediate routines and then minor ones.
Coding using the top-down manner gives the primary attention to the origination of the program.
Details are deferred or saved for minor modules, which are coded last.
Incremental testing
Structured COBOL Programming, Stern & Stern, 9th edition
100-MAINMODULE
210-WAGEROUTINE
HIERARCHY OR STRUCTURE CHARTHIERARCHY OR STRUCTURE CHART Structure charts illustrate the logic flow in a
program.The following chart shows the relationships among modules in a program:
200-INITIALIZE
Structured COBOL Programming, Stern & Stern, 9th edition
RULES FOR INTERPRETING PSEUDOCODERULES FOR INTERPRETING PSEUDOCODE
1. Each block on the Hierarchy chart needs to be represented in pseudocode
2. Paragraph names are prepared in numerical sequence 100-Main-Module 200-Initialize 210-Wage-Routine 220-Finalize-Program Etc…
3. The instructions between the:PERFORM UNTIL NO MORE DATA
executed repeatedly UNTIL there are no more records to process
END-PERFORM.
Structured COBOL Programming, Stern & Stern, 9th edition
Divisions of a Cobol ProgramDivisions of a Cobol Program Identification Division
Identifies program to OSProvides documentation
Environment DivisionDefines filesEstablishes Logical (program) and physical (computer) relationship
Data DivisionDescribes all input and output dataAs well as scratch pad data (working storage)
Procedure DivisionAll the program logic
Structured COBOL Programming, Stern & Stern, 9th edition
1 IDENTIFICATION DIVISION.2 PROGRAM-ID. SENIOR.3 AUTHOR. ROBERT GRAUER.45 ENVIRONMENT DIVISION.6 INPUT-OUTPUT SECTION.7 FILE-CONTROL.8 SELECT STUDENT-FILE ASSIGN TO ‘A:\CHAPTR02\SENIOR.DAT’9 ORGANIZATION IS LINE SEQUENTIAL.10 SELECT PRINT-FILE11 ASSIGN TO PRINTER.1213 DATA DIVISION.14 FILE SECTION.15 FD STUDENT-FILE16 RECORD CONTAINS 43 CHARACTERS17 DATA RECORD IS STUDENT-IN.18 01 STUDENT-IN.19 05 STU-NAME PIC X(25).20 05 STU-CREDITS PIC 9(3).21 05 STU-MAJOR PIC X(15).22
Identification Division
Data Division
Environment Division
Figure 1.6 The First COBOL Program
Structured COBOL Programming, Stern & Stern, 9th edition
23 FD PRINT-FILE24 RECORD CONTAINS 132 CHARACTERS25 DATA RECORD IS PRINT-LINE.26 01 PRINT-LINE PIC X(132).2728 WORKING-STORAGE SECTION. 29 01 DATA-REMAINS-SWITCH PIC X(2) VALUE SPACES.3031 01 HEADING-FILE.32 05 FILLER PIC X(10) VALUE SPACES. 33 05 FILLER PIC X(12) VALUE ‘STUDENT NAME’.34 05 FILLER PIC X(110) VALUE SPACES.3536 01 DETAIL-LINE.37 05 FILLER PIC X(8) VALUE SPACES.38 05 PRINT-NAME PIC X(25).39 05 FILLER PIC X(99) VALUE SPACES.40
Data Division
Figure 1.6 The First COBOL Program (continued)
Structured COBOL Programming, Stern & Stern, 9th edition
41 PROCEDURE DIVISION. 42 100-PREPARE-SENIOR-REPORT. 43 OPEN INPUT STUDENT-FILE 44 OUTPUT PRINT-FILE 45 READ STUDENT-FILE 46 AT END MOVE ‘NO’ TO DATA-REMAINS-SWITCH47 END-READ 48 PERFORM 200-WRITE-HEADING-LINE 49 PERFORM 210-PROCESS-RECORDS50 UNTIL DATA-REMAINS-SWITCH = ‘NO’51 CLOSE STUDENT-FILE52 PRINT-FILE
42 53 STOP RUN.54 55 200-WRITE-HEADING-LINE.56 MOVE HEADING-LINE TO PRINT-LINE57 WRITE PRINT-LINE.58 59 210-PROCESS-RECORDS.60 IF STU-CREDITS > 110 AND STU-MAJOR = ‘ENGINEERING’61 MOVE STU-NAME TO PRINT-NAME62 MOVE DETAIL-LINE TO PRINT-LINE63 WRITE PRINT-LINE64 END-IF
43 65 READ STUDENT-FILE66 AT END MOVE ‘NO’ TO DATA-REMAINS-SWITCH67 END-READ.
Figure 1.6 The First COBOL Program (continued)
Procedure Division
End End