Professional Documents
Culture Documents
The Unit
• Delivered as IT52/IS22
• Lecturing by Richard Bland, practical support from Mr WS
Johnstone
• Unit co-ordinator: Richard Bland
• Email addresses etc are on the syllabus sheet
• Course materials will be placed on the WWW at appropriate
times as the unit proceeds
• Daytime teaching
– 3 lectures per week, times on syllabus sheet
– 1 Practical session per week in 1A11 (starts week 3)
– 1 Tutorial per week (starts week 3)
– IT PGs have been allocated to groups, ISMs should see me
• Assessment
– A COBOL assignment (40%) and an exam (60%)
– Assignment date 3rd April
• Parkin, A. & Yorke COBOL for Students, 4th edition, Edward
Arnold, 1995
– or any other text based on the 85 standard
– if you buy MicroFocus COBOL (as used in this unit) for
your own machine, you get a reference manual as well
IT52/IS22 COBOL 1
11 March 2003
COBOL
IT52/IS22 COBOL 2
11 March 2003
Status Now
IT52/IS22 COBOL 3
11 March 2003
IT52/IS22 COBOL 4
11 March 2003
• If there is an association
– the file displays the icon of the application
– and that application starts when the icon is double-clicked
11 March 2003 IT52/IS22 COBOL 9
IT52/IS22 COBOL 5
11 March 2003
Hello, world
• We can compile the program (using the button with a tick on it,
or through the Compile/Run menu)
– if there are errors, we can edit and recompile (Animator will
always save an altered file before recompilation)
• If Animator is open in Edit+Execute mode, then we can run the
program: otherwise we close, & reopen the file in Execute mode
IT52/IS22 COBOL 6
11 March 2003
IT52/IS22 COBOL 7
11 March 2003
• When disks got big and cheap enough, programs were held on
disk and the need for the above format disappeared
– non-standard formats appeared
• However, Animator preserves the special nature of columns 1-7
and 73-80
• I shall (try to) show program examples with the first six
characters omitted, and using Area A and Area B conventionally
IT52/IS22 COBOL 8
11 March 2003
IT52/IS22 COBOL 9
11 March 2003
IT52/IS22 COBOL 10
11 March 2003
Echo: syntax
Syntax (continued)
IT52/IS22 COBOL 11
11 March 2003
Declaring variables
• Let us now turn to the Data Division of echo
DATA DIVISION.
WORKING-STORAGE SECTION.
77 x PIC 999.
• Before the detail, let's look at the big picture. The Data Division
of any COBOL program can consist of three Sections: File,
Working-Storage and Linkage. We shall not deal with the last.
The first two relate to COBOL's model of data movement.
• Parkin Figure E1
Data Division
File Section
Input File Input Record Output Record Output File
READ WRITE
Working-Storage Section
IT52/IS22 COBOL 12
11 March 2003
IT52/IS22 COBOL 13
11 March 2003
IT52/IS22 COBOL 14
11 March 2003
IT52/IS22 COBOL 15
11 March 2003
IT52/IS22 COBOL 16
11 March 2003
More verbs
ADD (continued)
– rounding is to the number of decimal places specified in the
PIC of the result. 5 and over rounds up.
• The ADD-GIVING version is
ADD {ident | literal} ...
GIVING {ident [ROUNDED]} ...
• (We can have an optional TO before the final input operand. I
have omitted this in the syntax.)
• In both cases, the ellipsis in the result comes after the optional
ROUNDED. So we can construct a total in number of different
ways: to different precisions and with/without rounding
ADD x,y,z GIVING Int-Ans ROUNDED, Real-Ans
• Let's write a complete program:
IT52/IS22 COBOL 17
11 March 2003
• The output is
b is 0178
d is 10
• The value for b is correct, but something nasty has happened to
d. What is the problem?
• It is this:
– the numbers to be added are aligned on their decimal points
– added
– and then moved to the result variable
– which in this case is too small
• In fact the syntax is more complicated: in both cases we can
have SIZE ERROR clauses
IT52/IS22 COBOL 18
11 March 2003
IT52/IS22 COBOL 19
11 March 2003
IT52/IS22 COBOL 20
11 March 2003
Defensive programming
IT52/IS22 COBOL 21
11 March 2003
SUBTRACT
• Subtract is very similar. First, the version without GIVING
SUBTRACT { ident | literal} ...
FROM {ident [ROUNDED]} ...
– The total of the values on the left is subtracted from each of
the variables on the right
SUBTRACT Tax, Insurance, Sports, Union FROM Pay
SUBTRACT 1 FROM Loop1, Loop2
• Then the version with GIVING
SUBTRACT { ident | literal} ...
FROM { ident | literal}
GIVING {ident [ROUNDED]} ...
– The total of the values on the left is subtracted from a single
value, and the result is stored, possibly in several variables
11 March 2003 IT52/IS22 COBOL 43
SUBTRACT (continued)
IT52/IS22 COBOL 22
11 March 2003
MULTIPLY
DIVIDE
DIVIDE {ident | literal}
INTO {ident [ROUNDED]} ...
• A single value on the left is divided-into each of a number of
variables on the right
DIVIDE {ident | literal}
INTO {ident | literal}
GIVING {ident [ROUNDED]} ...
• A value is divided into another value and the result is stored,
possibly in several variables.
DIVIDE 2 INTO x GIVING y, z
• We can write this the other way round using BY
DIVIDE x BY 2 GIVING y, z
• Can have size error clauses and END-DIVIDE (and REMAINDER)
11 March 2003 IT52/IS22 COBOL 46
IT52/IS22 COBOL 23
11 March 2003
COMPUTE
IT52/IS22 COBOL 24
11 March 2003
Transfer of control: IF
IF (continued)
IT52/IS22 COBOL 25
11 March 2003
IT52/IS22 COBOL 26
11 March 2003
IT52/IS22 COBOL 27
11 March 2003
• The example on the last two slides shows (in a trivial example)
how we can obtain clarity by moving details into subsidiary
paragraphs
• The first paragraph then corresponds to a top-level description of
the algorithm. We refer to this as the “main paragraph”
– for example, many programs have a three-phase structure:
initialise, process, tidy-up
* This is the Procedure Division's first para
Main-para.
PERFORM Start-up
PERFORM Process-data
PERFORM Tidy-up
STOP RUN.
* The code for the three paragraphs follows
Global variables
IT52/IS22 COBOL 28
11 March 2003
IT52/IS22 COBOL 29
11 March 2003
PROCEDURE DIVISION.
Main-para.
DISPLAY "Main-para".
A.
DISPLAY "A".
B.
DISPLAY "B1"
PERFORM A
DISPLAY "B2".
• What is the order of execution?
• So: we executed paragraph A twice
– the first time we fell into it (and so we fell out of it when it
was finished)
– the second time we jumped into it with PERFORM (and so we
jumped back when it was finished).
IT52/IS22 COBOL 30
11 March 2003
IT52/IS22 COBOL 31
11 March 2003
Types of loop
IT52/IS22 COBOL 32
11 March 2003
PERFORM-VARYING
* Form factorial n
DATA DIVISION.
WORKING-STORAGE SECTION.
77 i PIC 99.
77 n PIC 99.
77 fact PIC 99999.
PROCEDURE DIVISION.
DISPLAY "Enter n: " WITH NO ADVANCING
ACCEPT n
MOVE 1 TO fact
PERFORM Mult-para VARYING i FROM 1 BY 1
UNTIL i > n
DISPLAY n, "! is ", fact
STOP RUN.
Mult-para.
MULTIPLY i BY fact
ON SIZE ERROR
DISPLAY "Overflow”
STOP RUN
END-MULTIPLY.
IT52/IS22 COBOL 33
11 March 2003
IT52/IS22 COBOL 34
11 March 2003
IT52/IS22 COBOL 35
11 March 2003
Relation conditions
IT52/IS22 COBOL 36
11 March 2003
Class condition
Condition-names
IT52/IS22 COBOL 37
11 March 2003
Condition-names (continued)
IT52/IS22 COBOL 38
11 March 2003
Student-record
Hierarchical data
IT52/IS22 COBOL 39
11 March 2003
Level-numbers
IT52/IS22 COBOL 40
11 March 2003
IT52/IS22 COBOL 41
11 March 2003
MOVE (continued)
IT52/IS22 COBOL 42
11 March 2003
Corresponding
• There is a wonderful extension of MOVE. First we have to re-
visit our data declarations
• The names of data-items need not be unique: they must only be
unique within their wider group-item.
01 Home-address.
05 Street PIC X(20).
05 Town PIC X(20).
05 Postcode PIC X(8).
01 Work-address.
05 Street PIC X(20).
05 Town PIC X(20).
05 Postcode PIC X(8).
• We make references unique by qualification by OF or IN
MOVE "FK9 4LA" TO Postcode OF Work-address
Corresponding (continued)
IT52/IS22 COBOL 43
11 March 2003
Corresponding (continued)
• Assuming we have data in Student-record (from a file,
perhaps) we could say
MOVE CORRESPONDING Student-Record
TO Student-out
DISPLAY Student-out
• This example shows that the fields can be re-arranged in the two
group-items
• Also, there need not be a complete correspondence: fields (on
either side) that do not match are just ignored
• This kind of MOVE is very powerful, but
– you can lose track of what is copied and what is ignored
– it can involve the use of lengthy identifiers (as we shall see)
Files: Overview
IT52/IS22 COBOL 44
11 March 2003
IT52/IS22 COBOL 45
11 March 2003
IT52/IS22 COBOL 46
11 March 2003
• We shall put our READ into its own paragraph (because it’s
quite a long bit of code and we need it twice)
Read-record.
READ File-in
AT END
MOVE "Y" TO File-in-eof-flag
CLOSE File-in
END-READ.
• (Remember that the flag has an associated condition-name: we
shall use that to control the read loop)
• Here’s the whole of the Procedure Division:
IT52/IS22 COBOL 47
11 March 2003
Main-para.
OPEN INPUT File-in
PERFORM Read-record
PERFORM UNTIL File-in-ended
* Process the record (just display)
DISPLAY Student-record
PERFORM Read-record
END-PERFORM
STOP RUN.
Read-record.
READ File-in
AT END
MOVE "Y" TO File-in-eof-flag
CLOSE File-in
END-READ.
IT52/IS22 COBOL 48
11 March 2003
IT52/IS22 COBOL 49
11 March 2003
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT File-in ASSIGN TO "Input.dat”
ORGANIZATION IS LINE SEQUENTIAL.
SELECT File-out ASSIGN TO "Output.dat”
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD File-in.
01 Student-record.
* etc
FD File-out.
01 Student-out.
* etc
IT52/IS22 COBOL 50
11 March 2003
WORKING-STORAGE SECTION.
01 Flags.
05 File-in-eof-flag PIC A VALUE "N".
88 File-in-ended VALUE "Y".
PROCEDURE DIVISION.
Main-para.
OPEN INPUT File-in
OUTPUT File-out
PERFORM Read-record
PERFORM UNTIL File-in-ended
MOVE CORRESPONDING Student-record
TO Student-out
WRITE Student-out
PERFORM Read-record
END-PERFORM
CLOSE File-out
STOP RUN.
* Read-record para just as before
Edited PICs
IT52/IS22 COBOL 51
11 March 2003
IT52/IS22 COBOL 52
11 March 2003
IT52/IS22 COBOL 53
11 March 2003
IT52/IS22 COBOL 54
11 March 2003
CURRENCY SIGN
More on comma
IT52/IS22 COBOL 55
11 March 2003
More on full-stop
IT52/IS22 COBOL 56
11 March 2003
IT52/IS22 COBOL 57
11 March 2003
REDEFINES (continued)
This gives the output
Smith GBP****27.23
• However, if we use the other identifier:
MOVE "Kawasaki" TO Client-name
MOVE "JYE" TO Client-currency
* 183 Yen to the £, and 27.23 * 183 = 4983
MOVE 4983 TO Client-balance-yen
DISPLAY Client-Record
Then we obtain
Kawasaki JYE****4,983
• Formal syntax is
level new-id REDEFINES old-id [pic-clause]
IT52/IS22 COBOL 58
11 March 2003
REDEFINES (continued)
• Some rules:
– level numbers must be the same and with no lower number
intervening:
05 this
04 is
05 wrong REDEFINES this
– if in FILE SECTION, not 01 (same effect achieved with other
means, as we have seen)
– either/both item(s) can be elementary (as in the currency
example) or group
05 Weight-metric PIC 9V999.
05 Weight-imperial REDEFINES Weight-metric.
10 Weight-lbs PIC 99.
10 Weight-oz PIC 99.
REDEFINES (continued)
– same original item can be redefined more than once
05 This PIC 99.
05 That REDEFINES This PIC AA.
05 Other REDEFINES This PIC XX.
– cannot redefine the redefinitions
05 This PIC 99.
05 That REDEFINES This PIC AA.
05 Wrong REDEFINES That PIC XX.
– redefinitions cannot have VALUE (why?)
IT52/IS22 COBOL 59
11 March 2003
IT52/IS22 COBOL 60
11 March 2003
IT52/IS22 COBOL 61
11 March 2003
• We declare the same area twice, once with coarser structure and
constants, then again in full detail
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Airports.
03 Airport-strings.
05 PIC A(24) VALUE "LHR*London Heathrow" .
05 PIC A(24) VALUE "LCY*London City" .
05 PIC A(24) VALUE "GLA*Glasgow" .
05 PIC A(24) VALUE "EDI*Edinburgh" .
03 Airport-table REDEFINES Airport-strings.
05 Airport-line OCCURS 4 TIMES.
10 Airport-code PIC A(3).
10 PIC X.
10 Airport-name PIC A(20).
IT52/IS22 COBOL 62
11 March 2003
Multi-dimensional tables
IT52/IS22 COBOL 63
11 March 2003
IT52/IS22 COBOL 64
11 March 2003
IT52/IS22 COBOL 65
11 March 2003
IT52/IS22 COBOL 66
11 March 2003
IT52/IS22 COBOL 67
11 March 2003
• Now that we have built the table, we could write our own code
to search the table from 1 ... Table-size
• But how could we use the clever built-in SEARCHes?
• As it stands, they would search the whole of the declared space
of the table (all 10,000 rows) and not just the useful data
• The answer to this problem is DEPENDING ON
• So the full declaration of the table now is
05 Airport-line OCCURS 10000 TIMES
DEPENDING ON Table-size
ASCENDING KEY Airport-code
INDEXED BY Airport-idx.
10 Airport-code PIC X(4) .
10 PIC X.
10 Airport-name PIC X(16).
IT52/IS22 COBOL 68
11 March 2003
Sorting
• We shall sort some live data: all 20,639 grades obtained in units
in Autumn 2002. This file is 314 Kbytes in size
• A few records:
1023197 NM77 2C
1023180 NM77 2B
1023176 NM77 2D
1023195 NM77 1B
• The file is in no particular order. Let's sort it on registration-
number within unit
IT52/IS22 COBOL 69
11 March 2003
IT52/IS22 COBOL 70
11 March 2003
• So far, the COBOL sort gives us little more than we could have
obtained from the system’s built-in sort (in MS-DOS, Unix ..)
• But we can add our own programming ..
• We can manipulate (even create) the records that are input to the
sort
• The USING filename part of the verb is replaced by
INPUT PROCEDURE IS proc1 [THRU proc2]
• The procs are the names of paragraphs (or sections) in the
Procedure Division. (Sections, as in Check-input SECTION. ,
are a way of giving the following paragraphs a collective name)
• This is sometimes called “own-coding” the input to the sort
IT52/IS22 COBOL 71
11 March 2003
• The basic idea is that our code repeatedly places values in the
fields of the work-file record and “releases” each new record to
be included in the sort. The form is
SORT workfile
ASCENDING KEY field
INPUT PROCEDURE IS para
GIVING outfile.
...
para.
<loop>
<form workfile record>
RELEASE <workfile record>
<end-loop> .
• It is as if the own-coding “writes” a sequence of records into the
input of the sort. Often it reads (another) file to get the data
11 March 2003 IT52/IS22 COBOL 143
Telephone bills
IT52/IS22 COBOL 72
11 March 2003
IT52/IS22 COBOL 73
11 March 2003
FD File-out.
01 Call-out PIC X(9)
SD Work-file.
01 Call-charge-record.
05 Extension PIC 9999.
05 Call-charge PIC 9999V9.
• We need a a table to give the cost for each band. In a real
application we would read this from a file
WORKING-STORAGE SECTION.
01 Tariff-bands.
05 Three-costs PIC X(12)
VALUE "010*025*060*".
05 REDEFINES Three-costs.
10 OCCURS 3 TIMES.
15 Tariff PIC 99V9.
15 PIC X.
IT52/IS22 COBOL 74
11 March 2003
IT52/IS22 COBOL 75
11 March 2003
• We'll skip the details of the coding: but the main point is
– Sum-bill will “read” the sort’s output as if it were a file
– it has a read-loop, and detects end-of-file, in the same way
– except it uses the verb RETURN instead of READ
– just as input coding is like “writing into the sort”, so output
coding is like “reading from the sort”: RELEASE is like
WRITE , RETURN is like READ
IT52/IS22 COBOL 76
11 March 2003
IT52/IS22 COBOL 77
11 March 2003
• The first kind can be dealt with in a first pass, using a stand-
alone program, generating its own error log and suspense file(s)
• Then we execute the main pass, merging transaction and master
files
Suspense file
Transaction file Pass 1
Log file
Suspense file
Log file
11 March 2003 IT52/IS22 COBOL 156
IT52/IS22 COBOL 78
11 March 2003
• Remember, both the transaction and master files are sorted (by
e.g. client-number)
• The transaction records will be of three types:
– update (so there should be a matching master record)
– insertion (there should not be a matching master record)
– deletion (there should be a matching master record)
• A neat solution must deal cleanly with the ends of the files: it
greatly simplifies the algorithm if we use sentinels: dummy extra
records with keys higher than any valid key
– so the Master, Transaction and Suspense files are all
maintained with sentinel records at the end
• It's neat if the suspense file is the error log as well
– containing the dud transactions plus error messages
11 March 2003 IT52/IS22 COBOL 157
A sample problem
IT52/IS22 COBOL 79
11 March 2003
IT52/IS22 COBOL 80
11 March 2003
Indexes
• (Most of) the index is held in memory: (most of) the main file is
not.
11 March 2003 IT52/IS22 COBOL 162
IT52/IS22 COBOL 81
11 March 2003
Managing indexes
IT52/IS22 COBOL 82
11 March 2003
IT52/IS22 COBOL 83
11 March 2003
IT52/IS22 COBOL 84
11 March 2003
IT52/IS22 COBOL 85
11 March 2003
EVALUATE (continued)
EVALUATE (continued)
IT52/IS22 COBOL 86
11 March 2003
String handling
IT52/IS22 COBOL 87
11 March 2003
IT52/IS22 COBOL 88