CHAPTER 4 Coding Complete COBOL Programs - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

CHAPTER 4 Coding Complete COBOL Programs

Description:

The IDENTIFICATION and ENVIRONMENT DIVISIONs supply information about the nature ... records have been processed to release these files and deactivate the devices. ... – PowerPoint PPT presentation

Number of Views:167
Avg rating:3.0/5.0
Slides: 55
Provided by: troylewish
Category:

less

Transcript and Presenter's Notes

Title: CHAPTER 4 Coding Complete COBOL Programs


1
CHAPTER 4Coding Complete COBOL Programs
  • A Closer Look at the PROCEDURE DIVISION and the
    PERFORM Statement

2
A REVIEW OF THE FIRST THREE DIVISIONS
  • The IDENTIFICATION and ENVIRONMENT DIVISIONs
    supply information about the nature of the
    program and the specific equipment and files.
  • The FILE SECTION of the DATA DIVISION defines the
    input and output records.

3
A REVIEW OF THE FIRST THREE DIVISIONS
  • The WORKING-STORAGE SECTION of the DATA DIVISION
    is used for defining any areas not part of input
    and output files.
  • The instructions in the PROCEDURE DIVISION read
    and process the data and produce the output
    information.

4
THE FORMAT OF THE PROCEDURE DIVISION
  • PARAGRAPHS THAT SERVE AS MODULES Structure
    Chart documentation serves as the basis for
    determining paragraphs needed
  • The PROCEDURE DIVISION is divided into
    paragraphs.
  • Each paragraph is an independent module or
    routine that includes a series of instructions
    designed to perform a specific set of operations.
  • Using the structure chart as a guide paragraphs
    are written from the top down left to right -
    ie. 100s, 200s, 300s etc.

5
THE FORMAT OF THE PROCEDURE DIVISION
  • Paragraph-names, like the PROCEDURE DIVISION
    entry itself, are coded in Area A.
  • All other entries in the PROCEDURE DIVISION are
    coded in Area B.
  • Paragraph-names, like the PROCEDURE DIVISION
    entry, end with a period.

6
THE FORMAT OF THE PROCEDURE DIVISION
  • PARAGRAPHS THAT SERVE AS MODULES
  • Rules for forming paragraph-names are the same as
    rules for forming data-names except that a
    paragraph-name may have all digits.
  • Paragraph-names must be unique, meaning that two
    paragraphs may not have the same name.
  • Paragraph names must be meaningful ie. Reflect
    the purpose of the paragraph
  • Similarly, a data-name cannot also serve as a
    paragraph-name.

7
Paragraphs That Serve as Modules
  • We will use descriptive paragraph-names along
    with a numeric prefix such as 200-PROCESS-RTN to
    identify the type of paragraph.
  • A paragraph with a prefix of 200- is located
    after paragraph 100--XXX and before paragraph
    300-- YYY.

8
Statements within Paragraphs
  • Each paragraph in a COBOL program consists of
    statements, where a statement begins with a verb
    such as READ, MOVE, or WRITE, or a condition such
    as IF A B ....
  • As noted, all COBOL statements are coded in Area
    B whereas paragraph-names are coded in Area A.
  • Statements that end with a period are called
    sentences.
  • Use periods only
  • After Paragraph Names
  • After the last statement in a paragraph

9
Statements within Paragraphs
  • With COBOL 85 only the last statement in a
    paragraph ends with a period.
  • With COBOL 74, each statement typically ends
    with a period.
  • Recommended that each statement be coded on an
    individual line. And use of indentation
  • Makes programs much easier to read and debug.

10
THE SEQUENCE OF INSTRUCTIONS IN A PROGRAM
  • Instructions are typically executed in sequence
  • Unless a PERFORM statement is encountered.
  • PERFORM UNTIL ... END-PERFORM
  • A loop that repeatedly executes
  • All the statements between PERFORM and
    END-PERFORM
  • Until some condition is met
  • Instruction within the loop must do something to
    be sure the condition is met
  • Known as an inline PERFORM
  • A PERFORM paragraph-name UNTIL ....
  • Another form of using PERFORM to implement a loop

