Written by Colin Wheeler from various sources
These are notes that I have obtained from
various sources on how to write the syntax of COBOL programs. I initially
wrote this for myself but then decided to make this document public.
Feel free to re-distribute this but do not alter it. If I find any errors
on this page I will post the error and correct it and update the version
number which is why I wrote a version number above so you can keep
track of the updates. This page was written with Mozilla Composer v1.1a(Alpha)
and is generally the best browser to view this page with because Mozilla
supports more web standards than Internet Explorer. If you find anywhere
in this document that I have made an error please compose an e-mail to me
@ cdwheeler@mac.com with the subject "COBOL Error" and copy the section of
this page that has an error and paste it into the e-mail message and then
write what the error is then paste the code or section again and correct
it. I will attempt to correct any errors on this page as quickly as possible.
I know that i will probably make many errors but for me finding them
and correcting them is more important than anything else. If you think that
anything needs to be added to this guide please go to the COMING SOON page
and check to see if your idea is in the works if it's not then e-mail it
to me or if you think that one of my descriptions doesn't aqeately describe
the code or section it's refeering to then e-mail me and let me know if there
are enough people who have a problem with a section i will revise it. Please
note that this is the very first post of this webpage and it could very well
have a hundred errors and will eventually expand, but for now just notify
me of anything wrong with the page.
COBOL Bible v1.0.3 Coming Soon! Some of the fixes/new features are:
* Over 30 various spelling and format errors fixed!
* Fixed some various syntax errors present in the COBOL code
* Fixed the code on this page that caused some text to be disproportional in Internet Explorer (still no probs with Mozilla :-) )
* Finally started work to enhance the page for Mozilla/Netscape users (Mozilla 1.0 and above - Netscape 7.0 preview 1 and above)
* Finished Bolding all Procedure Division Statements
What's new in COBOL Bible v1.0.2 ALPHA? (Updated 12:10am Wednesday July 17)
* Completed Procedure Division Statements
* Added Evaluate Statement Syntax (was previously missing from list of commands)
* Corrected Miscelanious Errors on Page
* Corrected Font Code in HTML so the page can be viewed well on Internet
Explorer
Index of COBOL Notes:
I. Book ConventionsSELECT DISK-FILE
ASSIGN TO 'C:\Documents and Settings\User\file.dat'
ORGANIZATION IS LINE SEQUENTIAL.
In some cases I may show code that is like this...
When I put "..." on a line as such that means that you can put more code in the section as necessary. When I enter code like this "CARD-CODE" that means that it is a variable name or section of code that you can change as needed. Please note though that in some instances some variable names depend on another defined name at which I will note it within or after the code. Please note that with this document i am trying to provide the framework for your syntax and in a lot of cases you can move parts of the code I have provided around or substitute different statements other than the ones i have provided. These examples are here to show you ways of doing your syntax that you might not have even been aware you could do but they are hardly the limit of what you can do with your syntax . I encourage you to look up other websites that involve COBOL syntax examples and see what they have also. Knowledge is Power after all.CARD-FILE
RECORD CONTAINS 80 CHARACTERS
01 CARD-RECORD.
05 CARD-CODE PIC X(25).
...
...
05 CARD-NAME PIC X(15).
II. Compiler Directives
COBOL uses compiler directives to specify options that affect the way the
compiler behaves, what output is produced, what code is actually compiled,
and how the compiled code behaves when run. Compiler directives are set
by using the "$SET" statement in a Microfocus COBOL program. Microfocus
COBOL has default directives that can be overridden with the "$SET" statement.
When using the "$SET" statement it should be the first statement in the
program.
$SET statement in the Microfocus Environment
$SET OSVS NOFORM CHECKDIV"OSVS"
OSVS - Indicates the COBOL dialect used for compilation. The dialect will be IBM/OS/VS COBOL for most compilations unless the application is directed at a database or CICS environment.
NOFORM - Restricts the inserion of form feed characters into the program listing. Omitting NOFORM causes blank pages to be embedded in the printed program listing.
CHECKDIV"OSVS" - Causes a runtime error to occur if your program tries to divide by Zero in a statemtent that has no "ON SIZE ERROR" clause encoded.
The VSE and OS COBOL evironment also has default directives
that can be overridden or added to with a COBOL option statemtent called
a "CBL" statement. It must be coded as the first source statement in a VSE
or OS COBOL program.
III. SELECT Statements
Line Sequential disk file
SELECT DISK-FILERecord sequential disk file:
ASSIGN TO '/students/testdata/testfile.dat'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT DESK-FILEIndexed sequential disk file:
ASSIGN TO '/students/testdata/prtfile.prt'
ORGANIZATION IS RECORD SEQUENTIAL.
SELECT DISK-FILEPlease note that later on i will give more explanation to this section as for now i just want to post this information.
ASSIGN TO '/students/testdata/indxfile.dat'
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS INDEX-KEY
FILE STATUS IS RESPONSE-CODE.
FD CARD-FILEV. Picture and Value Clauses (Working Storage)
LABEL RECORD IS OMITTED
DATA RECORD IS CARD-RECORD CARD-REC
RECORD CONTAINS 80 CHARACTERS.
01 CARD-RECORD.
05 CARD-CODE PIC X.
... ...
10 CARD-HOUSE-NUM PIC X(25).
... ...
FD PRT-FILE
LABEL RECORD IS OMITTED
DATA RECORD IS PRT-RECORD
RECORD CONTAINS 132 CHARACTERS.
01 PRT-RECORD PIC X(132).
01 FLD1 PICTURE IS X.66 Level Items:
01 FLD2 PIC X USAGE IS DISPLAY.
** Note ** The PICTURE clause is normally abbreviated to PIC and the USAGE clause is normally omitted since the default usage
is DISPLAY.
01 H1.
05 PIC X VALUE SPACES. (There is no name here because this part of the line is blank
... ... ...
... ... ...
05 PIC X(12) VALUE "REPORT TITLE".
**Note** The number of characters on each line must equal the amount on the printer file
01 PERSONELL-REC.
05 PERS-NUM PIC 999.
05 PERS-NAME PIC X(25).
... ...
01 EMPLOYEE-RECORD.77 Level Items:
05 EMPLOYEE-LNAME PIC X(26).
05 EMPLOYEE-FNAME PIC X(26).
05 EMPLOYEE-BIRTH-DATE.
10 EMPLOYEE-BIRTH-DAY PIC 99.
10 EMPLOYEE-BIRTH-MON PIC 99.
66 EMPLOYEE-FULL-NAME RENAMES
EMPLOYEE-LNAME THRU EMPLOYEE-FNAME.
77 IO-STATUS PIC XX VALUE SPACES.88 Level Items:
77 IO-LITERAL PIC X(29) VALUE "TOTALS FOR SECTION".
01 WOR-REC.
05 WK-GRADE PIC 99.
88 K6 VALUE 1 THRU 6.
88 MIDSCHL VALUE 7 8.
88 HIGH-SCHOOL VALUE 9 THRU 12.
88 GRADE-ERROR VALUE 00 13 THRU 99.
All Procedure Division Statements must begin in column 12
or higher although in the new version of COBOL coming out shortly the column
requirements will go away and you will have the freedom to code wherever you
want. Currently paragraph names should begin in column 8 and should
be segnificant. Each paragraph must begin with the level number of the paragraph
(ex "1000-MAINLINE".) If a EXIT statement is necessary it should look like
such " 1000-X. EXIT."
Procedure Division Statements:
ACCEPT WORK-DATE FROM DATE.
**Note** : "WORK-DATE" should be defined in the working
storage and DATE is supplied by the operating system you are using ( Mac OS
X, Windows XP, etc.) ACCEPT causes the program to 'stop' and allows you to
enter data on console and will not continue with the execution of the program
until the user presses the Enter key. This allows you to communicate with
the terminal (user) and enter data into the program.
ACCEPT FILENAME FROM CONSOLE.
This statement will receive information into the program
and places it into a data-item defined in the program. FILENAME is desined
in the working storage section.
DISPLAY 'message here' UPON CONSOLE
** Note that the "UPON CONSOLE" section is not required
in this statement **
DISPLAY PAGE-CTRThis causes the value of PAGE-CTR to be displayed
(1)ADD SUB-BAL TO TOTAL-BAL.
(2)ADD SUB-BAL SLS-BAL SSLS-BAL TO TOT-BAL
(3)ADD SUB-BAL SLS-BAL SSLS-BAL GIVING TOT-BAL
(4)ADD INPUT-BAL TO TOT-BAL ROUNDED
ON SIZE ERROR
MOVE 0 TOT TOT-BAL
DISPLAY 'ERROR IN ADDITION'.
(1) Adds "SUB-BAL" to "TOTAL-BAL"
(2) Adds "SUB-BAL", "SLS-BAL","SSLS-BAL" to "TOT-BAL"
(3) Adds "SUB-BAL","SLS-BAL","SSLS-BAL" using the fiield defined for "TOT-BAL"
(4) Adds "INPUT-BAL" to "TOT-BAL" rounding the number. If the field is too
big it will give an error
- 3. CALL -
CALL 'PAYCAL' USING PARM1
PARM2
PARM3.
CLOSE PRT-FILE.- 5. COMPUTE -
CARD-FILE.
INPUT-FILE.
CLOSE PRT-FILE CARD-FILE INPUT-FILE.
(1)COMPUTE BALANCE=(INTRST * MBAL/SBAL)
(2)COMPUTE BALANCE=(INTRST * MBAL/SBAL) / 365.
(3)COMPUTE BALANCE ROUNDED = (INTRST * MPAY)/ INTRST
ON SIZE ERROR
MOVE 0 TO BALANCE
DISPLAY 'ERROR ON COMPUTE'.
- 6 . DELETE -
DELETE TABLE-FILE.
This form of the delete statement has to follow a sucsessful
read before the delete statement can be performed. If the read is not performed
then the delete statement will not be performed and in an interactive COBOL
program you would need to generate an error message.
- 7. DIVIDE -
(1)DIVIDE 100 INTO INT-RATE.- 8. EVALUATE -
(2)DIVIDE MONTHS INTO INT-RATE GIVING MONTH-RATE.
(3)DIVIDE INTO-RATE BY MONTHS GIVING MONTH-RATE.
EVALUATE YEARS-IN-COLLEGE-IN
WHEN 1 PERFORM 300-FRESHMAN-RTN
WHEN 2 PERFORM 400-SOPHMORE-TRN
WHEN 3 PERFORM 500-JUNIOR-RTN
WHEN 4 PERFORM 600-SENIOR-RTN
WHEN OTHER PERFORM 700-ERR-RTN
END EVALUATE
EXAMINE PRT-FLD REPLACING ALL ',' BY '/'.
- 9. IF... ELSE -
(1)IF RATE-CODE EQUAL 'A'
MOVE '9' TO RATE.
(2)IF RATE-CODE EQUAL 'A'
MOVE '9' TO RATE.
ELSE
MOVE '7' TO RATE.
WRITE PRT-REC FROM D1
AT EOP
PERFORM 4000-HDG.
GO TO 4000-EXIT.
10. If...Else If...Else
IF SALES LESS 50011. IF IF... ELSE ELSE
COMPUTE COMMISSSION = .02 * SALES
ELSE
IF SALES LESS 5000
COMPUTE COMISSION = .05 * SALES
ELSE
COMPUTE COMISSION = .1 * SALES.
IF SALES GREATER 499.9912. INSPECT
IF SALES GREATER 4999.99
COMPUTE COMISSION = .1 * SALES
...
ELSE
COMPUTE COMISSION = .05 * SALES
...
ELSE
COMPUTE COMISSION = .05 * SALES
-INSPECT FLD3 TALLYING CTR1 FOR ALL ','.13. MOVE
-INSPECT FLD3 REPLACING ALL ',' BY '/'.
MOVE INPUT-BAL TO PRT-BAL.14. MOVE CORRESPONDING
MOVE ZERO TO REC-CTR LINE-CTR PAGE-CTR
MOVE ZERO TO REC-CTR
LINE-CTR
PAGE-CTR.
01 CURR-DATE.15. MULTIPLY
05 DD PIC 99.
05 PIC X.
05 MM PIC 99.
05 PIC X.
05 YY PIC 9999.
01 SWT-DATE.
05 YYYY PIC 9999.
05 PIC X.
05 MM PIC 99.
05 PIC X.
05 DD PIC 99.
MOVE CORRESPONDING CURR-DATE TO SWT-DATE.
MOVE CORR CURR-DATE TO SWT-DATE.
**Note** If identically named field must be referenced, the group level of the field must be identified with an OF clause
ex:
MOVE DD OF CURR-DATE TO WORK-DD.
MULTIPLY INPUT-HRS BY INPUT-RATE.16. OPEN
MULTIPLY INPUT-HRS BY INPUT-RATE GIVING GROSS-PAY.
MULTIPLY INPUT-HRS BY INPUT-RATE
GIVING GROSS-PAY ROUNDED
ON SIZE ERROR
MOVE 'Y' TO ERROR-SWT
DISPLAY 'ERROR ON MULTIPLY'.
OPEN INPUT CARD-FILE OUTPUT PRT-FILE ISAM-FILE.17. PERFORM
OPEN INPUT CARD-FILE
TAPE-FILE
VSAM-FILE.
OPEN OUTPUT PRT-FILE.
PERFORM 2000-WRITE.18. PERFORM UNTIL:
PERFORM 2000-WRITE 4 TIMES.
PERFORM 2000-WRITE THRU 2000-X.
PERFORM 2000-PRT19. PERFORM VARYING:
UNTIL PAGE-CTR GREATER THAN 100.
PERFORM 2000-PRT THRU 2000-X.
UNTIL MORE-CARDS EQUAL 'N'.
PERFORM 4100-READ THRU 4100-X
UNTIL MORE-CARD EQUAL 'N'
OR CARD-CODE EQUAL 3.
PERFORM 4100-READ THRU 4100-X
UNTIL MORE-CARD EQUAL 'N'
OR CARD-CODE EQUAL 3
OR CARD-CTR GREATER 100.
PERFORM 1100-CLEAR20. PERFORM VARYING ...AFTER:
VARYING SUB FROM 1 BY 1
UNTIL SUB GREATER 100.
PERFORM 4100-PRT THRU 4100-X
VARYING EMP-NO FROM 10 BY -1
UNTIL LIST(EMP-NO) EQUAL 'ADAMSON'
OR EMP-NO EQUAL 1.
PERFORM 1100-FIND THRU 1100-X21. READ:
VARYING ROW-SUB FROM 1 BY 1
UNTIL ROW-SUB GREATER 10
AFTER COL-SUB FROM 1 BY 1
UNTIL COL-SUB GREATER 3
AFTER DEPTH-SUB FROM 1 BY 1
UNTIL DEPTH-SUB GREATER THAN 5.
READ CARD-FILERead Random Access - Indexed Sequential File - Primary Key
READ CARD-FILE INTO EMP-CARD.
READ CARD-FILE INTO EMP-CARD
AT END
MOVE 'N' TO MORE-RECS
GO TO 4100-X.
READ VSAM-FILE INTO EMP-CARDRead Random Access - Indexed Sequential File - Alternate Index
INVALID KEY
MOVE 'Y' TO ERROR-SWT
DISPLAY 'ERROR ON RANDOM READ'.
READ VSAM-FILE INTO EMP-CARD22. RELEASE:
KEY IS VSAM-ALTERNATE-KEY-FIELD
INVALID KEY
MOVE 'Y' TO ERROR-SWT
DISPLAY 'ERROR ON READOM READ'.
RELEASE SORT-RECORD.23. RETURN:
RELEASE SORT-RECORD FROM CARD-RECORD.
RETURN SORT-FILE.24. REWRITE:
RETURN SORT-FILE INTO WK-RECORD.
RETURN SORT-FILE INTO WK-RECORD
AT END
MOVE 'N' TO MORE-SORT-RECS
GO TO 1200-X.
REWRITE VSAM-REC.25. SEARCH:
REWRITE VSAM-REC FROM WK-REC.
REWRITE VSAM-REC FROM WK-REC
INVALID KEY
MOVE 'Y' TO ERROR-SWT
DISPLAY 'ERROR ON REWRITE'.
01 GRADE-AREA.26. SORT:
05 GRADE-TABLE OCCURS 25 TIMES INDEXED BY INDX.
10 GRADE-NAME PIC X(10).
10 GRADE-NUM PIC 999.
SET INDX TO 1.
SEARCH GRADE-TABLE
AT END
MOVE 'DONE' TO SEARCH-STATUS
WHEN WK-NAME GREATER GRADE-NAME(INDX)
MOVE 'FOUND' TO SEARCH-NAME-SWT.
SET INDX TO 1.
SEARCH GRADE-TABLE
AT END
MOVE 'DONE' TO SEARCH-STATUS
WHEN WK-NAME GREATER GRADE-NAME(INDX)
MOVE 'FOUND' TO SEARCH-NAME-SWT
WHEN WK-NUM EQUAL GRADE-NUM(INDX)
MOVE 'FOUND' TO SEARCH-NUM-SWT.
SORT SORT-FILE ASCENDING KEY SORT-SSN27. START:
SORT-LNAME
SORT-FNAME
USING INPUT-FILE
GIVING OUTPUT-FILE.
SORT SORT-FILE ASCENDING KEY SORT-SSN
SORT-LNAME
SORT-FNAME
DESCENDING KEY SORT-ACT-BAL
ASCENDING KEY SORT-TRAN-DATE
INPUT PROCEDURE IS 4100-READ THRU 4100-X
OUTPUT PROCEDURE IS 4200-PRT THRU 4200-X.
START VSAM-FILE28. STRING:
INVALID KEY
DISPLAY 'NO RECORD FOUND WITH MATCHING KEY'
START VSAM-FILE
KEY GREATER THAN EMP-NAME
INVALID KEY
DISPLAY 'NO RECORD FOUND WITH REQUESTED KEY'.
STRING 'A' 'B' 'C' DELMITED BY SIZE29. SUBTRACT:
INTO REC-FLD.
STRING FNAME DELMITED BY ' '
SPACE DELMITED BY SIZE
LNAME DELMITED BY SPACE
INTO PRT-NAME.
STRING FNAME
MNAME
SPACE DELMITED BY SIZE
LNAME DELMITED BY SPACE
INTO PRT-NAME
WITH POINTER BEGIN-POSITION
ON OVERFLOW
DISPLAY 'STRUNG TOO MUCH'.
SUBTRACT INPUT-BAL FROM TOT-BAL.30. TRANSFORM:
SUBTRACT INPUT-BAL INPUT-DEP INPUT-INT FROM TOT-BAL.
SUBTRACT INPUT-BAL FROM TOT-BAL ROUNDED
ON SIZE ERROR
MOVE 0 TO TOT-BAL
DISPLAY 'ERROR IN SUBTRACTION'.
TRANSFORM FLD3 CHARACTERS FROM ',' TO '-'31. WRITE
WRITE PRT-RECSequential Disk
AFTER ADVANCING 2 LINES.
WRITE PRT-REC FROM H1
AFTER AVDANCING PAGE.
WRITE PRT-REC
AT EOP
PERFORM 4100-HDGS.
WRITE PRT-REC
AFTER ADVANCING 4 LINES
AT EOP
PERFORM 4100-HDGS.
WRITE SEQ-DISK-REC FROM WK-RECIndexed Sequential Disk:
INVALID KEY
PERFORM 4300-ERR
GO TO 2000-X.
WRITE INDX-SEQ-DISK-REC FROM WK-REC
INVALID KEY
PERFORM 4300-ERR
GO TO 2000-X.