Você está na página 1de 91

1

Introduction Basics & Fundamentals


COBOL DAY - 2
2
Data Movement verbs.
Sequence Control verbs.
Types of conditions.
USAGE clauses.
Sequential File Processing
Design and development of sample programs.
Agenda for Day 2
3
The MOVE copies data from the source identifier or literal to
one or more destination identifiers.
The source and destination identifiers can be group or
elementary data items.
When the destination item is alphanumeric or alphabetic
(PIC X or A) data is copied into the destination area from left
to right with space filling or truncation on the right.
When data is MOVEd into an item the contents of the item
are completely replaced. If the source data is too small to
fill the destination item entirely the remaining area is zero or
space filled.
MOVE VERB
4
MOVE verb
{ } ... TO MOVE Identifier
Literal
Identifier

'

5
(1) MOVE
(2) MOVE . . . CORRESPONDING ( CORR )
(3) MOVE . . . OF . . . TO . . . OF
DATA movement verbs
6
Before
WS00-OUT1 BEST
WS00-OUT2 1234
Before
WS00-OUT1
WS00-OUT2 0
Before
WS00-OUT3 0786
After
WS00-OUT3 2345
Before
WS00-OUT4 PAYAL PAREKH
After
WS00-OUT4 SHUTI DEY
7
MOVE to a numeric item
When the destination item is numeric, or edited numeric, then
data is aligned along the decimal point with zero filling or
truncation as necessary.
When the decimal point is not explicitly specified in either the
source or destination items, the item is treated as if it had an
assumed decimal point immediately after its rightmost
character.
8
Before
WS00-OUT1 0000
WS00-OUT2 000000
Before
WS00-OUT1 3456
WS00-OUT2 345678
Before
WS00-OUT3 000000
After
WS00-OUT3 123456
Before
WS00-OUT4 00000000
After
WS00-OUT4 12345678
9
MOVE .. example
****************************
WS00-OUT1 : HARAYANA
WS00-OUT2 : HARAYANA
****************************
Output SPOOL
10
Is used to change the default type movement of alphabetic and
alphanumeric data.
Example
01 NAME PIC X(10) JUSTIFIED RIGHT.
MOVE KAJOL TO NAME.
Contents of NAME field is
bbbbbKAJOL
JUSTIFIED RIGHT clause
11
JUSTIFIED RIGHT clause ..
example
*********************************************
WS00-OUT1 : ABCDEFGHIJKLMNOPQRSTUVWXYZ
WS00-OUT2 : ABCDEFGHIJKLMNOPQRSTUVWXYZ
*********************************************
Output SPOOL
12
Facilitates movement of value of sub-item of a group item to a similar
named sub-item of another group item
Syntax
MOVE { CORRESPONDING, CORR } identifier-1
TO identifier-2
where identifier-1 and identifier-2 are group items.
MOVE CORRESPONDING
13
MOVE CORRESPONDING ..
example
****************************
WS00-GR2 : NISHANT 00000
****************************
Output SPOOL
14
Facilitates the movement of a particular field of a record to a particular field of
another record. (in other words it facilitates movement of value of a
individual/group item of one group item to an individual/group item of another
group item).
Example:
MOVE NAME OF STUD-REC TO
WS-NAME OF WS-STUD-REC.
MOVE . . . OF . . . TO . . . OF
15
Certain combinations of sending and receiving data types are not
permitted.
Alphabeti
c
Alphanu
meric
Edited
Alphan
umeric
Numeric Numeric
non
integer
Edited
numeric
Alphabetic Y Y Y N N N
Alphanumer
ic
Y Y Y Y Y Y
Edited
Alphanumer
ic
Y Y Y N N N
Numeric N Y Y Y Y Y
Numeric non
integer
N N N Y Y Y
Edited
numeric
N Y Y Y Y Y
Receiving field
S
e
n
d
i
n
g

f
i
e
l
d
LEGAL MOVES
16

GO TO

IF . . . THEN . . .

PERFORM

EVALUATE

STOP RUN
SEQUENCE CONTROL verbs
17

Syntax-1
GO TO paragraph-name.

