Vous êtes sur la page 1sur 30

COBOL/400 Quick Reference

Coding Format for COBOL/400 Programs


There are 80 characters position on each line of the coding sheet and these
positions are grouped into the following five fields.
Position

Field

16
7
8 11
12 72
72 80

Sequence
Indicator (*, /, -)
Area A / Margin A
Area B / Margin B
Identification

Structure of a COBOL Program


Identification Division
o Contains Program Name, Author, Date Compiled, Date Written, etc.
Environment Division
o Configuration Section
Specifies the source and object computer
o Input-Output Section
File Control
Associates each file in the COBOL program with an
external medium, and specifies file organization, access
mode, and other information.
I/O Control
Specifies when checkpoints are to be taken and the storage
areas to be shared by different files.(Commitment control)
Data Division
o File Section
Describes the externally stored and merge/sort files.
o Working Storage Section
Describes data records that are not part of external data files but
are used within the program.
o Linkage Section
Describes data made available from another program like the
parameters passed to the program. VALUE phrase should not be
defines for the data-items since it is somewhere outside the
program.
Procedure Division

Page 1 of 30

COBOL/400 Quick Reference


o Specifies the flow of the program and contains the actual executable
statements.

Arithmetic Operators
Binary Operators

Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Exponentiation (**)

Unary Operators
Positive (+)
Negative (-)

Conditional Expressions
Simple Conditions
Class Condition
To determine the type of the data-item
Syntax:
identifier [IS [NOT]] {1. NUMERIC
2. ALPHABETIC
3. ALPABETIC-LOWER
4. ALPHABETIC-UPPER
5. class-name}
Example:
NAME IS ALPHABETIC-UPPER
TOTAL IS NOT ALPHABETIC
Condition Name Condition
A condition-name condition tests a conditional variable to determine whether its
value is equal to any value(s) associated with the condition-name.
Example:
01 GENDER PIC X.
88 MALE
VALUE
M.
88 FEMALE
VALUE
F.
..
..
PROCEDURE DIVISION
..
..
IF MALE THEN
Page 2 of 30

COBOL/400 Quick Reference


DISPLAY You belong to Male Gender
END-IF

Relation Condition
A relation condition compares two operands
Syntax:
Operand1 [IS [NOT]] {1. GREATER THAN [OR EQUAL TO] operand2
2. >[=]
3. LESS THAN [OR EQUAL TO]
4. <[=]
5. EQUAL TO
6. =}
Example:
MARK IS GREATER THAN 40
NAME IS = VANI
Sign Condition
Syntax:
Operand1 [IS NOT]] {1. POSITIVE
2. NEGATIVE
3. ZERO}
Example:
MARK IS POSITIVE
DIVIDENT IS NOT ZERO
Switch-Status Condition
The switch-status condition determines the on or off status of an UPSI switch.
The condition has to be specified in the SPECIAL NAMES para.

Complex Conditions
Negated Simple Conditions
NOT of a simple condition
Syntax:
NOT <simple-condition>
Example:
NOT A IS > B

Combined Conditions
Two or more conditions can be logically connected to form a combined condition.
Syntax:
Condition-1 {1. AND Condition-2
2. OR}
Page 3 of 30

COBOL/400 Quick Reference


Example:
A > B AND A > C

Abbreviated Combined Relation Conditions


When relation-conditions are written consecutively, with no parentheses
used to define precedence, any relation-condition after the first can be
abbreviated by omitting the subject and/or relational operator
Example:
Abbreviated Combined Relation
Condition
A = B AND NOT < C OR D

Equivalent

A NOT > B OR C
NOT A = B OR C
NOT (A = B OR < C)
NOT (A NOT = B AND C AND NOT D)

((A = B) AND (A NOT < C)) OR (A NOT


< D)
A NOT > B) OR (A NOT > C)
(NOT (A = B)) OR (A = C)
NOT ((A = B) OR (A < C))
NOT ((((A NOT = B) AND (A NOT = C))
AND (NOT (A NOT = D))))

ENVIRONMENT DIVISION
FILE-CONTROL
Example:
SELECT temp-file ASSIGN TO device-type file-name
ORGANIZATION IS {1. INDEXED
2. SEQUENTIAL
3. RELATIVE
4. TRANSACTION}
ACCESS MODE IS { 1. SEQUENTIAL
2. RANDOM
3. DYNAMIC}
FILE STATUS IS identifier-1.

DATA DIVISION
LEVEL NUMBER
LEVEL NO.
01
02 49
66

USAGE
GROUP ITEM
SUB ITEMS
RENAMES
Page 4 of 30

COBOL/400 Quick Reference


88
77

CONDITION-NAME-CONDITION
ELEMETARY DATA ITEM

Example:
01 GROUP-ITEM
02
SUB-ITEM1 PIC X(5).
02
SUB-ITEM2 PIC X(5).
02
SUB-ITEM3 PIC X(5).
02
SUB-ITEM4 PIC X(5).
02
SUB-ITEM5 PIC X(5).
02
SUB-ITEM6.
03
SUB-SUB-ITEM1 PIC 9(2).
03
SUB-SUB-ITEM2 PIC 9(2).
66
NEW-ITEM RENAMES SUB-ITEM1
THROUGH SUB-ITEM4.
77
ELEMENTARY-ITEM
PIC 9(2).
77
MARITAL-STATUS PIC X.
88
MARRIED VALUE
M.
88
SINGLE
VALUE
S.

