22
Preparing Program Logic 1. Hierarchy Chart can be drawn when a program contains multiple modules or paragraphs. It shows the relationship of the paragraphs in the PROCEDURE DIVISION. Each rectangle represents one paragraph in the program COBOL21: A Complete COBOL Program 1 100-MAIN-RTN 200-INITIAL-RTN 500-PROCESS-RTN 900-CLOSE-RTN 600-HEADING-RTN 700-WRITE-RTN the program

COBOL Lectures 2nd Week

Embed Size (px)

Citation preview

Preparing Program Logic

1. Hierarchy Chart

can be drawn when a program contains multiple modules or

paragraphs. It shows the relationship of the paragraphs in the

PROCEDURE DIVISION. Each rectangle

represents one

paragraph in

the program

COBOL21: A Complete COBOL Program

1

100-MAIN-RTN

200-INITIAL-RTN 500-PROCESS-RTN 900-CLOSE-RTN

600-HEADING-RTN 700-WRITE-RTN

the program

Preparing Program Logic

2. Pseudocode

preferred by many programmers in planning program logic. It

should be comprehensive enough to use as a guide as you

write the COBOL code.Example:

Initialize variables Display summary lines

Access the system date Close files

COBOL21: A Complete COBOL Program

Access the system date Close files

Open input file Terminate program

Read a record

Do-while not end of file

Display headings as needed

move input fields to output fields

display a detail line

Increment counters

Hold the screen as needed

Read next record

End-do2

Preparing Program Logic

3. Flowcharts

can be used for both planning and documentation. It shows

the step-by-step logic flow within the program.

START

200-INITIAL-RTN

100-MAIN-RTN

COBOL21: A Complete COBOL Program

200-INITIAL-RTN

500-PROCESS-RTN

900-INITIAL-RTN

STOP

Until

EOFSW = 1

3

IDENTIFICATION DIVISION

IDENTIFICATION DIVISION.

PROGRAM-ID. Object program name.

(max of 8 chars)

[AUTHOR.] programmer’s name.

[INSTALLATION.] client’s name or company name.

COBOL21: A Complete COBOL Program

[INSTALLATION.] client’s name or company name.

[DATE-WRITTEN.] installation date.

[DATE-COMPILED.]

[SECURITY.] name of person who have access to

the output of the program.

4

ENVIRONMENT DIVISION

ENVIRONMENT DIVISION.

[CONFIGURATION SECTION.] used when a program

must access special hardware-related items.

[SOURCE-COMPUTER.]platform used in developing

the program.

COBOL21: A Complete COBOL Program

the program.

[OBJECT-COMPUTER.]platform used in executing

the program.

5

ENVIRONMENT DIVISION

ENVIRONMENT DIVISION.

INPUT-OUTPUT SECTION.

Division

headersection

header

paragraph

header

Input

COBOL21: A Complete COBOL Program

FILE-CONTROL.

SELECT file-name ASSIGN TO path

ORGANIZATION IS LINE SEQUENTIAL.

SELECT file-name ASSIGN TO path.

Inputfile

Output

file6

DATA DIVISION

DATA DIVISION.

FILE SECTION.

Describes the content and organization of the files to be processed

within the program. The only fields described are those found in the

records of the input or output files.

COBOL21: A Complete COBOL Program

WORKING-STORAGE SECTION.

Describes fields that are not part of the files, such as switches,

accumulators, counters, work area fields, and output record

layouts.

7

DATA DIVISION

DATA DIVISION.

FILE SECTION.

FD file-name

[RECORD CONTAINS integer CHARACTERS]

[LABEL RECORD IS STANDARD]

COBOL21: A Complete COBOL Program

[LABEL RECORD IS STANDARD]

[ RECORDS ARE OMITTED ]

[DATA RECORD IS data-name1 ]

[ RECORDS ARE ]

record-description-entry ...

8

DATA DIVISIONFILE SECTION.

FD --> File description is used for each file to be processed by the

program. The file name following FD must be the same user-defined name following the word SELECT in the FILE-CONTROL paragraph.

RECORD CONTAINS --> can be used to document the the logical

record length for the file being defined.

COBOL21: A Complete COBOL Program

LABEL RECORD --> required in COBOL74, used to specify the

presence or absence of a header label on files.

STANDARD --> used in the LABEL RECORD entry for disk files and

tape files.

OMITTED --> applies only to some tape files and to unit record input