Example
GO TO 400-READ-PARA.
GO TO Verb
18

Syntax-2

GO TO paragraph-name-1 [paragraph-name-2 ]
. . . DEPENDING ON identifier.

Example
GO TO 500-INSERT-PARA, 600-UPDATE-PARA,
700-DELETE-PARA DEPENDING ON TRANS-
CODE.
GO TO . . . DEPENDING ON . . .
19

Syntax-1

IF condition [ THEN ] {statement-1, NEXT
SENTENCE}
[ELSE {statement-2, NEXT SENTENCE}]
END-IF

Examples
(1)IF MARKS >= 80 THEN MOVE A TO GRADE
ELSE MOVE B TO GRADE
END-IF.
(2) IF NOT OK-BALANCE THEN MOVE 2 TO BALANCE-
CODE
ELSE NEXT-SENTENCE
END-IF
IF statement
20

Syntax-2 ( Nested IF )
IF condition-1 [ THEN ] statement-1
ELSE
IF condition-2 [ THEN ] statement-2
ELSE statement-3
END-IF
END-IF

Example
IF ( Var1 < 10 ) THEN DISPLAY Zero
ELSE
IF Var2 = 14 THEN DISPLAY First
ELSE DISPLAY Second
END-IF
END-IF
IF statement
21

Example
IF TIME < 2 AND TIME > 1
THEN MOVE SLOW TO SPEED
END-IF.
Is equivalent to
IF TIME < 2 AND > 1 THEN MOVE SLOW TO
SPEED.

Note: The following statement is invalid.


IF TOT-1 OR TOT-2 = 7 THEN DISPLAY The Sum is
7..
IF statement - Implied
Operands
22

Relational condition

Sign condition

Class condition

Compound condition

Condition-name
Classification of Conditions
23
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
Identifier
Literal
Arithmetic Expression
Identifier
Literal
Arithmetic Expression

'

'

'

IS
NOT GREATERTHAN
NOT >
NOT LESSTHAN
NOT <
NOT EQUALTO
NOT =
GREATERTHAN OR EQUALTO
>=
LESSTHAN OR EQUALTO
<=

Relational condition
24

Syntax

Example
IF DISCRIMINANT IS
NEGATIVE THEN
DISPLAY The roots are
imaginary.

'