FILE SECTION
Example:
FD TEMP-FILE.
BLOCK CONTAINS 150 CHARACTERS (USED ONLY FOR TAPE FILES)
RECORD CONTAINS 50 CHARACTERS (SIZE OF EACH RECORD)
LABEL RECORDS ARE STANDARD (ONLY FOR DOCUMENTATION)
DATA RECORD IS RECORD-FORMAT.
01 RECORD-FORMAT.
02
ITEMS
PIC .

Instead of defining the record-format we can even copy the record-format from
the database file itself using COPY statement.

REDEFINES
Using REDEFINES statement we can change the format or name of a particular dataitem provided the length is same in both the case. Both the data-items will share the
same memory but they can be referred with 2 different names.
Example:
05 REGULAR-EMPLOYEE.
10 LOCATION
10 GRADE

PICTURE
PICTURE

Page 5 of 30

A(8).
X(4).

COBOL/400 Quick Reference


10 SEMI-MONTHLY-PAY PICTURE

999V999.

05 TEMPORARY-EMPLOYEE REDEFINES REGULAR-EMPLOYEE.


10 LOCATION
PICTURE
A(8).
10 FILLER
PICTURE
X(6).
10 HOURLY-PAY
PICTURE
99V99.
10 CODE-H REDEFINES HOURLY-PAY PICTURE 9999.

PROCEDURE DIVISION STATEMENTS


DATA MOVEMENT VERBS
Syntax:
MOVE {1. identifier-1 TO identifier-2 [, identifier-3].
2. literal-i}
Example:
MOVE 0 TO NO1, NO2, NO3.

ARITHMETIC VERBS
ADD
Syntax1:
ADD {1. identifier-1 [, {1. identifier-2 .. TO identifier-3 [, identifier-4].....
2. literal-1}
2. literal-2}]
Example1:
ADD A, 10 TO B, C
Before Add: A = 5
B=5
C = 10
After Add: A = 5
B = 20
C = 25
Syntax2:
ADD {1. identifier-1 {1. identifier-2 [, {1. identifer-3.
2. literal-1}
2. literal-2}
2. literal-3}]
GIVING identifier-4 [, identifier-5]..
Example2:
ADD A , 10 GIVING B, C.
Before Add: A = 5
B=5
C = 10
After Add: A = 5
Page 6 of 30

COBOL/400 Quick Reference


B = 15
C = 15

SUBTRACT
Syntax:
SUBTRACT {1. identifier-1 [, {1. identifier-2
2. literal-i}
2. literal-2}]
FROM identifier-3 [, identifier-4]..
[GIVING identifier-5 [,identifier-6]]
Example:
Before Subtract: A = 5
B=3
C = 10
D = not defined
SUBTRACT A, B FROM C
After Subtract: A = 5
B=3
C = 2 (10 5 3)
D = not defined
SUBTRACT A, B FROM C GIVING D.
After Subtract: A = 5
B=3
C = 10
D = 2 ( 10 5 3)

MULTIPLY
Syntax:
MULTIPLY { 1. identifier-1 BY identifier-2 [, identifier-3]..
2. literal-1}
[GIVING identifier-4 [, identifier-5].]
Example
Before Multiply: A = 5
B=3
C = 10
D = not defined
E = not defined
MULTIPLY A BY B, C.
After Multiply: A= 5
B = 15 (A*B)
C = 50 (A*C)
D = not defined
E = not defined
MULTIPLY A BY B, C GIVING D, E
After Multiply: A = 5

Page 7 of 30

COBOL/400 Quick Reference


B=3
C = 10
D = 15 (A*B)
E = 50 (A*C)

DIVIDE
Syntax1:
DIVIDE {1. identifier-1 INTO identifier-2 [, identifier-3] .
2. literal-1}
[GIVING identifier-4 [, identifier-5]]
Example1:
Before Divide: A = 10
B = 20
C = not defined
D = not defined
DIVIDE 2 BY A, B.
After Divide: A = 5
B = 10
C = not defined
D = not defined
DIVIDE 2 BY A, B GIVING C, D.
After Divide: A = 10
B = 20
C=5
D = 10
Syntax2:
DIVIDE {1. identifier-1 BY {1. identifier-2
Literal-1}
literal-2}
GIVING identifier-3 [,identifier-4].
Example2:
DIVIDE A BY B GIVING C.
Before Divide: A = 20
B = 10
C = not defined
After Divide: A = 20
B = 10
C=2
Syntax3:
DIVIDE {1. identifier-1 {1. INTO {1. identifier-2
2. literal-1}
2. BY}
literal-2}
GIVING identifier-3 [REMAINDER identifier-4]
Example3:
DIVIDE A BY B GIVING C REMAINDER D
Before Divide: A = 20

Page 8 of 30

COBOL/400 Quick Reference


B=3
C = not defined
D = not defined
After Divide: A = 20
B=3
C=6
D=2
Note:
Small-number INTO big-number
Big-number BY small-number

