COBOL - Basic Verbs
The Procedure division contains the code used to perform the manipulation of the data described in the Data Division.
The procedure division uses COBOL verbs for data processing. A statement always initiates with a COBOL verb.
In COBOL, there are several verbs with different types of actions. Let's see few of them now,
ACCEPT:
The ACCEPT statement is used to acquire data from the primary input device or various system fields. Each of the two formats works slightly differently and is described in the correspondingly numbered area.
Syntax - Format 1:
This format is used to accept data from the primary input or other specified device. The field(s) listed after ACCEPT are loaded.
Syntax - Format 2:
This format is used to accept one of various system value(s). Each of the items to be accept has a different format.
Tips:
- Use the ACCEPT statement to acquire small amounts of data such as parameters to be entered at runtime.
- The chief advantage of acquiring data with the ACCEPT statement is that no file needs to be established to use it.
- The chief disadvantage of acquiring data with the ACCEPT statement is that there is no mechanism to recognize end of file.
- Many COBOLs have added enhancements to allow access to a four-digit year due to the Y2K problem. Check your system manual for details.
DISPLAY:
The DISPLAY statement is used to exhibit data upon the primary output device. The field(s) listed after DISPLAY is exhibited.
Syntax - Format:
DISPLAY identifier [upon{mnemonic-name-1/environment name}] [WITH NO ADVANCING] |
Tips:
- Use the DISPLAY statement to exhibit small amounts of data such as error messages.
- The chief advantage of exhibiting data with the DISPLAY statement is that no file needs to be established to use it.
- The chief disadvantage of exhibiting data with the DISPLAY statement is that there is no mechanism to supply carriage control.
Display: Rules
- Identifier-1 is converted automatically to external format, if required
- Negative signed values cause a low-order sign over punch
- Pointers are converted to an external PIC 9(10)
- INDEX names cannot be specified
INITIALIZE:
The INITIALIZE statement is used to initialize data items. Data items are initialized according to their type.
Syntax:
- Alphabetic and alphanumeric items are initialized to spaces.
- Numeric items are initialized to zeros.
- For group items, the subordinate elementary items are initialized depending on their individual data types. Only named items are initialized.
- The REPLACING phrase can be used to initialize data items to different values other than the default SPACES or ZEROS.
Tips:
- Use the initialize statement instead of moving ZEROS or SPACES to a group. That way, each subordinate item will be set to an appropriate value.
- Notice that unnamed and FILLER items are not initialized. This allows the variable portion of a detail line, for example, to be initialized without the constant values inserted into the unnamed fields being affected.
MOVE:
The MOVE statement is used to copy data items to other data items. Below two formats works slightly differently and is described below as well.
SYNTAX 1:
MOVE {Identifer-1/Litera-1} TO {Identifier-2....} |
The format is used to copy a field or value to another field(s). The field or value listed between MOVE and TO is copied to the value of the field(s) following the TO.
For example in MOVE A TO B, the value in A is copied to B. The value in A is unchanged. Furthermore, in MOVE C TO E F, the values in C is moved to the value in E, storing the answer in E and the value of C is also moved to F storing the answer in F. The value in C is unchanged.
SYNTAX 2:
MOVE {CORRESPONDING/CORR} Identifer-1 TO {Identifier-2.....} |
The format is used to copy subordinate field(s) of one group to subordinate field(s) in another. Those subordinate field(s) of the group item identifier-1 are copied to and those with exactly the same name in the group item identifier-2. The names of the subordinate items must be spelled exactly the same way in bot to participate in the move.
Things to remember:
- If the receiving field is edited, any editing required will be done at the time of the MOVE.
- If the receiving field is unedited and the sending field is edited, the field will be de- edited at the time of the MOVE.
- If the sending field is shorter than the receiving field:
- Numeric fields are padded on the left with zeros.
- Non-numeric fields are padded on the right with spaces.
- If the sending field is longer than the receiving field:
- Numeric fields are truncated on the left.
- Non-numeric fields are truncated on the right.
Use of CORRESPONDING in MOVE Statement
- Both identifiers must be group items.
- Both identifiers following the keyword CORRESPONDING must name group items. In this discussion, these identifiers are referred to as identifier-1 and identifier-2.
- A pair of data items (subordinate items), one from identifier-1 and one from identifier- 2, correspond if the following conditions are true:
- In a MOVE statement, at least one of the data items is an elementary item, and the move is permitted by the move rules.
- The subordinate items are not identified by the keyword FILLER.
- Neither identifier-1 nor identifier-2 is described as a level 66, 77, or 88 item, nor is either described as a USAGE IS INDEX item. Neither identifier-1 nor identifier-2 can be reference-modified.
- The subordinate items do not include a REDEFINES, RENAMES, OCCURS, or USAGE IS INDEX clause in their descriptions.
- However, identifier-1 and identifier-2, they can contain or be subordinate to items containing a REDEFINES or OCCURS clause in their descriptions.
- Neither identifier-1 nor identifier-2 nor the two subordinate items are described as USAGE IS POINTER items.
- Identifier-1 and/or identifier-2 can be subordinate to a FILLER item.
Valid and Invalid Elementary Moves:
- Includes DBCS data items.
- Includes nonnumeric literals.
- Figurative constants and nonnumeric literals must consist only of numeric characters and will be treated as numeric integer fields.
- Figurative constants and nonnumeric literals must consist only of numeric characters and will be treated as numeric integer fields. The ALL literal cannot be used as a sending item.
- Includes integer numeric literals.
- Includes non-integer numeric literals (for example, 3.142).
- Includes floating-point literals, external floating-point data items (USAGE DISPLAY), and internal floating-point data items (USAGE COMP-1 or USAGE COMP-2).
- Includes DBCS data-items, DBCS literals, and SPACE.
Quite often it is required to move some of the data item of one group to some other data item of other group. If the names of the corresponding data items are distinct then separate MOVE statements have to be used. But if the corresponding data items have identical names, then instead of separate MOVE statements, MOVE CORRESPONDING can be used.
Example:
In this example, the ID-NUMBER, NAME, DEPARTMENT, BASIC-PAY of PAY-REC will be moved the same data item of PRINT-REC.
This is equivalent to 4 move statements.
MOVE ID-NUMBER OF PAY-REC TO ID-NUMBER OF PRINT-REC.
MOVE NAME OF PAY-REC TO NAME OF PRINT-REC.
MOVE DEPARTMENT OF PAY-REC TO DEPARTMENT OF PRINT-REC.
MOVE BASIC-PAY OF PAY-REC TO BASIC-PAY OF PRINT-REC. |
STOP RUN:
STOP RUN is the last executable statement in the program which will returns the control back to OS.
STOP RUN always coded in the main program.
If STOP RUN coded in the sub program, the control will return to OS instead of returning to main program. In this case, the remaining task coded in main program will be incomplete.
STOP RUN closes all opened files in the program.
When the STOP RUN executed, immediately all running tasks by the program will be closed and control transfers to OS.
Syntax - Format:
STOP RUN terminates the program. STOP literal displays the literal and waits for a response before continuing with the next executable statement.
STOP RUN always terminates the current program execution regardless of the program type (main program or sub program).
Tips:
- Use the STOP RUN statement to terminate execution of a program.
- Do no use the STOP RUN statement in a called subprogram, as it will cause the termination of the entire run unit. Instead, to terminate execution of a subprogram, use the EXIT PROGRAM statement.
Let us see example how verbs used in the COBOL program,
IDENTIFICATION DIVISION.
PROGRAM-ID. TSTVERBS.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-AGE PIC 9(03).
01 INPUT-GENDER PIC X(01).
01 AGE PIC 9(03) VALUE ZEROES.
01 GENDER PIC X(01) VALUE SPACES.
PROCEDURE DIVISION.
INITIALIZE INPUT-AGE
INPUT-GENDER. ----> Good to initilize the variable before use it. or
we can declare VALUE Clause(see AGE & GENDER in working storage section)
ACCEPT INPUT-AGE. ----> Value shoud be passed from JCL
ACCEPT INPUT-GENDER. ----> Value shoud be passed from JCL
MOVE INPUT-AGE TO AGE. ----> Passing value from INPUT-AGE to AGE variable.
MOVE INPUT-GENDER TO GENDER. ----> Passing value from INPUT-GENDER to GENDER variable.
DISPLAY AGE. ----> Write the AGE value to output.
DISPLAY GENDER. ----> Write the GENDER value to output.
STOP RUN. -----> Returns the control back to OS
|
Now time to see "Hello World" COBOL program in next chapter.
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!