The document discusses the structure and components of a COBOL program. It describes the four main divisions:
1) Identification Division which names the program
2) Environment Division which describes the runtime environment
3) Data Division which defines data used in the program
4) Procedure Division which specifies the program logic
Within these divisions are sections, paragraphs and clauses that provide details like file definitions, configuration settings, and data descriptions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100%(1)100% found this document useful (1 vote)
307 views186 pages
Cobol Training New 1
The document discusses the structure and components of a COBOL program. It describes the four main divisions:
1) Identification Division which names the program
2) Environment Division which describes the runtime environment
3) Data Division which defines data used in the program
4) Procedure Division which specifies the program logic
Within these divisions are sections, paragraphs and clauses that provide details like file definitions, configuration settings, and data descriptions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 186
Text Text Text
Text Text Text
Text Text Text Text Text Text Naveen Kumar / Romi Rimesh 16/06/2008 ----------------------------------------------------------------------------------- COBOL/400 COBOL/400 - 1 - COBOL 400
Structure of COBOL program:
A COBOL Program consists of following 4 divisions:
IDENTIFICATION DIVISION ENVIRONMENT DIVISION DATA DIVISION PROCEDURE DIVISION
All the divisions must appear in the same order as shown above.
COBOL/400 - 2 - COBOL - DIVISION1
Identification Division
It names the program and may include the date the program was written, the date of compilation, and other such documentary information about the program.
INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT DISPLAY-FILE ASSIGN TO WORKSTATION-TESTDSPF ORGANIZATION IS TRANSACTION ACCESS IS DYNAMIC RELATIVE KEY IS RRN FILE STATUS IS FILE-STATUS.
COBOL/400 - 11 - Input-Output Section
Coding Example2:
INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT PHY-FILE1 ASSIGN TO DATABASE-PHYSICAL1 ORGANIZATION IS INDEXED ACCESS IS DYNAMIC RECORD KEY IS EXTERNALLY-DESCRIBED-KEY FILE STATUS IS WS-PHY-FILE1-STATUS.
COBOL/400 - 12 - Input-Output Section
SELECT Paragraph
Makes association between the file and the external medium
It consists of 3 parts: Device File Name Attribute
COBOL/400 - 13 - SELECT Paragraph DEVICE: Specifies the type of Device that the file will use It can be changed at run-time
List of Different Devices: Device File WORKSTATION Display File DATABASE Externally described Database File DISK Program described Database file FORMATFILE Externally described Printer file PRINTER Program described Printer file TAPEFILE Tape File DISKETTE Diskette File
COBOL/400 - 14 - SELECT Paragraph
FILE-NAME:
1 thru 10 char. system name of the actual external file
For Database files, a member name can not be specified in the COBOL program. If a particular member is to be specified, the OVRDBF command must be used at execution time to specify the member name
The file-name can be changed at execution time with the TOFILE parameter of the OVRxxxF command
COBOL/400 - 15 - SELECT Paragraph
ATTRIBUTE:
This part can be SI
SI indicates that a separate indicator area has been specified in the DDS for the FORMATFILE or WORKSTATION file
Sequential-Access Mode: Allows reading and writing records of a file in a serial manner; the order of reference is implicitly determined by the position of a record in the file
Random-Access Mode: Allows reading and writing records in a programmer-specified manner; the control of successive references to the file is expressed by specifically defined keys supplied by the user
Dynamic-Access Mode: Allows a specific I/O request to determine the access mode. Therefore, records may be processed sequentially and/or randomly
COBOL/400 - 25 - Input-Output Section
RECORD KEY Clause
It must be specified for an indexed file
Each record in the file must have a unique record key value
EXTERNALLY-DESCRIBED-KEY specifies that the key(s) for this file are same that are externally described in DDS
WITH DUPLICATES phrase can be specified for files assigned to DATABASE
COBOL/400 - 26 - Input-Output Section
RECORD KEY Clause
Note:
An error will be encountered at run-time, if
DUPLICATES is specified in COBOL program and file was created with UNIQUE specified in DDS
DUPLICATES is not specified in COBOL program and file was created allowing non unique keys
COBOL/400 - 27 - Input-Output Section
RELATIVE KEY Clause
It specifies the relative record number for a specific logical record within a relative file.
The POSITION parameter on the OVRDBF command can be used to set the file position indicator. This causes processing to begin with a record other than the first record.
COBOL/400 - 28 - Input-Output Section
FILE-STATUS Clause
It monitors the execution of each input-output request for the file
In some cases it indicates the I-O errors
It must be defined in Data Division
COBOL/400 - 29 - Input-Output Section
I-O-Control Paragraph
It is specified when checkpoints are to be taken and the storage areas to be shared by different files
This paragraph is optional in a COBOL program
COBOL/400 - 30 - Input-Output Section
SAME AREA Clause
It specifies that two or more files, that do not represent sort or merge files, are to use the same main storage area during processing
The files named in a SAME AREA clause need not have the same organization or access
The SAME AREA clause is syntax checked, but is treated as documentation
COBOL/400 - 31 - Input-Output Section
SAME RECORD AREA Clause
It specifies that two or more files are to use the same main storage area for processing the current logical record. All of the files may be open at the same tim
A logical record in the shared storage area is considered to be both of the following:
A logical record of each opened output file in the clause A logical record of the most recently read input file in the clause
This clause allows transfer of data from one file to another with no explicit data manipulation because the I/O record areas of named files are identical, and all are available to the user
COBOL/400 - 32 - Input-Output Section
SAME SORT AREA Clause
This clause optimizes the storage area assignment to a given SORT statement
When the SAME SORT AREA clause is specified, at least one file-name specified must name a sort file. Files that are not sort files may also be specified
COBOL/400 - 33 - Input-Output Section
SAME SORT AREA Clause
The following rules apply:
More than one SAME SORT AREA clause may be specified. However, a given sort file must not be named in more than one such clause
If a file that is not a sort file is named in both a SAME AREA clause and in one or more SAME SORT AREA clauses, all the files in the SAME AREA clause must also appear in that SAME SORT AREA clause
Files named in a SAME SORT AREA clause need not have the same organization or access
Files named in a SAME SORT AREA clause that are not sort files do not share storage with each other unless the user names them in a SAME AREA or SAME RECORD AREA clause
COBOL/400 - 34 - Input-Output Section
SAME SORT-MERGE AREA Clause
This clause optimizes the storage area assignment to a given SORT or MERGE statement
When the SAME SORT-MERGE AREA clause is specified, at least one file-name specified must name a sort or merge file. Files that are not sort or merge files may also be specified
COBOL/400 - 35 - Input-Output Section
SAME SORT-MERGE AREA Clause
The following rules apply:
More than one SAME SORT-MERGE AREA clause may be specified. However, a given sort or merge file must not be named in more than one such clause If a file that is not a sort or merge file is named in both a SAME AREA clause and in one or more SAME SORT-MERGE AREA clauses, all the files in the SAME AREA clause must also appear in that SAME SORT-MERGE AREA clause Files named in a SAME SORT-MERGE AREA clause need not have the same organization or access Files named in a SAME SORT-MERGE AREA clause that are not sort or merge files do not share storage with each other unless the user names them in a SAME AREA or SAME RECORD AREA clause
COBOL/400 - 36 - Input-Output Section
COMMITMENT CONTROL Clause
This clause specifies the files that will be placed under commitment control when they are opened. These files will be affected by the COMMIT and ROLLBACK statements
The COMMIT statement allows the synchronization of changes to database records while preventing other jobs from modifying those records until the COMMIT is complete
The ROLLBACK statement provides a method of canceling changes made to database files when those changes should not be made permanent
COBOL/400 - 37 - Input-Output Section
COMMITMENT CONTROL Clause
This clause can specify only files assigned to a device type of DATABASE
Files under commitment control may have an organization of sequential, relative or indexed, and may have any access mode valid for a particular organization
The system locks records contained in files under commitment control when these records are accessed. Records remain locked until released by a COMMIT or ROLLBACK statement
COBOL/400 - 38 - COBOL - DIVISION3
Data Division
The Data Division of a COBOL source program describes, in a structured manner, all the data to be processed by the object program
This section outlines the structure of the Data Division and explains the types of data and how they are related in a COBOL program
COBOL/400 - 39 - Data Division
File Section Describes externally stored data (including sort-merge files)
Working-Storage Section Describes internal data
Linkage Section Describes data made available by another program.
COBOL/400 - 40 - Data Division
Coding Example: DATA DIVISION. FILE SECTION. FD DISPLAY-FILE. 01 DISPLAY-REC COPY DD-ALL-FORMATS OF TESTDSPF.
FD PHY-FILE1. 01 PHY-FILE1-RCD. COPY DDS-ALL-FORMATS OF PHYSICAL1.
FD PHY-FILE2. 01 PHY-FILE2-REC. COPY DDSR-ALL-FORMATS OF PHYSICAL2.
COBOL/400 - 41 - Data Division
WORKING-STORAGE SECTION. FD DISPLAY-FILE. 01 PHYSICAL2-PARMS. 05 PHYSICAL2-PRODCD PIC X(04). 05 PHYSICAL2-INV PIC S9(03). 05 PHYSICAL2-STATUS PIC X(01). 88 PHYSICAL2-NONE-FOUND VALUE "0". 88 PHYSICAL2-ALL-VERIFIED VALUE "1". 88 PHYSICAL2-UNVERIFIED VALUE "2". 77 ERR-MESSAGE PIC X(80) VALUE SPACES.
COBOL/400 - 42 - Data Division
LINKAGE SECTION. 01 FUNCTION-MEMBER. 05 FUNCTION-CODE PIC X. 88 END-OF-PGM VALUE SPACES. 88 ENTRY-OPERATION VALUE "E". 88 VERIFY-OPERATION VALUE "V". 05 MEMBER-CODE PIC X. 88 ENTRY-MEMBER VALUE "E". 88 CLUSTER-MEMBER VALUE "C". 01 USER-ID PIC X(10). 01 JOB-NUMBER PIC X(10).
COBOL/400 - 43 - File Section
For all files except sort/merge, the File Section must contain an FD (File Description) entry
For each sort or merge file, the File Section must contain an SD (Sort Description) entry
COBOL/400 - 44 - File Section
file-description-entry
Represent the highest level of organization in the File Section
Provide information about the physical structure and identification of a file, and gives the record- name(s) associated with that file
COBOL/400 - 45 - File Section
record-description-entry
A set of data description entries that describe the particular record(s) contained within a particular file
More than one record description entry may be specified; each is an alternative description of the same record storage area
Data areas described in the File Section are not available for processing unless the file containing the data area is open
The initial value of a data item in the File Section is undefined
COBOL/400 - 46 - File Section
file-name
Must follow the level indicator (FD or SD), and must be the same as that specified in the associated SELECT clause
The file-name must be unique within this program
One or more record description entries must follow the file-name. When more than one record description entry is specified, each entry implies a redefinition of the same storage area
The clauses that follow file-name are optional; they may appear in any order
COBOL/400 - 47 - File Section
BLOCK CONTAINS Clause
Specify the size of the physical records
If the records in the file are not blocked, the clause may be omitted
This clause is always active for tape files, but is syntax-checked only for diskette files
COBOL/400 - 48 - File Section
RECORDS
Specify the number of logical records contained in each physical record
Maximum record size is 32766; maximum block size is 32766
COBOL/400 - 49 - File Section
RECORD CONTAINS Clause
Specify the number of character positions for fixed length records
COBOL/400 - 50 - File Section
RECORD CONTAINS Clause
Specify the number of character positions for fixed length records
When the record length determined from the record description entries does not match the length specified in the RECORD CONTAINS clause, the former will be used
COBOL/400 - 51 - File Section
LABEL RECORDS Clause
Indicate the presence or absence of labels
If it is not specified for a file, label records for that file must conform to the system label specifications
Generally treated as documentation
STANDARD Labels conforming to system specifications exist for this file
OMITTED No labels exist for this file
Note: The LABEL RECORDS clause is an obsolete element and is to be deleted from the next revision of the ANSI Standard
COBOL/400 - 52 - File Section
VALUE OF Clause
Describe an item in the label records associated with this file
The clause is syntax checked, then treated as documentation
Note: The VALUE OF clause is an obsolete element and will be deleted from the next revision of the ANSI Standard
COBOL/400 - 53 - Data Types
File Data (External Data) File data is contained in files
Program Data (Internal Data) Program data is created by the program itself
COBOL/400 - 54 - Data Types
Data Relationships
The relationships among all data to be used in a program are defined in the Data Division, through a system of level indicators and level-numbers
A level indicator, with its descriptive entry, identifies each file in a program. Level indicators represent the highest level of any data hierarchy with which they are associated; FD is the file description level indicator and SD is the sort-merge file description level indicator
A level-number, with its descriptive entry, indicates the properties of specific data
COBOL/400 - 55 - Data Types
Levels of Data
After a record has been defined, it can be subdivided to provide more detailed data references
A system of level-numbers specifies the organization of elementary and group items into records. Special level- numbers are also used; they identify data items used for special purposes
COBOL/400 - 56 - Data Types
A level-number is a 1- or 2-digit integer between 01 and 49, or one of three special level-numbers: 66, 77, or 88
The following level-numbers are used to structure records:
01 Specify the record itself, and is the most inclusive level- number possible. A level-01 entry may be either a group item or an elementary item
02-49 Specify group and elementary items within a record
COBOL/400 - 57 - Data Types
Special Level-Numbers
Special level-numbers identify items that do not structure a record. The special level-numbers are:
66 Identifies items that must contain a RENAMES clause; such items regroup previously defined data items
77 Identifies data item description entries independent Working- Storage or Linkage Section items that are not subdivisions of other items, and are not sub-divided themselves
88 Identifies any condition-name entry that is associated with a particular value of a conditional variable
COBOL/400 - 58 - Data Types
BLANK WHEN ZERO Clause
Specify that an item contains nothing but spaces when its value is zero
Specify only for elementary numeric or numeric-edited items
These items must be described, either implicitly or explicitly, as USAGE IS DISPLAY. When this clause is specified for a numeric item, the item is considered a numeric-edited item
Must not be specified for level-66 or level-88 items
Not allowed for items described with the USAGE IS INDEX clause or the USAGE IS POINTER clause
COBOL/400 - 59 - Data Types
JUSTIFIED Clause
Override standard positioning rules for a receiving item of the alphabetic or alphanumeric categories
May be specified only at elementary level. JUST is an abbreviation for JUSTIFIED, and has the same meaning. RIGHT is an optional word and has no effect on the execution of the program
Cannot be specified for numeric, numeric-edited, or alphanumeric-edited items
Must not be specified with level-66 (RENAMES) and level-88 (condition-name) entries
Define the PICTURE, USAGE, and SIGN characteristics of a data item by copying them from a previously defined data item
Make the length of the data item you define different from the length of the original item
Cause the new data item to inherit specific characteristics from the existing data item. These characteristics are the PICTURE, USAGE, SIGN, and BLANK WHEN ZERO attributes of the existing item
COBOL/400 - 62 - Data Types
Coding Example:
01 EMPLOYEE-RECORDS PIC 1. 88 MORE-EMPLOYEE-RECORDS VALUE B"1". 88 NO-MORE-EMPLOYEE-RECORDS VALUE B"0".
01 NEW- EMPLOYEE -REC. 05 NEW-EMPLOYEE-RECORD LIKE EMPLOYEE-RECORD.
01 WS-CNT1 PIC S9(3). 01 WS-CNT2 Like WS-CNT1.
COBOL/400 - 63 - Data Types
PICTURE Clause
Specify the general characteristics and editing requirements of an elementary item
Must be specified for every elementary item except an index data item or the subject of the RENAMES clause
The PICTURE character-string may contain a max of 30 chars
To use different data description entries to describe the same computer storage area
COBOL/400 - 66 - Data Types
Coding Example:
01 TODAYS-DATE. 05 DATE-YY PIC XX. 05 DATE-MM PIC XX. 05 DATE-DD PIC XX. 01 TODAYS-DATE-HOLD REDEFINES TODAYS-DATE PIC 9(6).
01 TODAYS-DATE PIC X(6). 01 TODAYS-DATE-HOLD REDEFINES TODAYS-DATE. 05 DATE-YY PIC XX. 05 DATE-MM PIC XX. 05 DATE-DD PIC XX.
COBOL/400 - 67 - Data Types
RENAMES Clause
Specify alternative, possibly overlapping, groupings of elementary data items
The special level-number 66 must be specified for data description entries that contain the RENAMES clause
One or more RENAMES entries can be written for a logical record
COBOL/400 - 68 - Data Types
Coding Example:
INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE ASSIGN TO DATABASE-EMPLOYEE ORGANIZATION IS INDEXED ACCESS IS DYNAMIC RECORD KEY IS EMPID FILE STATUS IS WS-EMPLOYEE-STATUS.
FILE SECTION. FD EMPLOYEE 01 EMP-REC. COPY DDS-EMPREC OF EMPLOYEE. 66 EMPID RENAMES EMPNO IN EMP-REC THRU DOJ IN EMP-REC.
COBOL/400 - 69 - Data Types
Coding Example:
MOVE WS-EMPNO TO EMPNO OF EMP-REC. MOVE WS-DOJ TO DOJ OF EMP-REC.
IF WS-EMPNO > ZERO THEN SET ONE-EMPOYEE TO TRUE.
START EMPLOYEE KEY NOT < EMPID INVALID KEY MOVE Invalid Emp No. TO WS-ERR.MSG END-START.
COBOL/400 - 70 - Data Types
SIGN Clause
Specify the position and mode of representation of the operational sign for a numeric entry
COBOL/400 - 71 - Data Types
USAGE Clause
Specify for an entry at any level. However, if it is specified at the group level, it applies to each elementary item in the group
COBOL/400 - 72 - Data Types
Coding Example:
01 EMPLOYEE-WORK-FIELDS. 05 EMPLOYEE-FILE-STATUS PIC X(2). 05 EMPLOYEE-END-OF-PAGE PIC X(1) VALUE "N". 05 EMPLOYEE-SALARY PIC 9(7) USAGE PACKED-DECIMAL VALUE ZERO.
The maximum length of a computational item is 18 decimal digits
COBOL/400 - 74 - Data Types
BINARY
Specified for binary data items
PACKED-DECIMAL
Specified for internal decimal items
COMPUTATIONAL or COMP
Specified for internal decimal items
COMPUTATIONAL-3 or COMP-3 (internal decimal)
This is the equivalent of PACKED-DECIMAL
COMPUTATIONAL-4 or COMP-4 (binary)
This is the equivalent of BINARY
COBOL/400 - 75 - Data Types
VALUE Clause
Specify the initial contents of a data item or the value(s) associated with a condition-name
Use of the VALUE clause differs depending on the Data Division section in which it is specified; In the File and Linkage sections, the VALUE clause must be used only in condition-name entries; in the Working-Storage Section, the VALUE clause may be used in condition-name entries, or in specifying the initial value of any data item
The data item assumes the specified value at the beginning of program execution.
If the initial value is not explicitly specified, it is unpredictable
COBOL/400 - 76 - Working-Storage Section
The Working-Storage Section describes data records that are not part of external data files but are developed and processed internally
The Working-Storage Section contains record description entries and data description entries for independent data items, called data item description entries
COBOL/400 - 77 - Working-Storage Section
record-description-entry
Data entries in the Working-Storage Section that bear a definite hierarchic relationship to one another must be grouped into records structured by level number
COBOL/400 - 78 - Working-Storage Section
data-item-description-entry
Independent items in the Working-Storage Section that bear no hierarchic relationship to one another need not be grouped into records, provided that they do not need to be further subdivided. Instead, they are classified and defined as independent elementary items. Each is defined in a separate data-item description entry that begins with either the level number 77 or 01
COBOL/400 - 79 - Working-Storage Section
The initial value of any data item in the Working-Storage Section, except an index data item, is specified by associating a VALUE clause with the item
The initial value of any index data item, or of any data item not associated with a VALUE clause, is undefined
COBOL/400 - 80 - Linkage Section
Describe data made available from another program
Record description entries and data item description entries in the Linkage Section provide names and descriptions, but storage within the program is not reserved because the data area exists elsewhere
Any data description clause may be used to describe items in the Linkage Section, with one exception: the VALUE clause may not be specified for items other than level-88 items
Consists of optional declaratives, and procedures that contain sections and/or paragraphs, sentences, and statements
COBOL/400 - 83 - Procedure Division
Procedure Division Header
The USING Phrase
Required only if the object program is to be invoked by a CALL statement and that statement includes a USING phrase
Makes data items defined in a calling program available to a called subprogram
Each USING identifier must be defined as a level-01 or level-77 item in the Linkage Section of the called subprogram
Must not contain a REDEFINES clause
Maximum number of data-names - 30
COBOL/400 - 84 - Procedure Division
Declaratives
Provide one or more special-purpose sections that are executed when an exceptional condition occurs
When Declarative Sections are specified, they must be grouped at the beginning of the Procedure Division, and the entire Procedure Division must be divided into sections
COBOL/400 - 85 - Procedure Division
Procedures
Within the Procedure Division, a procedure consists of:
A section or a group of sections A paragraph or group of paragraphs
A procedure-name is a user-defined name that identifies a section or a paragraph
COBOL/400 - 86 - Procedure Division
Section
A section header optionally followed by one or more paragraphs
Section-header
A section-name followed by:
the keyword SECTION
an optional segment-number
a separator period
COBOL/400 - 87 - Procedure Division
Section-name
A user-defined word that identifies a section
If referenced, a section-name must be unique within the program in which it is defined
COBOL/400 - 88 - Procedure Division
Paragraph
A paragraph-name followed by a separator period, optionally followed by one or more sentences
Paragraph-name
A user-defined word that identifies a paragraph
COBOL/400 - 89 - Procedure Division
Sentence
One or more statements terminated by a separator period
Statement
A syntactically valid combination of identifiers and symbols beginning with a COBOL verb
COBOL/400 - 90 - Procedure Division
Identifier
A syntactically correct combination of a data name
In any Procedure Division reference, the contents of an identifier must be compatible with the class specified through its PICTURE clause, or results are unpredictable
COBOL/400 - 91 - Procedure Division
Coding Example:
PROCEDURE DIVISION.
DECLARATIVES.
ERROR-IT SECTION. USE AFTER STANDARD ERROR PROCEDURE ON INPUT-DATA. ERROR-ROUTINE. IF CHECK-IT = "30" ADD 1 TO DECLARATIVE-ERRORS. END DECLARATIVES.
COBOL/400 - 92 - Procedure Division
Common Phrases
There are several phrases common to arithmetic and data manipulation statements; these phrases are described below
CORRESPONDING Phrase
Allows ADD, SUBTRACT, and MOVE operations to be performed on elementary data items of the same name if the group items to which they belong are specified
Both identifiers following the key word CORRESPONDING must name group items
COBOL/400 - 93 - Procedure Division
Coding Example:
MOVE CORRESPONDING DATE-YYMMDD TO DATE-WITH-SLASHES.
01 DATE-YYMMDD. 05 YY PIC XX. 05 MM PIC XX. 05 DD PIC XX.
01 DATE-WITH-SLASHES. 05 MM PIC XX. 05 SLASHA PIC X VALUE "/". 05 DD PIC XX. 05 SLASHB PIC X VALUE "/". 05 YY PIC XX.
COBOL/400 - 94 - Procedure Division
GIVING Phrase
The value of the identifier that follows the word GIVING is set equal to the calculated result of the arithmetic operation
Because this identifier is not involved in the computation, it may be a numeric edited item
COBOL/400 - 95 - Procedure Division
Coding Example:
SUBTRACT SUB1 FROM SUB2 GIVING RESULT.
DIVIDE NUM1 BY 10 GIVING RESULT.
COBOL/400 - 96 - Procedure Division
ROUNDED Phrase
After decimal point alignment, the number of places in the fraction of the result of an arithmetic operation is compared with the number of places provided for the fraction of the resultant identifier
When the size of the fractional result exceeds the number of places provided for its storage, truncation occurs unless ROUNDED is specified. When ROUNDED is specified, the least significant digit of the resultant identifier is increased by 1 when-ever the most significant digit of the excess is greater than or equal to 5
COBOL/400 - 97 - Procedure Division
Coding Example:
DIVIDE NUM1 BY 10 GIVING RESULT ROUNDED.
COMPUTE RESULT ROUNDED = NUM1 * NUM2.
COBOL/400 - 98 - Procedure Division
SIZE ERROR Phrases
A size error condition can occur in three different ways:
When the absolute value of the result of an arithmetic evaluation, after decimal point alignment, exceeds the largest value that can be contained in the result field
When division by zero occurs
In an exponential expression, when
Zero raised to the exponent Zero Zero raised to a negative exponent A negative number raised to a fractional exponent
The size error condition applies only to final results, not to any intermediate results
COBOL/400 - 99 - Procedure Division
Coding Example:
ADD 1 TO NUM1 ON SIZE ERROR MOVE 0 TO NUM1
COBOL/400 - 100 - Procedure Division
Input-Output Statements
Transfer data to and from files stored on external media
Control low-volume data that is obtained from or sent to an input/output device
Common Processing Facilities
Applied to more than one input-output statement:
Status key INVALID KEY condition INTO/FROM identifier phrase File position indicator
COBOL/400 - 101 - Procedure Division
Status Key
If the FILE STATUS clause is specified in the FILE-CONTROL entry, a value is placed in the specified status key (the 2-character data item named in the FILE STATUS clause) during execution of any request on that file, which indicates the status of that request
The value is placed in the status key before execution of any EXCEPTION/ERROR declarative or INVALID KEY/AT END phrase associated with the request
COBOL/400 - 102 - Procedure Division
Coding Example:
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT PROJECT-FILE ASSIGN TO DATABASE_PROJECT ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS EXTERNALLY-DESCRIBED-KEY FILE STATUS IS WS-PROJECT-FILE-STATUS.
DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-PROJECT-FILE-STATUS PIC XX. 88 WS-IO-SUCCESSFUL VALUE "00" THRU "09". 88 WS-END-OF-FILE VALUE "10" THRU "19". 88 WS-INVALID-KEY VALUE "20" THRU "29". 88 WS-IO-ERROR VALUE "30" THRU "99".
COBOL/400 - 103 - Procedure Division
INVALID KEY Condition
Can occur during execution of a START, READ, WRITE, REWRITE, or DELETE statement
When occurs, the input-output statement that caused the condition is unsuccessful
COBOL/400 - 104 - Procedure Division
Coding Example:
READ PROJECT RECORD INVALID KEY MOVE "N" TO PROJECT-RECORD-FOUND NOT INVALID KEY MOVE "Y" TO PROJECT-RECORD-FOUND END-READ.
IF WS-PROJECT-FILE-STATUS >= "30" MOVE "N" TO PROJECT-RECORD-FOUND END-IF.
COBOL/400 - 105 - Procedure Division
INTO/FROM Identifier Phrase
Valid for READ, RETURN, RELEASE, REWRITE, and WRITE statements
The identifier specified must be the name of an entry in the Working-Storage Section or the Linkage Section, or of a record description for another previously opened file
Record-name/file-name and identifier must not refer to the same storage area
The INTO phrase may be specified in a READ or RETURN statement
The FROM phrase may be specified in a RELEASE, REWRITE, or WRITE statement
COBOL/400 - 106 - Procedure Division
Coding Example:
READ PROJECT RECORD INTO WS-PROJECT FORMAT IS "PROJECT1" INDICATORS ARE WS-INDICATOR-TABLE END-READ.
REWRITE SUBFILE PROJECT-RCD FROM WS-PROJECT1SFL-O FORMAT IS PROJECT11SFL" INDICATORS ARE WS-INDICATOR-TABLE INVALID KEY CONTINUE END-REWRITE.
COBOL/400 - 107 - Procedure Division
Procedure Division Statements
ACCEPT Statement
Transfer data into the specified identifier
No editing or error checking of the incoming data
COBOL/400 - 108 - Procedure Division
Coding Example:
ACCEPT SYSTEM-DATE FROM DATE.
ACCEPT SYSTEM-TIME FROM TIME.
COBOL/400 - 109 - Procedure Division
ACQUIRE Statement
Acquire a program device for a TRANSACTION file
COBOL/400 - 110 - Procedure Division
ADD Statement
Sum two or more numeric operands and stores the result
COBOL/400 - 111 - Procedure Division
Coding Example:
ADD 1 TO PROJECT-COUNTER.
COBOL/400 - 112 - Procedure Division
CALL Statement
Transfer control from one object program to another within the run unit
Pass control to the first non-declarative instruction of the called subprogram
Called subprograms themselves can contain CALL statements, but a called subprogram that contains a CALL statement that directly or indirectly calls the calling program gives unpredictable results
COBOL/400 - 113 - Procedure Division
Coding Example:
CALL "XXXVALIDDT" USING DATE-CHECK.
COBOL/400 - 114 - Procedure Division
CANCEL Statement
Ensure that the next time the referenced subprogram is called it will be entered in its initial state
Close all open files
Operate only on the program specified, and not on any program that may have been called by the canceled program
No action is taken when a CANCEL statement is executed, naming a program that has not been called in the run unit, or that names a program that was called and subsequently canceled. In either case, control passes to the next statement
If names a program that does not exist in the library list, an error message is issued
COBOL/400 - 115 - Procedure Division
Coding Example:
CANCEL "XXXVALIDDT.
COBOL/400 - 116 - Procedure Division
CLOSE Statement
Terminate the processing of volumes and files, with optional rewind and/or lock or removal, where applicable
Terminate the processing of volumes and files, with optional rewind and/or lock or removal, where applicable
If the file is in an open status and the execution of a CLOSE statement is unsuccessful, the EXCEPTION/ERROR procedure (if specified) for this file is executed
If a CLOSE statement for an open file is not processed before a STOP RUN for this run unit, the file is implicitly closed
COBOL/400 - 117 - Procedure Division
Coding Example:
CLOSE PROJECT.
COBOL/400 - 118 - Procedure Division
COMMIT Statement
Provide a way of synchronizing changes to data base records while preventing other jobs from modifying those records until the COMMIT is performed
All record locks held by the job since the last commitment boundary for files under commitment control are released and the records become available to other jobs
Only affect files under commitment control
If a COMMIT is executed and there are no files opened under commitment control, the COMMIT statement has no effect and no commitment boundary is established
COBOL/400 - 119 - Procedure Division
COMPUTE Statement
Assign the value of an arithmetic expression to one or more data items
With the COMPUTE statement, arithmetic operations can be combined without the restrictions on receiving data items imposed by the rules for the ADD, SUBTRACT, MULTIPLY, and DIVIDE statements
COBOL/400 - 120 - Procedure Division
Coding Example:
COMPUTE RESULT = NUM1 - 1.
COBOL/400 - 121 - Procedure Division
CONTINUE Statement
Allow to specify a no operation statement
Indicate that no executable instruction is present
May be used anywhere a conditional statement
Has no effect on the execution of the program
COBOL/400 - 122 - Procedure Division
Coding Example:
READ PROJECT INVALID KEY CONTINUE END-READ.
COBOL/400 - 123 - Procedure Division
DELETE Statement
Remove a record from an indexed or relative file
For indexed files, the key may then be reused for record addition For relative files, the space is then available for a new record with the same RELATIVE KEY value
File must be open in I-O mode
Record is logically removed from the file and can no longer be accessed
COBOL/400 - 124 - Procedure Division
Coding Example:
DELETE PROJECT-FILE RECORD INVALID KEY MOVE "N" TO WS-PROJRCT-RECORD-FOUND NOT INVALID KEY MOVE "Y" TO WS-PROJECT-RECORD-FOUND END-DELETE
COBOL/400 - 125 - Procedure Division
DISPLAY Statement
Transfer the contents of each operand to the output device
Contents are displayed on the output device in the order, left to right, in which the operands are listed
COBOL/400 - 126 - Procedure Division
Coding Example:
DISPLAY IO-ERR-MSG UPON CNSL. DISPLAY USER-MSG.
SPECIAL-NAMES. SYSTEM-CONSOLE IS CNSL.
COBOL/400 - 127 - Procedure Division
DIVIDE Statement
Divide one numeric data item into or by one or more others, and sets the values of data items equal to the quotient and remainder
COBOL/400 - 128 - Procedure Division
Coding Example:
DIVIDE WS-HOURS BY 24 GIVING WS-DAYS REMAINDER WS-REMAINDER-HOURS.
COBOL/400 - 129 - Procedure Division
EVALUATE Statement
Provide a shorthand notation for a series of nested IF statements
COBOL/400 - 130 - IF (MENU-INPUT = 0") THEN PERFORM INIT-PROC ELSE IF (MENU-INPUT >= "1") AND (MENU-INPUT <= "9") THEN PERFORM PROCESS-PROC ELSE IF (MENU-INPUT = "R") THEN PERFORM READ-PARMS ELSE IF (MENU-INPUT = "X") THEN PERFORM CLEANUP-PROC ELSE PERFORM ERROR-PROC END-IF END-IF END-IF END-IF. EVALUATE MENU-INPUT WHEN 0" PERFORM INIT-PROC WHEN "1" THRU "9" PERFORM PROCESS-PROC WHEN "R" PERFORM READ-PARMS WHEN "X" PERFORM CLEANUP-PROC WHEN OTHER PERFORM ERROR-PROC END-EVALUATE. COBOL/400 - 131 - Procedure Division
EXIT Statement
Provide a common end point for a series of paragraphs
Specify the end of a called program and returns control to the calling program
When there is no next executable statement in a called program, an implicit EXIT PROGRAM statement is executed
COBOL/400 - 134 - Procedure Division
Coding Example:
END-OF-JOB. IF NO-PROJ-ERROR THEN CLOSE PROJECT. IF NO-DISPLAY-ERROR THEN CLOSE DISPLAY-FILE. EOJ-EXIT. EXIT PROGRAM. STOP RUN.
COBOL/400 - 135 - Procedure Division
GOBACK Statement
Function like the EXIT PROGRAM statement when it is coded as part of a program that is a subprogram in a COBOL run unit, and like the STOP RUN statement when coded in a program that is a main program in a COBOL run unit
Specify the logical end of a program
COBOL/400 - 136 - Procedure Division
Coding Example:
PROCEDURE DIVISION USING LINK-PARMS. 100-MAIN. IF PROJECT-FOUND = Y" PERFORM 150-PARA1 ELSE PERFORM 250-PARA2 END-IF. GOBACK. 100-EXIT. EXIT.
COBOL/400 - 137 - Procedure Division
GO TO Statement
Transfer control from one part of the Procedure Division to another
Three types of GO TO statements:
1. Unconditional
2. Conditional
3. Altered (Obsolete)
COBOL/400 - 138 - Procedure Division
Coding Example:
IF PROJECT1 NOT = PROJECT2 THEN GO TO 220-READ-NEXT-PROJECT END-IF
COBOL/400 - 139 - Procedure Division
IF Statement
Evaluate a condition and provides for alternative actions in the object program, depending on the evaluation
COBOL/400 - 140 - Procedure Division
Coding Example:
IF (MENU-INPUT >= "1") AND (MENU-INPUT <= "9") THEN PERFORM PROCESS-PROC ELSE IF (MENU-INPUT = "R") THEN PERFORM READ-PARMS END-IF END-IF.
COBOL/400 - 141 - Procedure Division
INITIALIZE Statement
Sets selected categories of data fields to predetermined values
Functionally equivalent to one or more MOVE statements
COBOL/400 - 142 - Procedure Division
Coding Example:
INITIALIZE PROJECT1-FIELD1.
INITIALIZE PROJECT1.
COBOL/400 - 143 - Procedure Division
INSPECT Statement
Specify that characters in a data item are to be counted (tallied) or replaced or both
Count the occurrence of a specific character (alphabetic, numeric, or special character) in a data item
Fill all or portions of a data item with spaces or zeros
Translate characters from one collating sequence to another
COBOL/400 - 144 - Procedure Division
Coding Example:
INSPECT WS-PAGE-IN-ERROR REPLACING LEADING ZEROS BY SPACES.
INSPECT PROJECT1-FIELD1 TALLYING WS-SPACE-CNTR FOR ALL .
COBOL/400 - 145 - Procedure Division
MERGE Statement
Combine two or more identically sequenced files (that is, files that have already been sorted according to an identical set of ascending/ descending keys) on one or more keys and makes records available in merged order to an output procedure or output file
May appear anywhere in the Procedure Division except in a Declarative Section
COBOL/400 - 146 - Procedure Division
MOVE Statement
Transfer data from one area of storage to one or more other areas
COBOL/400 - 147 - Procedure Division
Coding Example:
MOVE NUM1 TO NUM2
MOVE "MISSING OR INVALID VALUE" TO WS-ERROR-MESSAGE.
COBOL/400 - 148 - Procedure Division
MULTIPLY Statement
Multiplies numeric items and sets the values of data items equal to the results
At least one of the phrases, INPUT, OUTPUT, I-O, or EXTEND, must be specified with the OPEN keyword The INPUT, OUTPUT, I-O, and EXTEND phrases may appear in any order
Indexed and Relative Files
At least one of the phrases, INPUT, OUTPUT, or I-O, must be specified with the OPEN keyword. The INPUT, OUTPUT, and I-O phrases may appear in any order
Transaction Files
A TRANSACTION file must be opened in the I-O mode
COBOL/400 - 152 - Procedure Division
Coding Example:
OPEN I-O FILE1. OPEN INPUT FILE2, FILE3. OPEN OUTPUT FILE4.
COBOL/400 - 153 - Procedure Division
PERFORM Statement
Transfer control explicitly to one or more procedures and implicitly returns control to the next executable statement after execution of the specified procedure(s) or imperative statements is completed
4 PERFORM statement formats:
1. Basic PERFORM 2. TIMES phrase PERFORM 3. UNTIL phrase PERFORM 4. VARYING phrase PERFORM
COBOL/400 - 154 - Procedure Division
Coding Example:
PERFORM 110-INITIALIZATION.
PERFORM 300-LOAD-PROJECTS VARYING CNT1 FROM 1 BY 1 UNTIL CNT1 > PROJECT-COUNT END-PERFORM.
COBOL/400 - 155 - Procedure Division
READ Statement
Make a record available to the program
Associated file must be open in INPUT or I-O mode
For sequential access, the READ statement makes the next logical record from a file available to the object program
For random access, the READ statement makes a specified record from a direct-access file available to the object program
COBOL/400 - 156 - Procedure Division
Coding Example:
READ PROJECT-FILE INVALID KEY SET INVALID-PROJ TO TRUE END-READ.
READ PROJECT-FILE-L NEXT RECORD AT END PERFORM 300-END-OF-FILE-PROCESSING END-READ .
READ PROJECT-FILE AT END SET END-OF-FILE TO TRUE END-READ.
COBOL/400 - 157 - Procedure Division
RELEASE Statement
Transfer records from an input/output area to the initial phase of a sorting operation
Can only be used within the range of an input procedure associated with a SORT statement
COBOL/400 - 158 - Procedure Division
RETURN Statement
Transfer records from the final phase of a sort or merge operation to an OUTPUT PROCEDURE
Can be used only within the range of an output procedure associated with a SORT or MERGE statement
COBOL/400 - 159 - Procedure Division
REWRITE Statement
Logically replace an existing record in a direct-access file
Associated direct-access file must be open in I-O mode
COBOL/400 - 160 - Procedure Division
REWRITE Statement for Sequential Files
When the access mode is sequential, the last prior input/output statement executed for this file must be a successfully executed READ statement. When the REWRITE statement is executed, the record retrieved by that READ statement is logically replaced
The INVALID KEY phrase must not be specified for a file with sequential organization
An EXCEPTION/ERROR procedure may be specified
COBOL/400 - 161 - Procedure Division
REWRITE Statement for Indexed Files
When the access mode is sequential, the record to be replaced is specified by the value contained in the prime RECORD KEY. When the REWRITE statement is executed, this value must equal the value of the prime record key data item in the last record read from this file
The INVALID KEY phrase must be specified if an applicable USE AFTER STANDARD EXCEPTION procedure is not specified for the associated file-name
COBOL/400 - 162 - Procedure Division
REWRITE Statement for Relative Files
For relative files in sequential access mode, the INVALID KEY phrase must not be specified
An EXCEPTION/ERROR procedure may be specified
The INVALID KEY phrase must be specified in the REWRITE statement for relative files in the random or dynamic access mode, and for which an appropriate USE AFTER STANDARD EXCEPTION procedure is not specified
When the access mode is random or dynamic, the record to be replaced is specified in the RELATIVE KEY data item
If the file does not contain the record specified, an invalid key condition exists, and, if specified, the INVALID KEY imperative-statement is executed
REWRITE SUBFILE PROJECT-RCD FROM WS-LIST1SFL-O FORMAT IS "LIST1SFL" INDICATORS ARE WS-INDICATOR-TABLE INVALID KEY CONTINUE END-REWRITE.
COBOL/400 - 164 - Procedure Division
ROLLBACK Statement
Provide a way to cancel one or more changes to data-base records when the changes should not remain permanent
Any changes made to files under commitment control since the last commitment boundary are removed from the database
A commitment boundary is the previous occurrence of a ROLLBACK or COMMIT statement
If no COMMIT or ROLLBACK has been issued, the commitment boundary is the first OPEN of a file under commitment control
COBOL/400 - 165 - Procedure Division
Removal of changes takes place for all files under commitment control in the job, and not just for files under commitment control in the COBOL program that issues the ROLLBACK
All record locks held by the job for files under commitment control are released and the records become available to other jobs
Has no effect on files not under commitment control
If a ROLLBACK is executed and there are no files under commitment control, the ROLLBACK is ignored
A ROLLBACK, when executed, leaves files in the same open or closed state as before execution
COBOL/400 - 166 - Procedure Division
SEARCH Statement
Search a table for an element that satisfies the specified condition, and adjusts the associated index to indicate that element
COBOL/400 - 167 - Procedure Division
Coding Example:
SEARCH PROJECT-TABLE AT END DISPLAY "TABLE OVERFLOW"
SEARCH PROJECT-TABLE VARYING PROJECT-TABLE-INDEX
COBOL/400 - 168 - Procedure Division
SET Statement
Establish reference points for table handling operations by doing one of the following:
1. Placing values associated with table elements into indexes associated with index-names
2. Incrementing or decrementing an occurrence number
3. Setting the status of an external switch to ON or OFF
4. Moving data to condition names to make conditions true
5. Setting addresses of pointer data items
COBOL/400 - 169 - Procedure Division
Coding Example:
SET PARM-ERROR-FOUND TO TRUE.
READ PROJECT-RECORD AT END SET NO-MORE-RECORDS TO TRUE END-READ.
COBOL/400 - 170 - Procedure Division
SORT Statement
Accept records from one or more files, sorts them according to the specified key(s), and makes the sorted records available either through an OUTPUT PROCEDURE or in an output file
COBOL/400 - 171 - Procedure Division
START Statement
Provide a means of positioning within an indexed or relative file for subsequent sequential record retrieval
This positioning is achieved by comparing the key values of records in the file with the value you place in the RECORD KEY portion of a files record area (for an indexed file), or in the RELATIVE KEY data item (for a relative file) prior to execution of the START statement
COBOL/400 - 172 - Procedure Division
Coding Example:
START PROJECT-FILE KEY IS NOT LESS THAN EXTERNALLY-DESCRIBED-KEY INVALID KEY SET NO-MORE-RECORDS TO TRUE END-START.
COBOL/400 - 173 - Procedure Division
STOP Statement
Halts execution of the object program either permanently or temporarily
COBOL/400 - 174 - Procedure Division
STRING Statement
String together the partial or complete contents of two or more data items or literals into one single data item
One STRING statement can be written instead of a series of MOVE statements
COBOL/400 - 175 - Procedure Division
Coding Example:
STRING LASTNAME DELIMITED BY " " "," DELIMITED BY SIZE FIRSTNAME DELIMITED BY SIZE INTO USERNAME.
STRING MM, "/", DD, "/", YY DELIMITED BY SIZE INTO CURR-DATE.
COBOL/400 - 176 - Procedure Division
SUBTRACT Statement
Subtract one numeric item, or the sum of two or more numeric items, from one or more numeric items, and stores the result
COBOL/400 - 177 - Procedure Division
Coding Example:
SUBTRACT 1 FROM NUM1
SUBTRACT 1 FROM NUM1 GIVING RESULT.
COBOL/400 - 178 - Procedure Division
UNSTRING Statement
Cause contiguous data in a sending field to be separated and placed into multiple receiving fields
One UNSTRING statement can be written instead of a series of MOVE statements
COBOL/400 - 179 - Procedure Division
Coding Example:
UNSTRING NAME INTO NAME1 NAME2 NAME3.
01 NAME PIC X(60).
COBOL/400 - 180 - Procedure Division
WRITE Statement
Release a logical record for an output or input/output file
When the WRITE statement is executed
the associated indexed or relative file must be open in OUTPUT, I-O, or EXTEND mode
the associated sequential file must be open in OUTPUT or EXTEND mode
COBOL/400 - 181 - Procedure Division
Coding Example:
WRITE ERROR-LINE FROM ERROR-REPORT-LINE AFTER ADVANCING 2 LINES END-WRITE.
WRITE PROJECT-RECORD END-WRITE.
WRITE PROJECT-RECORD FORMAT IS "LIST1CMD" INDICATORS ARE WS-INDICATOR-TABLE INVALID KEY CONTINUE END-WRITE.
COBOL/400 - 182 - COBOL/400 Compiler Limits
Language Element COBOL/400 Limit Data Division Length of: Working-Storage Section group item 3 000 000 bytes Linkage Section group item 3 000 000 bytes Elementary item 3 000 000 bytes Procedure Division Number of: GO TO proc-name 255 IF nesting levels 30 CALL parameters 30 SORT-MERGE input files 31 SORT-MERGE keys 30
COBOL/400 - 183 - COBOL/400 Compiler Limits
Language Element COBOL/400 Limit
General Number of Files 99 Number of characters to identify: Library-name 10 Program-name 10 Text-name 10
Environment Division Number of: SELECT file-names 99
COBOL/400 - 184 - COBOL/400 Compiler Limits
Language Element COBOL/400 Limit Data Division Length of: Working-Storage Section group item 3 000 000 bytes Linkage Section group item 3 000 000 bytes Elementary item 3 000 000 bytes Procedure Division Number of: GO TO proc-name 255 IF nesting levels 30 CALL parameters 30 SORT-MERGE input files 31 SORT-MERGE keys 30