COMPUTE
Syntax:
COMPUTE identifier-1 [ROUNDED] = arithmetic expression
[[ON] SIZE ERROR imperative-statement-1]
[NOT [ON] SIZE ERROR imperative-statement-2]
[END-COMPUTE]
Example:
COMPUTE A ROUNDED = (A + B) * 3.5 / 2
ON SIZE ERROR DISPLAY ERROR.

INPUT AND OUTPUT VERBS


ACCEPT
Syntax:
ACCEPT identifier [FROM {1. mnemonic-name
2. DATE
3. DAY
4. TIME}]
where date is 9(6) - yymmdd
day is 9(5) - yyddd
time is 9(8) hhmmssxx
where xx hundredth of second
Example:
The mnemonic-name option is implementor-dependent. The hardware
device from which the data is to be read is to be equated to a mnemonic-name in the
SPECIAL-NAMES paragraph. For example, the following entry in the SPECIALNAMES paragraph
TYPEWRITER-1 IS CONTROL-DATA
May equate the mnemonic-name CONTROL-DATA with the assumed implementor-name
TYPEWRITER-1.
ACCEPT FLAG-A FROM CONTROL-DATA
Will read the value of FLAG-A from the hardware device indicated by TYPEWRITER-1.

Page 9 of 30

COBOL/400 Quick Reference

DISPLAY
Syntax:
DISPLAY {1. identifier-1 [, {1. identifier-2 . [UPON mnemonic-name]
2. literal-1}
2. literal-2}]
Refer the ACCEPT verb for explanation for mnemonic-name
Example:
DISPLAY THE TOTAL IS TOTAL.

CONDITIONAL AND SEQUENCE CONTROL VERBS


IF STATEMENT
SIMPLE IF
Syntax:
IF condition-1 statement-1.
Refer conditional expressions for detailed explanation for condition-1 and
statement-1 can be one or more COBOL Statements. The IF should be normally be
terminated by a period or an END-IF statement.
Example:
IF A >B
DISPLAY A IS GREATER THAN B.

NESTED IF
If an IF Statement occurs within another IF Statement, its referred as Nested IF.
Example:
IF A > B
IF A > C
DISPLAY A IS GREATEST
END-IF
END-IF.

CONTINUE
The CONTINUE statement may be used anywhere a conditional statement or an
imperative statement may be used. It has no effect on the execution of
the program.
Example:
IF A > B
CONTINUE
ELSE
COMPUTE A = A + 1
END-IF.

Page 10 of 30

COBOL/400 Quick Reference

GO TO
Syntax:
GO TO procedure-name-1 [, procedure-name-2procedure-name-n]
DEPENDING ON identifier
Example1:
GO TO exit-para
Example2:
GO TO FIRST-PARA, SECOND-PARA, THIRD-PARA
DEPENDING ON VALUE.
This statement is equivalent to the set of IF conditions
IF VALUE = 1 GO TO FIRST-PARA.
IF VALUE = 2 GO TO SECOND-PARA.
IF VALUE = 3 GO TO THIRD-PARA.

ALTER STATEMENT
ALTER Statement can be used to modify the targets of GO TO statements written
elsewhere.
Syntax:
ALTER procedure-name-1 TO [PROCEED TO] procedure-name-2
[, procedure-name-3 TO [PROCEED TO] procedure-name-4].
Example:
MODIFIED-TRANSFER.
GO TO FIRST-TIME.
Suppose this GO TO Statement is to be modifier after its first execution so
that the subsequent execution of the sentence transfers control to the procedure named
OTHER-TIMES we have to code as follows
ALTER MODIFIED-TRANSFER TO PROCEED TO OTHER-TIMES.
After the execution of this ALTER Statement, the GO TO sentence
becomes
MODIFIED-TRANSFER.
GO TO OTHER-TIMES.

PERFORM STATEMENT
Syntax1:
PERFORM procedure-name-1 [{1. THRU
2. THROUGH}
Example1:
PERFORM FIRST-PARA THRU LAST-PARA.
Syntax2:
PERFORM procedure-name-1 [{1. THRU
2. THROUGH}
{1. identifier TIMES
Page 11 of 30

procedure-name-2]

procedure-name-2]

COBOL/400 Quick Reference


2. integer}
Example2:
PERFORM READ-PARA 10 TIMES.
Syntax3:
PERFORM procedure-name-1 [{1. THRU
procedure-name-2]
2. THROUGH}
UNTIL condition
Example3:
PERFORM FILE-READ-PARA UNTIL EOF = B1.
PERFORM FILE-READ-PARA UNTIL EOF = B1.
Syntax4:
PERFORM procedure-name-1 [{1. THRU
procedure-name-2]
2. THROUGH}
VARYING {1. identifier-1
FROM {1. identifier-2
2. index-name-1}
2. index-name-2
3. literal-1}
BY {1. identifier-3
UNTIL condition
2. literal-2}
Example4:
PERFORM READ-ARRAY-PARA
VARYING I FROM 1 BY 1
UNTIL I > 10.
This is equivalent to
For (i=1; i <= 10; i++)
{
...
...
}
Syntax5:
PERFORM procedure-name-1 [{1. THRU
procedure-name-2]
2. THROUGH}
VARYING {1. identifier-1
FROM {1. identifier-2
2. index-name-1}
2. index-name-2
3. literal-1}
BY {1. identifier-3
UNTIL condition-1
2. literal-2}
[AFTER {1. identifier-4
FROM
{1. identifier-5
2. index-name-3}
2. index-name-4
3. literal-3}
BY
{1. identifier-6
UNTIL
condition-2
2. literal-4}
[AFTER {1. identifier-7
FROM
{1. identifier-8

Page 12 of 30

COBOL/400 Quick Reference


2. index-name-5}
BY

{1. identifier-9
2. literal-6}

UNTIL

2. index-name-6
3. literal-5}
condition-3].]