11
PERFORM .... UNTIL
  • Pretest loop the condition stated is tested
  • If condition is not met paragraph / instructions
    are executed
  • Upon return - start over at Step 1
  • If condition is met execution of statements
    continues with the statement following the LOOP

12
PERFORM UNTIL . . . END-PERFORM Statement A
Structured Programming Technique
  • The basic instruction format of the PERFORM UNTIL
    ... END- PERFORM statement is as follows
  • PERFORM
  • UNTIL condition-1
  • .
  • .
  • .
  • END-PERFORM COBOL 85 only

13
THE SEQUENCE OF INSTRUCTIONS IN A PROGRAM
  • A PERFORM paragraph-name
  • An instruction that temporarily transfers control
    to another paragraph
  • Instructions are executed 1 time
  • Program control is returned to statement
    following the PERFORM
  • Program knows when to return when
  • A new PARAGRAPH is encountered or
  • No more program statements to execute

14
The Top-Down Approach for Coding Paragraphs
  • Well-designed programs are written using a
    top-down approach.
  • This means that the main module is coded first
  • subsequent modules are coded from the major
    level to the detail level.
  • Endeavor to code the more general paragraphs
    first and end with the most detailed ones.
  • Facilitates incremental testing
  • Write the paragraphs at one level
  • Lower level paragraphs at stubs (ie. No code)
  • Test
  • Proceed to write paragraphs at next level
  • Go step 2

15
STATEMENTS TYPICALLY CODED IN THE MAIN MODULE OF
BATCH PROGRAMS
16
OPEN Statement
  • The OPEN statement accesses the files in a
    program and indicates which are input and which
    are output. It has the following instruction
    format
  • Format
  • OPEN INPUT file-name-1 . . .
  • OUTPUT file-name-2 . . .
  • OPEN INPUT file-name-1 ...
  • OPEN OUTPUT file-name-2 ...

17
OPEN
OPEN INPUT file-name-1 ...
OUTPUT file-name-2 ...
18
OPEN STATEMENT
  • A REVIEW OF INSTRUCTION FORMAT SPECIFICATIONS
  • 1. Uppercase words are COBOL reserved words.
  • 2. Underlined words are required in the
    statement or option specified.
  • 3. Lowercase entries are user-defined words.
  • 4. Braces denote that at least one of the
    enclosed items is required.

19
OPEN STATEMENT
  • A REVIEW OF INSTRUCTION FORMAT SPECIFICATIONS
  • 5. Brackets denote that the enclosed item is
    optional.
  • 6. Punctuation, when included in the format, is
    required.
  • 7. The use of three dots or ellipses (. . .)
    indicates that additional entries of the same
    type (a file-name in this case) may be repeated
    if desired.

20
OPEN STATEMENT
  • FUNCTIONS OF THE OPEN STATEMENT
  • 1. Indicates which files will be input and which
    will be output.
  • 2. Makes the files available for
    processing Locates INPUT file if not found a
    runtime error Allocates space for the
    OUPUT filr
  • 3. Performs header label routines if label
    records are STANDARD.

21
OPEN STATEMENT
  • DEBUGGING TIPS CODING GUIDELINES
  • Indent each line within an OPEN sentence.
  • This makes a program more readable.
  • For the OPEN sentence, we typically indent so
    that the words INPUT and OUTPUT are aligned.
    For other entries we indent four spaces.

22
OPEN STATEMENT
  • DEBUGGING TIPS CODING GUIDELINES
  • AN EXAMPLE
  • OPEN INPUT OLD-MASTER-IN
  • TRANS-FILE
  • OUTPUT NEW-MASTER-OUT
  • ERROR-LIST.

23
OPEN STATEMENT
  • DEBUGGING TIPS CODING GUIDELINES
  • For output disk files, the ASSIGN clause of a
    SELECT statement often specifies the name the
    file is to be saved as
  • SELECT SALES-FILE ASSIGN TO A\DATA100.DAT
    ORGANIZATION IS LINE SEQUENTIAL.