ZERO
NEGATIVE
POSITIVE
] NOT [ IS Expression Arithmetic
Sign condition
25

Syntax

Example
IF REGNO IS NOT NUMERIC
THEN DISPLAY Records will not be
sorted.
IdentifierIS [NOT]
NUMERIC
ALPHABETIC
ALPHABETIC- LOWER
ALPHABETIC- UPPER
UserDefine dClassName

'

Class condition
26

Syntax
Condition-1 { AND, OR } Condition-2

Examples
(1) IF PERCENT > 80 AND TOTAL > 480
THEN MOVE A TO GRADE.
(2) IF ROW-NUMBER > 24 OR COLUMN > 80
THEN DISPLAY Page Error ! .
Compound Condition
27

Are essentially boolean variables.

Are always associated with data names called


condition variables.

Is defined in the DATA DIVISION with level


number 88.

Syntax
88 condition-name {VALUE IS, VALUES ARE } literal-1 [
{ THRU, THROUGH } literal-2 ].
Condition Names
28
01 MARITAL STATUS PIC 9.
88 SINGLE VALUE IS ZERO.
88 MARRIED VALUE IS 1.
88 WIDOWED VALUE IS 2.
88 DIVORCED VALUE IS 3.
88 ONCE-MARRIED VALUES ARE 1, 2, 3.
88 VALID-STATUS VALUES ARE 0 THRU 3.
PROCEDURE DIVISION Statements.
IF SINGLE SUBTRACT 125 FROM DEDUCTIONS.
IF ONCE-MARRIED ADD 300 TO SPECIAL-PAY.
IF MARRIED PERFORM B000-MARRIAGE-GIFT.
Condition Variable
Condition
Names
Condition-Names .. example
29

Condition Names are defined using the special level


number 88 in the DATA DIVISION of a COBOL program.

They are defined immediately after the definition of the


data item with which they are associated with.

We can use Condition Names for a group as well as an


elementary item.

A condition name takes the value TRUE or FALSE


depending on the value of the data item with which it is
associated. The VALUE clause of the associated data item
is used to identify the values which make the Condition
Name TRUE.
88 ConditionName
VALUE
VALUES

Literal
LowValue
THROUGH
THRU
HighValue

'


'

'

Defining Condition Names.


30
Before
WS00-MARKS 000
WS00-DISP
After
WS00-MARKS 050
WS00-DISP NOT CLEARED COMPRE
Before
WS00-MARKS 000
WS00-DISP
After
WS00-MARKS 081
WS00-DISP PASSED COMPRE
JCL
000100 //ER4857C JOB ,,NOTIFY=&SYSUID,CLASS=B
000500 //STEP1 EXEC PGM=COND88
000700 //STEPLIB DD DSN=OPERN.CICS3.LOADLIB,DISP=SHR
000800 //SYSIN DD *
000900 050
001000 081
001100 /*
31
Iteration constructs are used when we need to repeat the
same instructions over and over again in our programs.
Other programming languages have a variety of iteration /
looping constructs (e.g. WHILE, FOR, REPEAT). Each of these
in turn facilitate the creation of different types of iteration
structure.
In COBOL we have PERFORM verb which is used to create
these looping constructs. The PERFORM has several variations
each of which simulates different looping constructs of other
programming languages.
The PERFORM Verb
32

Simple PERFORM

In-line PERFORM

Nested PERFORM

PERFORM . . . THRU

PERFORM . . . UNTIL

PERFORM . . . TIMES

PERFORM . . . VARYING
PERFORM Verb - variations
33

Syntax
PERFORM Paragraph-Name.

Example
PERFORM 500-PROCESS-PARA.

This is not iterative but instructs the computer to


execute the chunk of code inside the mentioned
paragraph before reverting back to the sentence
following the PERFORM coded.
PERFORM Verb - Simple
PERFORM
34
PERFORM Verb Simple
PERFORM example
****************************************
WE ARE INSIDE B000-LAST-PARA
WE ARE INSIDE B001-FIRST-PARA
WE ARE INSIDE B002-MIDDLE-PARA
****************************************
Output SPOOL
35

Syntax
PERFORM imperative-statements.

Example
PERFORM
MOVE NUM-1 TO MAX
IF NUM-2 > MAX THEN MOVE NUM-2 TO
MAX
DISPLAY Maximum is MAX.
END-PERFORM
Lets see an example ..
PERFORM Verb - In-line
PERFORM
36
INLINE PERFORM PROGRAM
37
JCL FOR THE INLINE
PERFORM PROGRAM
38
When SYSIN data satisfies
the condition WS-STRING =
KARINA the scope of the
INLINE PERFORM gets
terminated
39

Syntax
Paragraph-Name-1.
PERFORM Paragraph-Name-2.
. . . . .
. . . . .
Paragraph-Name-2.
PERFORM Paragraph-Name-3.
. . . . .
. . . . .
Paragraph-Name-3.
MOVE A TO B.
. . . . .
. . . . .
PERFORM Verb Nested
PERFORM
40
PERFORM Verb Nested
PERFORM
****************************************
WE ARE INSIDE B000-LAST-PARA
WE ARE INSIDE B001-FIRST-PARA
WE ARE INSIDE B002-MIDDLE-PARA
****************************************
Output SPOOL
41

Syntax
PERFORM Paragraph-Name-1 [ { THRU, THROUGH }
Paragraph-Name-2 ].

Example
PERFORM 300-READ-PARA THRU 600-UPDATE-
PARA.
PERFORM Verb PERFORM
THRU
42
PERFORM THRU -
example
****************************
WE ARE INSIDE B000-DISP-PARA
WE ARE INSIDE B001-DISP-PARA
WE ARE INSIDE B002-DISP-PARA
****************************
Output SPOOL
43

Syntax
PERFORM Paragraph-Name-1 [ { THRU,
THROUGH }
Paragraph-Name-2 ] UNTIL
condition.

Example
PERFORM 300-READ-PARA UNTIL EOF = N.
PERFORM Verb PERFORM ..
UNTIL ..
44

Syntax
PERFORM Paragraph-Name-1 [ { THRU,
THROUGH }
Paragraph-Name-2 ]
[WITH TEST {BEFORE,
AFTER}]
UNTIL condition.

Example
PERFORM 300-PROCESS-PARA WITH TEST AFTER
UNTIL VALUE NOT = 0.
PERFORM Verb
PERFORM . . UNTIL .. WITH TEST AFTER OPTION
45

This format is used where the WHILE or REPEAT


constructs are used in other languages.

If the WITH TEST BEFORE phrase is used the


PERFORM behaves like a WHILE loop and the
condition is tested before the loop body is entered.

If the WITH TEST AFTER phrase is used the


PERFORM behaves like a REPEAT loop and the
condition is tested after the loop body is entered.

The WITH TEST BEFORE phrase is the default and


so is rarely explicitly stated.
PERFORM Verb
PERFORM . . UNTIL .. WITH TEST AFTER OPTION
46
PERFORM Verb
PERFORM . . UNTIL .. WITH
TEST BEFORE
****************************
****************************
Output SPOOL
47
PERFORM Verb
PERFORM . . UNTIL .. WITH
TEST AFTER
****************************
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
****************************
Output SPOOL
10 Times!! Why?
48

Syntax
PERFORM Paragraph-Name-1 [ { THRU, THROUGH }
Paragraph-Name-2 ] { integer, identifier }
TIMES.

Example
PERFORM 500-PROCESS-PARA THRU 800-END-PARA 8 TIMES.
PERFORM Verb PERFORM .. TIMES
49
PERFORM Verb PERFORM ..
TIMES Example
****************************
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
****************************
Output SPOOL
50

Syntax
PERFORM Paragraph-Name-1 [ { THRU, THROUGH }
Paragraph-Name-2 ] VARYING identifier-1 FROM
{identifier-2, integer-1} BY { identifier-3, integer-
2 }
UNTIL condition.

Example
PERFORM 500-WRITE-PARA
VARYING I FROM 1 BY 1
UNTIL I > 5.
PERFORM Verb - PERFORM . . . VARYING
51
PERFORM Verb -
PERFORM . . . VARYING
****************************
HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
****************************
Output SPOOL
52
Is used to transfer the control back to the statement following the
PERFORM statement from within a paragraph invoked by the
PERFORM statement.
Syntax
EXIT.
Note:
It is recommended to avoid using EXIT similar to GO TO since it is against
the idea of structured programming.
EXIT statement
53

The EVALUATE verb provides a very powerful construct to carry


out DATA validation. It is similar to the SWITCH statement in C
programs.
It assists us in implementing decision table logic.

Syntax
EVALUATE subject-1 [ ALSO subject-2 ] . . .
{ { WHEN object-1 [ ALSO object-2 ] . . . } . . . }
imperative-statement-1 } . . .
Where subject = { identifier, expression, TRUE, FALSE }
and object = { condition, TRUE, FALSE }.
EVALUATE Verb
54
[ ]
EVALUATE
Identifier
Literal
CondExpres sion
ArithExpre ssion
TRUE
FALSE

WHEN
ANY
Condition
TRUE
FALSE
NOT
Identifier
Literal
ArithExpre ssion

THRU
THROUGH

Identifier
Literal
ArithExpre ssion
Statement Block

'

'

'

'

1
]
1
1
1

'

'

'

[ ]


WHENOTHERStatement Block
END- EVALUATE
The Evaluate
55
EVALUATE Verb .. example
There are two valid ranges
which the logic checks for
1) Marks > 79
2) Marks > 64 & <= 79
*************************************
YOU HAVE CLEARED EXAM WITH A GRADE
*************************************
Output SPOOL
56

Syntax : STOP RUN.

Instructs the computer to terminate the program.

Closes all the files that were opened for file operations.

The STOP RUN is usually the last statement in the main


paragraph.
STOP RUN statement
57

Is used to specify the internal form in which the data is to be


stored.

Every variable will have an attached usage clause (even if


not declared by the programmer).

Syntax
USAGE IS {DISPLAY, COMPUTATIONAL, COMP} [ - {1, 2,
3}].
USAGE Clause
58

USAGE is DISPLAY

Each character of the data is represented in one byte

USAGE IS COMPUTATIONAL

The data item is represented as pure binary

USAGE IS COMP-1

The data item is represented as a single precision


floating point number (similar to real or float).

USAGE IS COMP-2

The data item is represented internally as Double


precision floating number (similar to Long or Double).

USAGE IS COMP-3

In decimal form but 1 digit takes half a byte (nibble).


The sign is stored as right most half a byte character.
USAGE Clause
59
Rules to followed while using USAGE clause

Usage clause cannot be used with data items declared with 66 or 88


levels.

Usage clause when declared for a group item, ensures that all the
sub-items of the group item default to the same USAGE clause as the
group items.
USAGE Clause
60
USAGE Clause .. example
B0001-POPULATE-FIELDS.
MOVE 99999 TO WS00-COMP-FORM
MOVE 99999 TO WS00-COMP1-FORM
MOVE 99999 TO WS00-COMP2-FORM
MOVE 99999 TO WS00-COMP3-FORM
B0002-DISPLAY-FIELDS.
DISPLAY '******************************************'
DISPLAY '* COMP DISPLAY IS : ' WS00-COMP-FORM
DISPLAY '* COMP1 DISPLAY IS : ' WS00-COMP1-FORM
DISPLAY '* COMP2 DISPLAY IS : ' WS00-COMP2-FORM
DISPLAY '* COMP3 DISPLAY IS : ' WS00-COMP3-FORM
DISPLAY '******************************************'
******************************************
* COMP DISPLAY IS : 99999
* COMP1 DISPLAY IS : .99999000E 05
* COMP2 DISPLAY IS : .99999000000000000E 05
* COMP3 DISPLAY IS : 99999
******************************************
Output SPOOL
61

Data Movement verbs. (MOVE)

Sequence Control verbs (IF,PERFORM,EVALUATE)

Types of conditions.

REDEFINES, RENAMES, USAGE clauses


Review
62

What is wrong with the following code


(1) IF A EQUALS B
MOVE 1 TO A
END IF
This should be ; IF A IS EQUAL TO B
Review questions
63

How many times will the paragraph named


100-PARA be executed by the following PERFORM
STATEMENT
PERFORM 100-PARA VARYING X
FROM 1 BY 1 UNTIL X=10
PERFORM 100-PARA VARYING X
FROM 1 BY 1 UNTIL X > 10
PERFORM 100-PARA VARYING X
FROM 0 BY 1 UNTIL X=10
9 TIMES
10 TIMES
10 TIMES
Review questions ..
64

State true or false

The justified clause can be used for any data type

The data item at the level 49 will always have a picture


clause

In a move statement, though the sending field is one,


the receiving fields may be more than one.
False
True
True
Review questions
65
Field Field type and Field size.
Record Record-Size, Fixed length records and
Variable length records.
File Master files, Transaction files, File
organization and File access method.
Introduction to File processing Basic terms

A file is a collection of data related to a set of entities and typically
exists on a magnetic tape or a disk.
The data contained in a file is logically organised into an ordered set of
data items and is known as a record.
The individual data items in a record are called its fields.

The records present in a file may be of fixed length or variable length.


In most applications, fixed length records are used. However in certain
cases it is essential that records be of different lengths. This happens
when some of the fields are irrelevant to a set of records. In case of
variable length records, usually the first four bytes are reserved for
record length.
Files, Records, Fields ..

Files, Records, Fields
VIEW ER4857.SEQ.FLOUT
Command ===>
****** ***************************** Top of Data **
=COLS> ----+----1----+----2----+----3----+----4
000001 1000MARY ANNA JOHN
000002 2000TIM MAYOR KRUEGER
000003 3000HANS OSLON
000004 4000KUMAR GAURAV
000005 5000SEVARATHNAM ALVA
000006 6000KIM JAE JING
****** **************************** Bottom of Data
Record occurrences in the file.
Record Layout / Structure /
Template. Data length of
CUSTOMERO would be
equal to LRECL of
ER4857.SEQ.FLOUT (FIXED
format)
This would be similar to the GROUP ITEM concept. CUSTOMERO would be the group item at 01 level
and corresponds to the individual record. So after the first write, ACCNOO would map to the first 4
bytes of data of the first record (1000) and NAMEO would map the next 36 bytes of data of the first
record (MARY ANNA JOHN ).

MVS uses the programmers description of the record to
set aside sufficient memory to hold one record at a time.
The memory allocated for storing a record is termed as
record buffer.
The link between the program and the records in the file is
maintained by the record buffer.
Specifying two buffers for a file is very effective. In this
case while the program reads record into one buffer, the
CPU processes the record already read into the other
buffer.
Record Buffers

Record Buffers
SEQFLO would be the
record buffer inside the
COBOL program for the
output file.
This is the actual file
which the record
buffer SEQFLO maps
on to in the COBOL
program
SELECT clause in the
FILE-CONTROL section
tells which are the files
in this program

Describing the record buffer in
COBOL
A description of the buffer
requirement for a file is
mentioned in the FD (File
Descriptor) entry in the
FILE SECTION of DATA
DIVISION.

Some considerations when we are using Files in our programs
When using multiple files in a program, we have to describe a
record buffer for each file.
Whenever we carry out a READ of a file, an instance of the file
record is copied from the file into the record buffer.
Similarly after copying data onto the output file buffer if we
carry out the WRITE operation then an instance of the file record
gets added into the file
Implications of Buffers
72
For a file to be accessed in a COBOL program we require following
information of the file
a) Data Organization
b) File access method
Organization and Access
73

Data Organization refers to the method in which data inside a file is


organized. COBOL recognizes the following broad classification of DATA
Organizations
a) Sequential

Simplest

Records placed in sequence of writing into file

Records can be read in the same order only


b) Relative

Every record can be accessed directly (or sequentially)

Each record has a unique number identifying it


c) Indexed

Most complex

Each record identified by a unique user defined key

Each record can be accessed either directly or sequentially


Data Organization
74
File access method refers to the method/sequence in which
the records of a file are processed.
Records of a relative or index file could be accessed either
directly (using key) or sequentially
However records of a sequential file can be processed only
sequentially.
File Access Method
75
It is the simplest of the file organizations with records arranged
serially, one after another. However they are least flexible
For reading the nth record, we would be required to carry out n reads
to reach the record we are looking for.
Sequential files may be Ordered Ordered or Unordered Unordered (Serial files)
No mechanism for insertion or deletion of record
Records can only be added at the end of the file
Sequential Organization
76
FILE-CONTROL paragraph for
sequential files
STUDENTS
DISK
//ER4857A JOB ,,NOTIFY=&SYSUID,CLASS=D,
// MSGLEVEL=(1,1),MSGCLASS=X
//*
//STEP1 EXEC PGM=SEQREAD
//STEPLIB DD DSNAME=OPERN.CICS3.LOADLIB,DISP=SHR
//SEQFL DD DSN=ER4857.SEQ.FLINP,DISP=SHR
//SYSOUT DD SYSOUT=*
JCL gives the
actual physical
file name
The JCL
77
FD file-name
[ RECORD CONTAINS integer-1 CHARACTERS ]
[ BLOCK CONTAINS integer-2 { RECORDS, CHARACTERS }]
[ LABEL { RECORD IS, RECORDS ARE } { STANDARD,OMITTED }]
[ DATA { RECORD IS, RECORDS ARE } identifier-1, identifier-2, . . . ]
FD entries for Fixed length
records
78
FD file-name

[ RECORD CONTAINS integer-1 TO integer-2 CHARACTERS ]

[ BLOCK CONTAINS integer-3 TO integer-4 CHARACTERS ]
[ LABEL { RECORD IS, RECORDS ARE } { STANDARD, OMITTED }]
[ DATA { RECORD IS, RECORDS ARE } identifier-1, identifier-2, . . . ]
FD entries for variable length
records
79
OPEN

READ
WRITE
REWRITE
CLOSE
File handling verbs
80

Syntax
OPEN {INPUT, OUTPUT, I-O, EXTEND} Filename-1 . . .
OPEN MODE
STATEMENT INPUT OUTPUT I-O EXTEND
READ
WRITE
REWRITE
OPEN verb

The READ statement makes it possible to get the file
record from the file onto the file buffer into the
program.
So to process all the records of a given file we would be
required to read all the records one by one and carry
out the required processing.
Again the file record buffer is our only connection with
the file and it is only able to store a single record at a
time.
The READ verb

READ verb syntax
Internal filename is given in the select
statement in FILE-CONTROL section. By
this time the file should be already opened
This mentions the identifier
name into which the file data
would be fetched into after the
read operation
So at any given point of time after the read statement has been executed,
there would be two copies of the data read. One would be the FD entry (i.e.
Internal Filename) and the other would be the identifier used in [INTO
Identifier] statement. The AT END traps the condition when the end of file
has been reached.
READ InternalFileName [NEXT] RECORD
[INTO identifier]
[NOT] AT END Statement block
END-READ
83
INSIDE THE COBOL PROGRAM
READ STATEMENT
1000MARY ANNA JOHN
2000TIM MAYOR KRUEGER
3000HANS OSLON
4000KUMAR GAURAV
5000SEVARATHNAM ALVA
6000KIM JAE JING
1000 ANNA JOHN N
ACCNO NAME EOF
2000 MAYOR KRUEGER N
3000 HANS OSLON N
4000 KUMAR GAURAV N
5000 SEVARATHNAM ALVA N
6000 KIM JAE JING N
Y

The WRITE statement is used to transfer data


from the record buffers into the output file.
[ ] WRITE
ADVANCING
AdvanceNum
MnemonicName
PAGE
RecordName FROM Identifier

BEFORE
AFTER

LINE
LINES

'

1
]
1

'

1
]
1
1
1
1
1
1
WRITE Syntax
85
001800 MAIN-PARA.
001900 OPEN OUTPUT SEQFL
002000 MOVE '1000' TO ACCNO
002100 MOVE 'RAJ KAPOOR' TO NAME
002200 WRITE CUSTOMER
002300 MOVE '2000' TO ACCNO
002400 MOVE 'DEV ANAND' TO NAME
002410 WRITE CUSTOMER
002600 MOVE '3000' TO ACCNO
002700 MOVE 'DILIP KUMAR' TO NAME
002710 WRITE CUSTOMER
002810 CLOSE SEQFL
002900 STOP RUN.
The WRITE statement is
used to write the output file
buffer data into the output
file
86
REWRITE is used to update an existing record in the file which has been read in the
program
Syntax
REWRITE record-name [FROM data-name]
{AFTER, BEFORE} ADVANCING integer {LINE, LINES, PAGE}
[END-REWRITE]
Note:
The REWRITE statement can only be used if the file is opened in the I-O
mode and its execution must be preceded by the successful READ
statement on the file.
The REWRITE statement replaces last read record
REWRITE verb
87

Syntax
CLOSE filename1

Releases the named files from the program.

If a file is stored on a magnetic tape, after the execution


of the CLOSE statement the tape is rewound.

Is optional for COBOL- 85.


CLOSE verb
88
Advantages
Slow - when the hit rate is low.
Complicated to change (insert, delete).
Fast - when the hit rate is high.
Most storage efficient.
Simple organization.
Dis-advantages
Sequential files - A Final Look
89
Review questions

If there are 15 records in the file, the __________ attempt


to read a record causes an AT END condition to be executed

READ statement should be followed by _____________

Write statement should be followed by ________________

Which of the following does not appear in the DATA


DIVISION

REDEFINES

JUSTIFIED

SELECT

WRITE statement cannot be used when a file opened in


EXTEND mode (True / False)
sixteenth
File name
Record name
90
Summary

Data Movement verbs.

Sequence Control verbs.

Types of conditions.

USAGE clauses.

Sequential File Processing

Design and development of sample programs


91
Thank You!

Você também pode gostar