Example6:
PERFORM READ-MATRIX-PARA
VARYING I FROM 1 BY 1 UNTIL I > 3
AFTER J FROM 1 BY 1 UNTIL J > 3.
This is equivalent to
For (i=1; I <= 3; i++)
For (j=1; j<=3; j++)
{
...
...
}

EVALUATE
Syntax:
EVALUATE { 1. identifier-1
[ALSO {1. identifier-2
2. literal-1
2. literal-2
3. expression-1
3. expression-2
4. TRUE
4. TRUE
5. FALSE}
5. FALSE}]
WHEN phrase-1 [ALSO phrase2.] imperative-statement-1
[WHEN OTHER imperative-statement-2]
[END-EVALUATE]
where phrase-1 and phrase-2 are defined as
{1. ANY
2. condition
3. TRUE
4. FALSE
5. [NOT] {1. identifier
[{1. THRU
{1. identifier
2. literal
2. THROUGH} 2. literal
3. arithmetic-expression}
3. arithmeticexp.}]
Example1:
EVALUATE VALUE
WHEN 1
DISPLAY ONE
WHEN 2
DISPLAY TWO
WHEN 3
DISPLAY THREE
WHEN OTHER

Page 13 of 30

COBOL/400 Quick Reference


DISPLAY 1 > VALUE > 3
END-EVALUATE.
Example2:
EVALUATE A = B ALSO C > D ALSO TRUE
WHEN TRUE ALSO TRUE ALSO E = F + 15
imperative-statement-1
WHEN TRUE ALSO TRUE ALSO E > 12
imperative-statement-2
WHEN TRUE ALSO FALSE ALSO ANY
imperative-statement-3
WHEN FALSE ALSO TRUE ALSO ANY
imperative-statement-4
WHEN FALSE ALSO FALSE ALSO ANY
imperative-statement-5
END-EVALUATE.
The Equivalent IF Statement:
IF A = B THEN
IF C > D THEN
IF E = F + 15 THEN
imp-stat-1
ELSE
IF E > 12 THEN
imp-stat-2
END-IF
END-IF
ELSE
imp-stat-3
END-IF
ELSE
IF C > D THEN
imp-stat-4
ELSE
imp-stat-5
END-IF
END-IF.

PROGRAM TERMINATION STATEMENTS


Termination Statement
EXIT PROGRAM
STOP RUN
GO BACK

Main Program
Non operational
Return to calling
program(may be the OS)
Return to the calling

Page 14 of 30

Sub Program
Return to calling program
Return to the calling
program
Return to calling program

COBOL/400 Quick Reference

EXIT

program(may be the OS)


Has no effect at all. For
documentation purpose.
Should be the only
statement in the paragraph.

Has no effect at all. For


documentation purpose.
Should be the only
statement in the paragraph.

TRANSFER CONTROL STATEMENTS


CALL
Syntax:
CALL {1. identifier-1
2. literal-1}
[USING [BY] {1. [ REFERENCE [{1. ADDRESS OF
2. file-name-1}]]
2. [CONTENT [{1. ADDRESS OF
2. LENGTH OF

identifier-2]

3. literal-2
4. file-name-1}]]
{1. [[ON] OVERFLOW imperative-statement-1]
2. [[ON] EXCEPTION imperative-statement-2]
[NOT [ON] EXCEPTION imperative-statement-3]}
[END-CALL]
Using
To pass the parameters to the called program.
Reference BY
To pass the parameters as pointers(address of the variables)
Content BY
To pass the parameters as value(pass by value)
ADDRESS OF
To get the address of a data-item
LENGTH OF
Returns the no. of bytes required for the particular data-item
ON EXCEPTION
When the call to the sub-program cannot be made
NOT ON EXCEPTION
If there is no exception
OVERFLOW
Same as ON EXCEPTION

Page 15 of 30

COBOL/400 Quick Reference

CANCEL
The cancel statement ensures the next time the program is called it will be entered in its
initial state. (ie) It disconnects the logical connection between the calling program and
called program. (ie) closes all files, re-initialize the variable to its initial state and so on.
Syntax:
CANCEL {1. identifier-1
2. literal-1}

CHARACTER HANDLING STATEMENTS


EXAMINE
This is used to scan the string to find the no. of occurrences of a given character in
it. The count of the no. of occurrence will be stored in the TALLY register.
Syntax1:
EXAMINE identifier TALLYING {1. ALL
literal-1
2. LEADING
3. UNTIL FIRST}
Syntax2:
EXAMINE identifier REPLACING {1. ALL
literal-2
2. LEADING
3. UNTIL FIRST}
BY literal-3
Syntax3:
EXAMINE identifier TALLYING {1. ALL
literal-4
2. LEADING
3. UNTIL FIRST}
REPLACING BY literal-5
Example:
77
A
PIC X(5) VALUE
IS EERIE
EXAMINE A TALLYING ALL E.
TALLY = 3
EXAMINE A TALLYING LEADING E.
TALLY = 2
EXAMINE A TALLYING UNTIL FIRST I.
TALLY = 3
EXAMINE A TALLYING LEADING R.
TALLY = 0
EXAMINE A TALLYING ALL E REPLACING BY B.
TALLY = 3
A = BBRIB
EXAMINE A REPLACING FIRST I BY P.
A = EERPE
EXAMINE A REPLACING UNTIL FIRST I BY P.
Page 16 of 30

COBOL/400 Quick Reference


A = PPPIE

INSPECT
This is similar to the EXAMINE verb except for it doesnt maintain a special register
TALLY instead it stores the count in a separate variable. The tally count value will be
added to the identifier.
Syntax1:
INSPECT identifier-1 TALLYING
[identifier-2 FOR {1. {1. ALL
{1. identifier-3
2. LEADING}
2. literal-1}
2. CHARACTERS}
[{1. BEFORE
INITIAL
{1. identifier-4
2. AFTER}
2. literal-2}]]
Example:
77 MY-STRING PIC X(20) VALUE ANANTA KUMAR MAITY
77 TALLY-COUNT PIC 9(2) VALUE 0.
INSPECT MY-STRING TALLYING TALLY-COUNT FOR ALL A.
TALLY-COUNT = 5
INSPECT MY-STRING TALLYING TALLY-COUNT FOR LEADING A.
TALLY-COUNT = 1
INSPECT MY-STRING TALLYING TALLY-COUNT FOR CHARACTERS.
TALLY-COUNT = 20
INSPECT MY-STRING TALLYING TALLY-COUNT FOR ALL A
AFTER INTIAL A AND BEFORE INTIAL T.
TALLY-COUNT = 3 (NAN)
Syntax2:
INSPECT identifier-1 REPLACING
{1. CHARACTERS BY {1. identifier-5 {1. BEFORE INITIAL {1. identifier-6
2. literal-3}
2. AFTER}
2. lieral-4}
2. {1. ALL
{1. identifier-7 BY {1. identifier-8
2. LEADING
2. literal-5}
2. literal-6}}
3. FIRST}
[{1. BEFORE
INITIAL
{1. identifier-9
2. AFTER}
2. literal-7}]
Example:
INSPECT AMOUNT REPLACING LEADING BY 0.
INSPECT KARD REPLACING ALL % BY (, < BY ),
BY +.
Syntax3:

Page 17 of 30

COBOL/400 Quick Reference


INSPECT identifier-1 TALLYING
<tallying part as in option 1>
REPLACING
<replacing part as in option 2>
Example:
INSPECT KARD TALLYING TALLY-COUNTER
REPLACING ALL % BY (, < BY ),
BY +.

STRING
Used to combine 2 or more strings to a single string.
Syntax:
STRING {1. identifier-1
[{1. identifier-2
2. literal-1}
2. literal-2}]
DELIMITED BY {1. identifier-3
2. literal-3
3. SIZE}
[{1. identifier-4 [{1. identifier-5
2. literal-4}
2. literal-5}]
DELIMITED BY {1. identifier-6
2. literal-6
3. SIZE}]
INTO identifier-7 [WITH POINTER identifier-5]
[ON OVERFLOW imperative-statement]
Example:
77 FIELD-1 PIC X(4) VALUE
RAIN.
77 FIELD-2 PIC X(4) VALUE
SPACES.
STRING FIELD-1 DELIMITED BY I INTO FIELD-2
FIELD2 = RA
Example:
77 FIELD-1 PIC X(4) VALUE
ABCD.
77 FIELD-2 PIC X(6) VALUE
MA IN .
77 FIELD-3 PIC X(9) VALUE
121,34,56.
77 FIELD-4 PIC X(14) VALUE
SPACES.
STRING FIELD-1, FIELD2, FIELD3 DELIMITED BY , ,,
INTO FIELD-4.
FIELD-4 = ABCDMA121 .
Example:
77 FIELD-1 PIC X(4) VALUE
ABCD.
77 FIELD-2 PIC X(6) VALUE
MA IN .
77 FIELD-3 PIC X(9) VALUE
121,34,56.
77 FIELD-4 PIC X(14) VALUE
SPACES.
77 CHARACTER-POSITION PIC 9(2) VALUE 3.
STRING FIELD-1, FIELD-2, FIELD-3 DELIMITED BY

Page 18 of 30

COBOL/400 Quick Reference


, , INTO FIELD-4
WITH POINTER CHARACTER-POSITION
FIELD-4 = ABCDMA121 .
CHARACTER-POSITION = 12.
Phrase POINTER is used to determine the left-most position of the
receiving field.
Example:
02

NAME.
03
FIRST-NAME
PIC X(10).
03
MIDDLE-NAME
PIC X(6).
03
SURNAME
PIC X(20).
Suppose we want to store the name in the order as surname+first+middle,
Define the following also,
77
OUTPUT-REC
PIC X(39).
77
A
PIC X
VALUE
SPACE.
STRING SURNAME DELIMITED BY SPACES
A DELIMITED BY SIZE
FIRST-NAME DELIMITED BY SPACES
A DELIMITED BY SIZE
MIDDLE-NAME DELIMITED BY SPACES
INTO OUTPUT-REC.

UNSTRING
Syntax:
UNSTRING identifier-1
[DELIMITED BY [ALL] {1. identifier-2 [OR [ALL] {1. identifier-3
2. literal-1}
2. literal-2}]]
INTO identifier-4 [DELIMITER IN identifier-5]
[COUNT IN identifier-6]
[identifier-7 [DELIMITER IN identifier-7]
[COUNT IN identifier-8]]
[WITH POINTER identifier-9]
[TALLYING IN identifier-11]
[ON OVERFLOW imperative-statement]
Example:
77 FIELD-1 PIC X(10).
77 FIELD-2 PIC X(10).
77 FIELD-3 PIC X(15).
77 DATA-FIELD
PIC X(38) VALUE A.K. KRISHNAMURTHY.
UNSTRING DATA-FIELD DELIMITED BY .
INTO FIELD-1, FIELD-2, FIELD-3.
FIELD-1 = A

FIELD-2 = K

FIELD-3 = KRISHNAMURTHY
Example:

Page 19 of 30

COBOL/400 Quick Reference


77 FIELD-1 PIC X(5).
77 FIELD-2 PIC X(5).
77 FIELD-3 PIC X(5).
77 DATA-FIELD
PIC X(15) VALUE ABCDEFGCCHIJKCL.
UNSTRING DATA-FIELD DELIMITED BY C
INTO FIELD-1, FIELD-2, FIELD-3.
FIELD-1 = AB
FIELD-2 = DEFG
FIELD-3 =
UNSTRING DATA-FIELD DELIMITED BY ALL C
INTO FIELD-1, FIELD-2, FIELD-3.
FIELD-1 = AB
FIELD-2 = DEFG
FIELD-3 = HIJK
Example:
DATA-FIELD = A.K. MAJUMDAR
UNSTRING DATA-FIELD DELIMITED BY . OR ALL
INTO FIELD-1, FIELD-2, FIELD-3.
FIELD-1 = A
FIELD-2 = K
FIELD-3 = MAJUM
Example:
77 FIELD-1 PIC X(6).
77 FIELD-2 PIC X(6).
77 FIELD-3 PIC X(6).
77 FIELD-4 PIC X(6).
77 D1
PIC X.
77 D2
PIC X.
77 D3
PIC X.
77 D4
PIC X.
77 C1
PIC 9.
77 C2
PIC 9.
77 C3
PIC 9.
77 C4
PIC 9.
77 T
PIC 9.
77 DATA-FIELD PIC X(26)
VALUE THERE MAY-BE-SOME ERROR .
UNSTRING DATA-FIELD DELIMITED - OR ALL
INTO FIELD-1 DELIMITER D1 COUNT C1
INTO FIELD-2 DELIMITER D2 COUNT C2
INTO FIELD-3 DELIMITER D3 COUNT C3
INTO FIELD-4 DELIMITER D3 COUNT C4
TALLYING T
ON OVERFLOW PERFORM ERROR-ROUTINE.

Page 20 of 30

COBOL/400 Quick Reference


FIELD1 = THERE
FIELD2 = MAY
FIELD3 = BE
FIELD4 = SOME

D1=
D2=-
D3=-
D4=

C1=5
C2=3
C3=2
C4=4

PROGRAM TO PROGRAM INTERACTION


Program to program interaction can be done through a CALL statement.
Example:
CALLING PROGRAM:
IDENTIFICATION DIVISION.
PROGRAM-ID. PGMA.
AUTHOR. LAKS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 PLIST.
02 PARM1 PIC 9(2).
02 PARM2 PIC 9(2).
PROCEDURE DIVISION.
MAIN-PARA.
CALL PGMB USING PLIST.
STOP RUN.
CALLED PROGRAM:
IDENTIFICATION DIVISION.
PROGRAM-ID. PGMB.
AUTHOR. LAKS.
DATA DIVISION.
LINKAGE SECTION.
01 PARAMETER-LIST.
02 PARAMETER1
PIC 9(2).
02 PARAMETER2
PIC 9(2).
PROCEDURE DIVISION USING PARAMTER-LIST.
MAIN-PARA.
DISPLAY PARAMETER1 = PARAMETER1.
DISPLAY PARAMETER2 = PARAMETER2.
STOP RUN.
For Call Statement using Pointers refer to transfer control statements Call statement
section.

Page 21 of 30

COBOL/400 Quick Reference

TABLE HANDLING
Static Array:
01 TABLE-ONE.
05 ELEMENT-1
OCCURS
3
10
ELEMENT-2 OCCURS
15
VALUE1
PIC
15
VALUE2
PIC

TIMES.
3
TIMES.
X(10).
9(5).

Dynamic Array:
01 TABLE-ONE.
05 LEN
PIC 9(2 ).
05 ELEMENT-1 OCCURS 1 TO 25 TIMES DEPENDING ON LEN.
10
ELEMENT-2 OCCURS
3
TIMES.
15
VALUE1
PIC X(10).
15
VALUE2
PIC 9(5).

Search:
Linear Search:
Syntax:
SEARCH identifier-1 VARYING {1. identifier-2
2. index-name-1}
AT END imperative-statement-1
WHEN condition-1 {1. imperative-statement-1
2. NEXT SENTENCE}
WHEN condition-2 {1. imperative-statement-2
2. NEXT SENTENCE}.
END-SEARCH.
Example:
01

TABLE-ONE.
02
ELEMENT-1 OCCURS 5 TIMES INDEXED BY INDX.
03
VALUE
PIC 9(2).

SET INDX TO 1.
SEARCH TABLE-ONE VARYING INDX
AT END DISPLAY not found
WHEN ELEMENT-1(INDX) <= 10
DISPLAY ELEMENT-1(INDX)
END-SEARCH.

Page 22 of 30

COBOL/400 Quick Reference

Binary Search:
Syntax:
SEARCH ALL identifier-1
AT END imperative-statement-1
WHEN condition-1 [AND condition-2]
{1. imperative-statement-2
2. NEXT SENTENCE}

END-SEARCH.
Example:
SEARCH ALL TABLE-ONE
AT END DISPLAY not found

FILE HANDLING
OPEN
Syntax:
OPEN mode file-name-1
mode file-name-2
Example:
OPEN INPUT NEW-FILE.

CLOSE
Syntax:
CLOSE file-name-1, file-name-2 .

READ
SEQUENTIAL RETRIEVAL USING SEQUENTIAL ACCESS
Syntax:
READ file-name NEXT RECORD [INTO identifier-1]
[WITH NO LOCK]
[FORMAT IS {1. identifier-2
2. literal-1}]
[AT END imperative-statement-2]
[NOT AT END imperative-statement-2]
[END-READ]
Example:
READ TEMP-FILE NEXT RECORD INTO RECORD-1
AT END MOVE B1 TO EOF.
Page 23 of 30

COBOL/400 Quick Reference

SEQUENTIAL RETRIEVAL USING DYNAMIC ACCESS


Syntax:
READ file-name {1. NEXT RECORD
2. FIRST
3. LAST
4. PRIOR}
INTO identifier-1
[WITH NO LOCK]
[FORMAT IS {1. identifier-2
2. literal-1}]
[AT END imperative-statement-2]
[NOT AT END imperative-statement-2]
[END-READ]
Example:
READ TEMP-FILE PRIOR RECORD
AT END MOVE B1 TO BOF.

RANDOM RETRIEVAL
Syntax:
READ file-name RECORD INTO identifier-1
[WITH NO LOCK]
[KEY IS data-name-1]
[FORMAT IS {1. identifier-2
2. literal-1}
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-READ]
Example:
READ TEMP-FILE
KEY IS STUID OF TEMP-FILE
INVALID KEY DISPLAY not present
NOT INVALID KEY DISPLAY present
END-READ.

WRITE
SEQUENTIAL
Syntax:
WRITE record-name-1 FROM identifier-1
{1. BEFORE ADVANCING {1. identifier-2
Page 24 of 30

{1. LINE

COBOL/400 Quick Reference


2. AFTER}

2. mnemonic-name-1
2. LINES}
3. integer-1
4. PAGE}
AT {1. END-OF-PAGE imperative-statement-1
2. EOP}
NOT AT {1. END-OF-PAGE imperative-statement-1
2. EOP}
END-WRITE.
Example:
WRITE NEWREC FROM TEMPREC
AT END-OF-PAGE DISPLAY page overflow
NOT AT EOP DISPLAY continue u can still write more records
END-WRITE.

INDEXED FILES
Syntax:
WRITE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
2. literal-1}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-WRITE.

Example:
WRITE NEW-REC FROM TEMP-REC
FORMAT IS ADDREC
INVALID KEY DISPLAY error in write statement
END-WRITE.

RELATIVE FILES
Syntax:
WRITE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
2. literal-1}
{1. INDICATORS
{1. IS
identifier-3
2. INDICATOR
2. ARE}
3. INDIC}
AT {1. EOP
imperative-statement-1
2. END-OF-PAGE}
NOT AT {1. EOP
imperative-statement-2
2. END-OF-PAGE}