24
READ Statement
  • Typically, after an input file has been opened,
    the PERFORM ... END-PERFORM loop, which begins
    with a READ, is executed.
  • A READ statement transmits data from the input
    device, assigned in the ENVIRONMENT DIVISION, to
    the input storage area, defined in the FILE
    SECTION of the DATA DIVISION.

25
READ Statement
  • The following is a partial instruction format for
    a READ statement
  • Format
  • READ file-name-1
  • AT END statement-1 . . .
  • NOT AT END statement-2 . . .
  • END-READ COBOL 85 ONLY

26
READ Statement
  • AT END
  • READ test to see if there are more records on the
    file if not the instruction(s) following the AT
    END clause are executed
  • NOT AT END
  • If a successful READ the instruction(s) following
    the NOT AT END are executed
  • Program execution then continues with the
    instruction following the END-READ

27
READ Statement
  • The file-name specified in the READ statement
    appears in three previous places in the program
  • 1. The SELECT statement, indicating the file-name
    and the device or implementor-name assigned to
    the file.
  • If a file is stored on a disk, for example, a
    READ operation transmits data from the disk to
    the input area.
  • 2. The FD entry, describing the file and its
    format.
  • 3. The OPEN statement, accessing the file and
    activating the device.

28
READ Statement
  • The primary function of the READ statement is to
    transmit one data record to the input area
    reserved for that file.
  • Each time a READ statement is executed, one
    record is read into primary storage - not the
    entire file.

29
DEBUGGING TIP
  • Code the AT END and NOT AT END clause on separate
    lines and indent them for readability.
  • If an error occurs, you will be able to more
    easily identify the problem because the line
    number of the error is specified.
  • Common programming error trying to continued to
    process records whrn END-OF-FILE has been
    encountered

30
QUESTIONS?
31
End-of-Job Processing The CLOSE and STOP RUN
Statements
  • The CLOSE Statement
  • Files must be accessed or activated by an OPEN
    statement before data may be read or written.
  • Similarly, a CLOSE statement is coded at the end
    of the job after all records have been processed
    to release these files and deactivate the devices.

32
End-of-Job Processing The CLOSE and STOP RUN
Statements
  • The format of the CLOSE is
  • CLOSE file-name-1 . . .

33
End-of-Job Processing The CLOSE and STOP RUN
Statements
  • The STOP RUN Statement
  • The STOP RUN instruction tells the computer to
    terminate the program.
  • All programs should include a STOP RUN statement
    to end the run.
  • The STOP RUN is usually the last instruction in
    the main module.

34
STATEMENTS TYPICALLY CODED FOR PROCESSING INPUT
RECORDS AND PRODUCING OUTPUT RECORDS
35
Simplified MOVE Statement
  • A simple MOVE statement has the following basic
    instruction format
  • MOVE identifier-1 TO identifier-2
  • Fields in main memory may be moved to other
    fields with the use of the MOVE instruction.
  • The word identifier'' means data-name''.

36
WRITE Statement
  • The WRITE instruction takes data in the output
    area defined in the DATA DIVISION and transmits
    it to the device specified in the ENVIRONMENT
    DIVISION.
  • A simple WRITE statement has the following
    format
  • WRITE record-name-1

37
WRITE Statement
  • Format
  • WRITE record-name-1 AFTER ADVANCING n
    LINE(S) AFTER ADVANCING PAGE
  • Note that although files are read, we write
    records.
  • The record-name appears on the 01 level and is
    generally subdivided into fields. The record
    description specifies the format of the output.
  • With each WRITE instruction, we tell the computer
    to write data that is in the output area.

38
NOTE Carefully
  • You READ a file
  • You WRITE a record

39
LOOKING AHEAD AN INTRODUCTION TO ARITHMETIC and
CONDITIONAL verbs
40
The four basic arithmetic verbs have the
following simple formats
  • ADD identifier-1 literal-1 TO
    identifier-2
  • SUBTRACT identifier-1 literal-1 FROM
    identifier-2
  • MULTIPLY identifier-1 literal-1 BY
    identifier-2
  • DIVIDE identifier-1 literal-1 INTO
    identifier-2
  • Result always stored in last identifier.

