Lesson 5: Procedure Division and COBOL Statements
Learning goals
After studying this lesson, learners should be able to:
- Use common Procedure Division statements.
- Read and write file-processing statements.
- Apply arithmetic statements and the
COMPUTEstatement. - Use condition statements and relational operators.
- Use
PERFORMfor repeated processing. - Understand basic one-dimensional and two-dimensional COBOL tables.
Procedure Division
The Procedure Division contains the executable part of a COBOL program. It tells the computer what to do with the data described in the Data Division.
A simple Procedure Division may look like this:
PROCEDURE DIVISION.
MAIN-PROCEDURE.
OPEN INPUT STUDENT-FILE
OUTPUT REPORT-FILE.
PERFORM PROCESS-RECORDS UNTIL EOF-SW = 1.
CLOSE STUDENT-FILE REPORT-FILE.
STOP RUN.
OPEN statement
OPEN connects a program's file name to the actual physical file and prepares it for processing.
Formats:
OPEN INPUT file-name.
OPEN OUTPUT file-name.
OPEN I-O file-name.
OPEN EXTEND file-name.
Modes:
| Mode | Use |
|---|---|
INPUT | Read existing data. |
OUTPUT | Create or write output. |
I-O | Read and update direct-access files. |
EXTEND | Add records to the end of an existing file. |
Examples:
OPEN INPUT STUDENT-FILE.
OPEN OUTPUT REPORT-FILE.
OPEN INPUT STUDENT-FILE OUTPUT REPORT-FILE.
READ statement
READ gets one record from an input file.
READ STUDENT-FILE
AT END MOVE 1 TO EOF-SW.
For a sequential file, the first READ gets the first record, the next READ gets the next record, and so on. The AT END clause handles the end of the file.
MOVE statement
MOVE copies a literal or data-field value into another data field.
Direct move:
MOVE 1 TO EOF-SW.
MOVE "Y" TO ANSWER.
MOVE 20 TO COUNTER.
Indirect move:
MOVE EMPLOYEE-NAME TO PRINT-NAME.
MOVE SALARY TO PRINT-SALARY.
Important rules:
- Numeric values should be moved into numeric fields.
- Alphanumeric values should be moved into alphanumeric fields.
- If the receiving alphanumeric field is longer, blanks are added on the right.
- If the receiving numeric field is longer, zeroes are added on the left.
- If the receiving field is too short, truncation can occur.
Example:
| Receiving field | Move statement | Result |
|---|---|---|
PIC X(10) | MOVE "PASSED" TO REMARKS | PASSED plus four blanks |
PIC 999 | MOVE 20 TO COUNTER | 020 |
PIC 9 | MOVE 123 TO COUNTER | 3 after left truncation |
WRITE statement
WRITE sends a record to an output file.
WRITE REPORT-RECORD.
WRITE REPORT-RECORD FROM PRINT-LINE.
WRITE REPORT-RECORD FROM HEADER-LINE AFTER PAGE.
WRITE REPORT-RECORD FROM DETAIL-LINE AFTER 1 LINE.
Use the output record name from the File Section after WRITE, not the file name itself.
CLOSE statement
CLOSE makes a file unavailable for further processing and releases it properly.
CLOSE STUDENT-FILE.
CLOSE STUDENT-FILE REPORT-FILE.
Do not write INPUT or OUTPUT in the CLOSE statement.
STOP RUN statement
STOP RUN ends program execution.
STOP RUN.
Arithmetic statements
COBOL has several arithmetic statements:
ADDSUBTRACTMULTIPLYDIVIDECOMPUTE
Fields used in arithmetic should be numeric, usually declared with PIC 9, optionally with S or V.
ADD
Format:
ADD data-field-1 TO data-field-2.
This adds the first value to the second and stores the result in the second field.
ADD SALARY TO TOTAL-SALARY.
ADD 1 TO RECORD-COUNT.
ADD QUIZ1 QUIZ2 QUIZ3 TO TOTAL-QUIZ.
With GIVING:
ADD QUIZ1 QUIZ2 GIVING TOTAL-QUIZ.
GIVING stores the result in a separate receiving field, so the source values do not need to be changed.
SUBTRACT
Format:
SUBTRACT data-field-1 FROM data-field-2.
This subtracts the first value from the second and stores the result in the second field.
SUBTRACT DISCOUNT FROM PRICE.
SUBTRACT TAX LOAN FROM GROSS-PAY.
With GIVING:
SUBTRACT DEDUCTION FROM GROSS-PAY GIVING NET-PAY.
MULTIPLY
Format:
MULTIPLY data-field-1 BY data-field-2.
This multiplies the first value by the second and stores the result in the second field.
MULTIPLY RATE BY HOURS.
With GIVING:
MULTIPLY RATE BY HOURS GIVING GROSS-PAY.
DIVIDE
Two common forms:
DIVIDE data-field-1 BY data-field-2.
DIVIDE data-field-1 INTO data-field-2.
With BY, the first field is divided by the second. With INTO, the second field is divided by the first. In the simple form, the quotient is stored in the dividend field.
With GIVING:
DIVIDE TOTAL-GRADE BY NUMBER-OF-GRADES GIVING AVERAGE-GRADE.
DIVIDE 12 INTO ANNUAL-SALES GIVING MONTHLY-SALES.
ROUNDED clause
Use ROUNDED when the receiving field has fewer decimal places than the result and you want rounding instead of simple truncation.
DIVIDE TOTAL-GRADE BY SUBJECT-COUNT
GIVING AVERAGE-GRADE ROUNDED.
REMAINDER clause
Use REMAINDER with DIVIDE ... GIVING to store the remainder.
DIVIDE TOTAL-ITEMS BY ITEMS-PER-BOX
GIVING FULL-BOXES
REMAINDER LEFTOVER-ITEMS.
ON SIZE ERROR clause
Use ON SIZE ERROR to handle a result too large for the receiving field.
MULTIPLY SALES BY RATE
GIVING COMMISSION
ON SIZE ERROR
MOVE 1 TO ERROR-SW.
COMPUTE statement
COMPUTE allows a full arithmetic expression in one statement.
COMPUTE TOTAL-GRADE = GRADE-1 + GRADE-2.
COMPUTE DISCOUNT = PRICE * DISCOUNT-RATE.
COMPUTE NET-SALES = GROSS-SALES - EXPENSES.
COMPUTE AVERAGE-GRADE = TOTAL-GRADE / SUBJECT-COUNT.
COMPUTE GROSS-PAY = RATE-PER-HOUR * HOURS-WORKED.
Order of operations:
- Parentheses
- Exponentiation, if supported by the compiler
- Multiplication and division
- Addition and subtraction
- Equal-priority operations from left to right
IF statement
IF executes statements when a condition is true.
IF GRADE > 74
MOVE "PASSED" TO REMARKS.
With ELSE:
IF TRANSACTION-CODE = "D"
ADD AMOUNT TO BALANCE
ELSE
SUBTRACT AMOUNT FROM BALANCE.
Relational operators
COBOL can compare values using words or symbols:
| Meaning | COBOL form |
|---|---|
| Equal | IS EQUAL TO or = |
| Not equal | IS NOT EQUAL TO or NOT = |
| Less than | IS LESS THAN or < |
| Not less than | IS NOT LESS THAN or NOT < |
| Greater than | IS GREATER THAN or > |
| Not greater than | IS NOT GREATER THAN or NOT > |
| Greater than or equal | IS GREATER THAN OR EQUAL TO or >= |
| Less than or equal | IS LESS THAN OR EQUAL TO or <= |
Class conditions
Class conditions test whether a field contains numeric or alphabetic data.
IF INPUT-AMOUNT IS NUMERIC
MOVE INPUT-AMOUNT TO AMOUNT.
IF CUSTOMER-NAME IS ALPHABETIC
MOVE CUSTOMER-NAME TO PRINT-NAME.
Sign conditions
Sign conditions test whether a numeric field is positive, negative, or zero.
IF BALANCE IS NEGATIVE
DISPLAY "ACCOUNT IS OVERDRAWN".
IF TOTAL IS ZERO
DISPLAY "NO RECORDS PROCESSED".
Condition names with level 88
Level 88 can name a condition for readability.
01 EOF-SW PIC X VALUE "N".
88 END-OF-FILE VALUE "Y".
88 NOT-END-OF-FILE VALUE "N".
Then the Procedure Division can say:
IF END-OF-FILE
DISPLAY "NO MORE RECORDS".
SET END-OF-FILE TO TRUE.
Compound conditions
Use AND and OR when more than one condition must be tested.
IF AGE >= 18 AND ACCOUNT-STATUS = "A"
MOVE "APPROVED" TO RESULT.
IF OPTION = "Y" OR OPTION = "y"
PERFORM PROCESS-RECORD.
PERFORM statement
PERFORM transfers control to another paragraph, executes it, and then returns.
Unconditional form:
PERFORM PRINT-HEADING.
Repeat a fixed number of times:
PERFORM PRINT-LINE 5 TIMES.
Perform a range of paragraphs:
PERFORM INITIALIZE-ROUTINE THRU INITIALIZE-END.
Repeat until a condition becomes true:
PERFORM PROCESS-RECORDS UNTIL EOF-SW = 1.
PERFORM with VARYING
Use PERFORM ... VARYING to repeat a paragraph while changing a counter or subscript.
PERFORM PRINT-QUIZ
VARYING I FROM 1 BY 1 UNTIL I > 5.
This means:
- Set
Ito 1. - Check whether
I > 5. - If false, perform the paragraph.
- Add 1 to
I. - Repeat until the condition becomes true.
Tables and OCCURS
A COBOL table is similar to an array. It stores repeated data items.
One-dimensional table:
01 QUIZ-SCORES.
05 QUIZ-SCORE OCCURS 5 TIMES PIC 99.
01 I PIC 9 VALUE 0.
01 TOTAL-QUIZ PIC 9(3) VALUE 0.
Processing example:
PERFORM ADD-QUIZ-SCORE
VARYING I FROM 1 BY 1 UNTIL I > 5.
ADD-QUIZ-SCORE.
ADD QUIZ-SCORE(I) TO TOTAL-QUIZ.
Rules:
OCCURSdefines how many elements the table has.- A subscript identifies which element is being used.
- The subscript is written in parentheses after the table item name.
- The subscript must be within the table's range.
Two-dimensional tables
A two-dimensional table uses two subscripts, commonly row and column.
Example:
01 STUDENT-COUNTS.
05 YEAR-LEVEL OCCURS 4 TIMES.
10 COURSE-COUNT OCCURS 2 TIMES PIC 999.
Access an element using two subscripts:
ADD COURSE-COUNT(I, J) TO YEAR-TOTAL(I).
Use nested PERFORM ... VARYING ... AFTER for two-dimensional processing:
PERFORM ADD-COUNTS
VARYING I FROM 1 BY 1 UNTIL I > 4
AFTER J FROM 1 BY 1 UNTIL J > 2.
In this pattern, one subscript represents the row and the other represents the column.
DISPLAY statement
DISPLAY prints output to the screen.
DISPLAY "Average Grade is: ".
DISPLAY AVERAGE-GRADE.
DISPLAY "Average Grade is: " AVERAGE-GRADE.
Some compilers support screen position syntax:
DISPLAY "Average Grade is: " LINE 5 COLUMN 15.
DISPLAY AVERAGE-GRADE LINE 5 COLUMN 40.
ACCEPT statement
ACCEPT gets input from the keyboard.
ACCEPT STUDENT-NAME.
ACCEPT MIDTERM-GRADE.
ACCEPT FINAL-GRADE.
Some compilers support screen position syntax:
ACCEPT STUDENT-NAME LINE 7 COLUMN 11.