Page 25 of 30

COBOL/400 Quick Reference


Example:
WRITE NEW-REC
FORMAT IS ADDREC
INDICATORS ARE INDIC-AREA.

START
Syntax:
START file-name KEY IS {1. EQUAL TO
2. =
3. GREATER THAN
4. >
5. NOT LESS THAN
6. NOT <
7. GREATER THAN OR EQUAL TO
8. >=}
{1. EXTERNALLY-DESCRIBED KEY
2. data-item-1}
FORMAT IS {1. identifier-1
2. literal-1}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
Example:
START EMPLOYEE KEY IS EQUAL TO
EXTERNALLY-DESCRIBED KEY
INVALID KEY DISPLAY Employee does not exist
NOT INVALID KEY DISPLAY Employee found
END-START.

REWRITE
Syntax:
REWRITE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
2. literal-1}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-REWRITE.
Example:
REWRITE NEW-REC
FORMAT IS EDTREC.

Page 26 of 30

COBOL/400 Quick Reference

DELETE
Syntax:
DELETE filename RECORD
FORMAT IS identifier-1
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-DELETE.
Example:
DELETE TEMP-FILE
FORMAT IS TEMP-REC
INVALID KEY DISPLAY DELETION NOT DONE
NOT INVALID KEY DISPLAY DELETED SUCCESSFUL
END-DELETE.