or output devices such as scanners, card readers, andprinters.

DATA RECORD --> used to specify the name of the records within the

file, for documentation purposes only.9

DATA DIVISION

record-description-entry ...

Very important part of the program, it creates a temporary storage

area, or buffer, for input and output records.

Example.

01 INVENTORY-RECORD.

02 IR-STATUS PIC X.

88 AVAILABLE VALUE “A”.

COBOL21: A Complete COBOL Program

88 AVAILABLE VALUE “A”.

88 OUT-OF-STOCK VALUE “O”.

02 IR-NUMBER PIC X(11).

02 IR-ITEM-NAME PIC X(20).

02 IR-QUANTITY-ON-HAND PIC 9(5).

02 IR-QUANTITY-ORDERED PIC 9(5).

02 IR-LAST-SHIPMENT.

03 IR-DATE-LAST-RECEIVED PIC 9(8).

03 IR-QUANTITY-LAST-RECEIVED PIC 9(5).

10

DATA DIVISION

Level Number

Used to show how data items or fields are related to each other.It shows the hierarchy of data. It can range from 01 to 49 and special level number 77 and 88.Used to define switches, accumulators, and counters, and to

define output record layouts for reports.

COBOL21: A Complete COBOL Program

Level 01 (used in defining record-name)

used to specify an individual record or group of related fields.

Level 02-49 (used in defining field-name)

used to specify an individual record or group of related fields.

11

DATA DIVISION

Two types of Item/Field

1. Group itemThese items can be divided into subordinate data items withother level numbers. It has no picture clause.

Example: Employee-Name, Employee-Address

COBOL21: A Complete COBOL Program

Example: Employee-Name, Employee-Address

2. Elementary ItemA field that is part of some larger field or record.A name that is not divided into subordinate data items and has apicture clause.

Example: Last-Name, First-Name, MI, Street, City, Province

12

DATA DIVISION

PICTURE or PIC Clause• It specifies a detailed description of an elementary item. • It indicates the number of characters in the data item and

the type of data: letters only (alphabetic), PIC A; numbers only (numeric), PIC S9V99; or

COBOL21: A Complete COBOL Program

numbers only (numeric), PIC S9V99; or letters, numbers, and

special characters (alphanumeric), PIC X. Example:

02 EMPLOYEE-NAME.

03 LAST-NAME PIC X(15).

03 FIRST-NAME PIC X(15).

03 MI PIC X.

13

DATA DIVISION

Five basic data categories1. Alphabetic

An item that may contain only letters of the alphabet or spaces. The character A is used to represent alphabetic items.

2. AlphanumericAn item that may contain letters, numbers, spaces, and special characters.

COBOL21: A Complete COBOL Program

An item that may contain letters, numbers, spaces, and special characters.The character X is used to represent alphanumeric items.

3. NumericAn item that may contain numbers 0 to 9. The character 9 is used to define numeric fields. An S at the beginning of the picture of a numeric field indicates

the fields is a signed numeric field, either positive or negative.Without an S, a numeric field is unsigned and can never contain a

negative value. 14

DATA DIVISION

Five basic data categories3. Numeric (continuation)

Implied decimal position --> The use of a V in a numeric picture identifiesthe implied decimal position. No decimal point actually isstored in the field, but whenever numeric processingoccurs, the field will be treated as though the decimalpoint is in the designated position.

COBOL21: A Complete COBOL Program

point is in the designated position.

USAGE --> indicates how the numeric field is stored in memory or auxiliary storage. The default USAGE for a field is DISPLAY. It means that each character or digit in the field requires one byte of storage.

Ex. 02 UNIT-COST PIC 9(5)V99. <-- default

02 UNIT-COST PIC 9(5)V99 DISPLAY.

15

DATA DIVISION

Five basic data categories3. Numeric (continuation)

BINARY/COMP --> provides an efficient way for the computer to work with and manipulate numeric data.

Ex. 02 UNIT-COST PIC 9(5)V99 BINARY.

02 UNIT-COST PIC 9(5)V99 COMPUTATIONAL.

COBOL21: A Complete COBOL Program

02 UNIT-COST PIC 9(5)V99 COMPUTATIONAL.

02 UNIT-COST PIC 9(5)V99 COMP.

PACKED-DECIMAL --> causes data to be stored in a format that is efficient for the computer's numeric operations.The data are stored as a base 10 number with two digits per byte.