41
The four basic arithmetic verbs have the
following another format
  • ADD identifier-1 literal-1 TO identifier-2
    GIVING indentifer-3
  • SUBTRACT identifier-1 literal-1 FROM
    identifier-2 GIVING indentifer-3
  • MULTIPLY identifier-1 literal-1 BY
    identifier-2 GIVING indentifer-3
  • DIVIDE identifier-1 literal-1 INTO
    identifier-2 GIVING indentifer-3
  • Result always stored in last identifier.

42
The basic instruction format for a conditional IF
is as follows
  • Format

    IF condition-1
  • THEN imperative-statement-1. . .
  • ELSE

    imperative-statement-2 . . .
  • END-IF
  • Note that the ELSE clause is optional. Numerous
    statements can follow each IF or ELSE clause.

43
The simple conditions that can be tested are as
follows
  • (identifier-1)
  • (or IS EQUAL TO) identifier-2
  • lt (or IS LESS THAN) literal-1
  • gt (or IS GREATER THAN)

44
REVIEW OF COMMENTS IN COBOL
45
COMMENTS IN COBOL
  • An asterisk () in column 7 (the continuation
    position) of any line makes the entire line a
    comment
  • Use comments freely to make your program
    user-friendly and easier to understand.

46
Coding Guidelines for PROCEDURE DIVISION Entries
  • 1. Each clause should be on a separate line
    indented for readability. For example
  • READ ...
  • AT END ...
  • NOT AT END ...
  • END-READ.

47
Coding Guidelines for PROCEDURE DIVISION Entries
  • 2. Each paragraph-name should begin with a
    sequence number that helps to pinpoint the
    location of the paragraph
  • a descriptive name should follow this number
    (e.g., 100-MAIN-MODULE, 200-CALC-RTN).
  • 3. The last statement in a paragraph should
    always end with a period.

48
COBOL 2000 CHANGES
  • You will be able to code comments on a line, even
    those with instructions
  • gt will be used to add a comment to a line.
  • After gt appears, characters to the end of the
    line will not be compiled.

49
YEAR 2000-COMPLIANT DATE FIELDS
  • Older, legacy programs, using only two-digit for
    the year, will no longer accurately depict the
    date beginning in the year 2000.
  • Problems associated with dates after this are
    referred to as the Year 2000 Problem or Y2K
    Problem.

50
YEAR 2000-COMPLIANT DATE FIELDS
  • Fixes for the Y2K problem are not all that
    difficult
  • They are just costly and time-consuming.
  • In addition to program source changes, all files
    on which they operate will also need to be
    modified.

51
CHAPTER SUMMARY
  • COBOL 85
    PROCEDURE DIVISION.
    paragraph-name-1.

    OPEN INPUT file-name-1
    OUTPUT file-name-2
    PERFORM
    UNTIL ARE-THERE-MORE-RECORDS 'NO
  • READ file-name-1
    AT END
    MOVE 'NO ' TO ARE-
    THERE-MORE-RECORDS NOT AT END
    PERFORM paragraph-name-2

    END-READ

    END-PERFORM

    CLOSE file-name-1

    file-name-2

    STOP RUN.

52
CHAPTER SUMMARY
  • A. Paragraph-names are coded in Area A and end
    with a period.
  • Rules for forming paragraph-names are the same as
    for data-names except that a paragraph-name can
    have all digits.
  • We use a prefix such as 100-, 200- , 300-, along
    with a descriptive name such as HEADING- RTN or
    MAIN-MODULE.
  • A paragraph with a prefix of 200- is located
    after a paragraph with prefix 100- and before a
    paragraph with prefix 300-.

53
CHAPTER SUMMARY
  • B. All statements are coded in Area B.
  • We recommend coding one statement per line.
  • C. Instructions are executed in the order in
    which they appear unless a PERFORM statement
    executes a loop or transfers control.

54
CHAPTER SUMMARY
  • D. When the main module's
  • PERFORM UNTIL ... END-PERFORM
  • is encountered, the loop specified is executed
    repeatedly until the condition most often that
    condition is met when there are no more input
    records.
Write a Comment
User Comments (0)
About PowerShow.com