SUBFILES
READ
Syntax:
READ SUBFILE filename
NEXT MODIFIED RECORD
INTO indentifier-1
FORMAT IS {1. identifier-2
2. literal-1}
TERMINAL IS {1. identifier-3
2. literal-2}
{1. INDICATORS
{1. IS
identifier-4
2. INDICATOR
2. ARE}
3. INDIC}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
AT END imperative-statement-3
NOT AT END imperative-statement-4
END-READ
Example:
READ SUBFILE EMPLOYEE
NEXT MODIFIED RECORD
FORMAT IS EMPSFL
INDICATORS ARE INDIC-AREA
END-READ.

WRITE
Syntax:
WRITE SUBFILE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
Page 27 of 30

COBOL/400 Quick Reference


2. literal-1}
TERMINAL IS {1. identifier-3
2. literal-1}
{1. INDICATORS
{1. IS
identifier-4
2. INDICATOR
2. ARE}
3. INDIC}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-WRITE.
Example:
WRITE SUBFILE EMPREC
FORMAT IS EMPSFL
INDICATORS ARE INDIC-AREA
INVALID KEY DISPLAY ERROR IN RRN VALUE
END-WRITE.

REWRITE
Syntax:
REWRITE SUBFILE record-name-1 FROM identifier-1
FORMAT IS {1. identifier-2
2. literal-1}
TERMINAL IS {1. identifier-3
2. literal-2}
{1. INDICATORS
{1. IS
identifier-4
2. INDICATOR
2. ARE}
3. INDIC}
INVALID KEY imperative-statement-1
NOT INVALID KEY imperative-statement-2
END-REWRITE.
Example:
REWRITE SUBFILE EMPREC
FORMAT IS EMPSFL
INDICATORS ARE INDIC-AREA
INVALID KEY DISPLAY ERROR IN RRN
END-REWRITE.