Ex. 02 UNIT-COST PIC 9(5)V99 PACKED-DECIMAL.

02 UNIT-COST PIC 9(5)V99 COMPUTATIONAL-3.

02 UNIT-COST PIC 9(5)V99 COMP-3. 16

DATA DIVISION

Five basic data categories3. Numeric (continuation)

INDEX --> special fields reserved to store values used in conjunction

with arrays and tables.Ex. 02 EMPLOYEE-IDX PIC 9(5) INDEX.

4. Alphanumeric edited

COBOL21: A Complete COBOL Program

4. Alphanumeric editedAllows an alphanumeric field to be edited or formatted for output. It fills the

destination item from left to right.

The edit patterns are blank (B), zero (0), and slash (/) insertion characters.This can be used in both numeric and alphanumeric fields.

Ex. 02 F PIC XBXBXBX/XXBXBXBX VALUE “EAST ASIA”.

Output: E A S T/ A S I A 17

DATA DIVISIONFive basic data categories5. Numeric edited (Edit Patterns)

Insertion Characters$ --> Dollar Sign, --> Comma. --> Decimal Period

Z --> Zero Suppression* --> Zero Suppression with asterisks

(check protection symbol)

COBOL21: A Complete COBOL Program

18

Signed Numeric Fields+ --> Plus Sign- --> Minus Sign

Accounting ApplicationsCR --> Credit SymbolDB --> Debit Symbol

. --> Decimal Period P --> Scaling Symbol

DATA DIVISION

DATA DIVISION.

FILE SECTION.

. . .

WORKING-STORAGE SECTION.

Contains fields that are not part of any record layout, but are

necessary to accomplish the task of the program.

COBOL21: A Complete COBOL Program

Switches --> are used in the decision-making logic of a program.Ex. 02 EOFSW PIC X VALUE ‘Y’.

Accumulators or Counters--> are used in arithmetic and containssums.

Ex. 02 ACC-SALES-AMOUNT PIC 9(9)V99 VALUE ZEROES.

02 EMPLOYEE-CTR PIC 9(3) VALUE ZEROES.

19

DATA DIVISION

Work Area fields --> are miscellaneous fields needed for processing but not falling under the otherspecial categories used.

Ex. 02 WS-SYSTEM-DATE.

03 WS-YY PIC 99.

03 WS-MM PIC 99.

03 WS-DD PIC 99.

COBOL21: A Complete COBOL Program

Output record layouts --> define the formats for the heading lines,detail lines, and summary lines.

Ex. 01 REPORT-HEADING.

02 HEADING-LINE1.

03 F PIC X(18) VALUE SPACES.

03 F PIC X(62) VALUE “EAST ASIA COLLEGE

- “OF INFORMATION TECHNOLOGY”.

02 HEADING-LINE2.

. . . 20

DATA DIVISION

FILLER --> a reserved word used when a name is not given to a field. Only fields that will be referenced later in the program need user-defined names. A filler entry is not directly referenced anywhere in the program and, therefore, needs no name.

Ex. 02 FILLER PIC X(09) VALUE “EAST ASIA”.

SPACES/SPACE --> is a reserved word called a figurative constant. It is a nonnumeric literal filled with blanks, such as “ “.

COBOL21: A Complete COBOL Program

a nonnumeric literal filled with blanks, such as “ “.Ex. 02 FILLER PIC X(05) VALUE “SPACES”.

ZEROES/ZEROS --> is another figurative constant. It represents the numeric value 0.

Ex. 02 CTR PIC 9(05) VALUE ZEROES.

21

DATA DIVISION

ALL --> is another figurative constant used to fill a field with the character in the non-numeric literal.

Ex. 02 FILLER PIC X(05) VALUE ALL “-”. or

Ex. 02 FILLER PIC X(05) VALUE “-----”.

HIGH-VALUES/HIGH-VALUE --> is another figurative constant that represents the greatest value in a computer’s collating sequence. It means that no other field will have a value greater than HIGH-VALUES.

COBOL21: A Complete COBOL Program

means that no other field will have a value greater than HIGH-VALUES.Ex. 02 CTR PIC 9(05) VALUE HIGH-VALUES.

LOW-VALUES/LOW-VALUE --> is another figurative constant that represents the lowest value in a computer’s collating sequence. It means that no other fields will have a value less than LOW-VALUES.

Ex. 02 CTR PIC 9(05) VALUE LOW-VALUES.

22