Title: COBOL Structured Programming
1COBOL - Structured Programming
- Structured programming use logical control
structures to specify the order in which
instructions are executed. - These structures are the same for all languages
- The four logical structures are
- Sequence
- Selection
- Iteration
- Case structure
2COBOL - Introduction
- A program - is a set of instructions that enable
a computer to process data. - There are two types of programs
- Operating System programs
- Application programs
- Operating Systems
- All programs run under the control of an
operating system - This program monitors and control the overall
operation of the computer - Application programs
- Takes input data and converts it to meaningful
output information
3COBOL - History
- COBOL is an acronym that stands for Common
Business Oriented Language. - COBOL was developed in the 1950s and was
designed for applications with large volumes of
Data - To date over 50 of computer code in existence is
in COBOL
4COBOL - History
- COBOLs popularity is due largely to the USAs
Department of Defense which played in leading
role in standardizing the COBOL language
5Steps in program Development- COBOL programming
context
- Read and understand program specifications
- Draw hierarchy charts
- Draw program flowcharts
- Write program code
- Compile a program and correct errors
- Test-run the program with test data
- Document the program
6Divisions and Programming Area
- There are four main divisions in a COBOL program
- IDENTIFICATION DIVISION
- ENVIRONMENT DIVISION
- DATA DIVISION
- PROCEDURE DIVISION
7Notes to remember about Divisions
- The divisions are coded in a text Editor.
- The divisions are coded in that particular order
- Name of the division and actual word DIVISION
are coded separated by a single space - Each division plays a specific role in the COBOL
program
8Rules for the Coding a COBOL program
- columns 1-6
- Optional sequence number
- column 7
- Comment line (with an )
- (ignored by compiler)
- columns 8-11
- Area A (division heads, paragraphs, FD,
01entries) - columns 12-72
- Area B (All other entries)
- columns 73-80
- Program identification(also ignored by the
compiler)
91.0 IDENTIFICATION DIVISION
- Identifies the program
- Example
IDENTIFICATION DIVISION. PROGRAM-ID. PRGM01.
102.0 ENVIRONMENT DIVISION
- There are two sections in this division
- Configuration section (Hardware details this
section is NOT NEEDED) -
- Input-Output section(File details)
INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT
SALES-FILE ASSIGN TO DISK-SALEFLE. SELECT
PRNT-FILE ASSIGN TO PRINTER-QPRINT.
113.0 DATA DIVISION
- This DIVISION typically contains two sections
- FILE SECTION, and
- WORKING-STORAGE SECTION
- File section deals with Input and Output file
descriptions - FD- File description (starts in Area A)
- 01 - to describe the record layout (also in area
A) - Working Storage section
- Is the section where the input and output records
are manipulated and all other variables needed
are defined. - Typically contains the variables, heading lines
and detail record
12COBOL Language Elements
- In a COBOL program, youll find the following
words - Words
- Reserved words
- User Defined words
- Constants or Literals
- Non-Numeric
- Numeric
- Figurative constants
- Note Reserve words, and User Defined words are
case NON-sensitive
13COBOL Language Elements
- Reserved words
- See Appendix B, page 700
- User Defined words (page 85)
- These are data names for storage, paragraph, and
section names - Examples
- EMPLOYEE-NAME-OUT
- WS-END-OF-FILE-INDICATOR
- Rules
- max 30 characters
- A-Z, 0-9, and hyphen (-)
- must start and end with A-Z, or 0-9 (paragraph ad
section name are the only user defined words that
are allowed to start with 0-9) - cannot begin or end with hyphen
- must be at least one alphabetic character
- cannot be a reserved word
14COBOL Language Elements
- Non-Numeric Literals
- these are string constants
- Examples
- Summary of Loans Report
- ABC
- Rules
- max 160 characters
- quotation marks are required before and after
(single or double - compiler option) - contents between the quotes are case-sensitive
15COBOL Language Elements
- Numeric Literals
- these are hard-coded numbers
- Examples
- 12.45
- -1
- Rules
- max 18 digits
- , - must appear on the left of the number
16Non-Edited Picture Characters
- X Alpha-numeric (1 byte)
- 9 Numeric (1 bytes)
- S Operational SIGN (does not take up any space)
- e.g. 05 WS-CREDIT PIC S9(3)
- V Implied Decimal Point
- e.g. 05 WS-SALARY PIC 9(5)V9(02)
17Example Input file and File Description
- Data File of SALEFILE
- 111111111MOOING MARTY
001451255000 - 222222222PATSY POTATO
450003569050 - 333333333ROWDY RODENT
205001259020 - 444444444STARING STACEY
000090000650 - The File Description
- FD SALES-FILE
- RECORD CONTAINS 80 CHARACTERS
- DATA RECORD IS SALES-REC.
- 01 SALES-REC.
- 05 EMPLOYEE-NUM-IN PIC 9(09).
- 05 NAME-IN PIC X(20).
- 05 QUANTITY-IN PIC 9(05).
- 05 AMOUNT-IN PIC 9(05)V99.
- 05 FILLER PIC X(39)
- Note the use of X and 9 and numbers in
parentheses
18Example Input file and File Description
Disk
.
Program Executable Code
ENVIRONMENT DIVISION. INPUT-OUTPUT
SECTION. FILE-CONTROL. SELECT SALES-FILE
ASSIGN TO DISK-SALEFLE. DATA DIVISION. FILE
SECTION. FD FD SALES-FILE RECORD CONTAINS
80 CHARACTERS DATA RECORD IS SALES-REC.
SALEFLE file
Buffer
01 SALES-REC. 05 EMPLOYEE-NUM-IN PIC
9(09). 05 NAME-IN PIC X(20). 05
QUANTITY-IN PIC 9(05). 05 AMOUNT-IN PIC
9(05)V99. 05 FILLER PIC X(39)
AS400 Operating System
Working Storage
19Working Storage Section
- Purpose
- to save, or store temporary data for use in the
program - Records and variables
- consist of Group and Elementary items
- Elementary items MUST HAVE a PIC clause
- Group items may consist of one or more elementary
item, or other Group items - Group items are consider alphanumeric always
- To show groupings, LEVEL numbers are used
- Rules
- In sequence (from smaller numbers to larger)
- But dont have to be consecutive
- Also, not required to jump by any given interval
can be random - Record level is 01 level
- Valid numbers are 01 through 49 inclusive
- 66, 77, 88 have special purpose usage
20Working Storage Section
- VALUE clauses are ONLY allowed in Working Storage
(not allow in FILE section. - Used to initial elementary data items
- Cannot be used for group items
- can take a
- literal
- Figurative constant
- the word ALL
- Examples
- 05 WS-RPT-TITLE PIC X(20) VALUE SALES REPORT.
- 05 WS-CHRISTMAS-DAY.
- 10 XMAS-YR PIC 9(04) VALUE 2002.
- 10 XMAS-MM PIC 9(02) VALUE 12.
- 10 XMAS-DD PIC 9(02) VALUE 25.
- 01 DRAW-LINE PIC X(133) VALUE ALL .
214.0 Procedure Division
- Its very important to be precise while dealing
with the procedure division. - This is the only division that requires hierarchy
chart, pseudocode, and program flowcharts. - Hierarchy chart Overview of the process.
- Pseudocode Near COBOL code without syntax.
- Flowchart Graphical representation of program
flow.
22Hierarchy Chart
MAIN-RTN
HDG-RTN
READ-RTN
DETAIL-RTN
READ-RTN
23Pseudocode
MAIN PROCESS OPEN FILES WRITE
HEADINGS READ FIRST RECORD PROCESS
RECORDS UNTIL NO MORE RECORDS CLOSE FILES
STOP PROCESSING WRITE HEADINGS
MOVE AND WRITE HEADING LINE 1 MOVE AND WRITE
HEADING LINE 2 PROCESS RECORDS MOVE AND
WRITE DETAIL RECORDS READ NEXT
RECORD
24FLOWCHART
PROGRAM FLOW
START
OPEN FILES
READ- RTN
HDG- RTN
UNTIL WS- EOFY
CLOSE FILES
DETAIL- RTN
STOP