ACQUIRE
Syntax:
ACQUIRE {1. identifier
FOR file-name
2. literal}
This statement is used to acquire a device for the specified transaction file.

Page 28 of 30

COBOL/400 Quick Reference


On successful execution of the ACQUIRE statement the device is ready for further I/O
operations.

DROP
Syntax:
DROP {1. identifier FROM
file-name
2. literal}
This statement is used to release a device which has been acquired by the ACQUIRE
statement.

COMMITMENT CONTROL
To make the commitment control work we need to define the file in I-O-CONTROL.
When all the changes can be made permanent to the file, we issue a COMMIT or else if
all the changes should not reflect in the physical file, we issue a ROLLBACK.
Example:
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ACCOUNT-FILE ASSIGN TO DATABASE-ACCTMST
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD IS EXTERNALLY-DESCRIBED-KEY
FILE STATUS IS ACCOUNT-FILE-STATUS.
.
.
.
I-O-CONTROL.
COMMITMENT CONTROL FOR ACCOUNT-FILE.
.
.
.
PROCEDURE DIVISION.
MAIN-PARA.
OPEN I-O ACCOUNT-FILE.
.
.
.
IF <condition> THEN
COMMIT
ELSE
ROLLBACK
END-IF.

Page 29 of 30

COBOL/400 Quick Reference

DECLARATIVES
Declaratives is used to specify the set of statements which is to be performed when an
error occurred while operating on the specified file.
The USE statement can contain either the file-name itself or the mode of file ie INPUT,
OUTPUT, etc.
Example:
.
.
.
PROCEDURE DIVISION.
DECLARATIVES.
INPUT-FILE-ERROR SECTION.
USE AFTER STANDARD ERROR PROCEDURE ON INPUT-FILE.
INPUT-FILE-ERROR-PARA.
MOVE INPUT-FILE-STATUS TO SK.
MOVE "INPUT-FILE" TO FILE-NAME.
DISPLAY "FILE PROCESSING ERROR".
DISPLAY ERROR-INFO.
DISPLAY "PROCESSING TERMINATED DUE TO I-O ERROR".
STOP RUN.
I-O-FILE-ERROR SECTION.
USE AFTER STANDARD ERROR PROCEDURE ON MASTER-FILE.
MASTER-FILE-ERROR-PARA.
MOVE MASTER-FILE-STATUS TO SK.
MOVE "MASTER-FILE" TO FILE-NAME.
DISPLAY "FILE PROCESSING ERROR".
DISPLAY ERROR-INFO.
DISPLAY "PROCESSING TERMINATED DUE TO I-O ERROR".
STOP RUN.
END DECLARATIVES.

Page 30 of 30

Vous aimerez peut-être aussi