Enterprise COBOL Concepts: Dr. David Woolbright Woolbright - David@columbusstate - Edu 2013
Enterprise COBOL Concepts: Dr. David Woolbright Woolbright - David@columbusstate - Edu 2013
Why Cobol?
Billions of lines of existing code with more
added each year
Designed for business
Great compilers
Runs fast
Relatively simple to learn
The language keeps evolving
http://www-01.ibm.com/software/awdtools/cobol/zos/library/
Languagage Reference
http://publibfp.boulder.ibm.com/epubs/pdf/igy3lr50.pdf
Programming Guide
http://publibfp.boulder.ibm.com/epubs/pdf/igy3pg50.pdf
Program Organization
Grammatical Hierarchy
Paragraphs
Entries
Environment division
Sections
Paragraphs
Entries
Clauses
Phrases
Data division
Sections
Entries
Clauses
Clauses
Phrases
Procedure division
Sections
Paragraphs
Sentences
Statements
Phrases
Coding Rules
Continuation of Statements
Continuation of Literals
Division headers
Section headers
Paragraph headers or paragraph names
Level indicators or level-numbers (01 and 77)
DECLARATIVES and END DECLARATIVES
End program, end class, and end method markers
Things That Go in A or B
Area A or B
Level-numbers
Comment lines
Compiler-directing statements
Debugging lines
Pseudo-text
Structure of a Program
IDENTIFICATION DIVISION
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
AUTHOR. JOE SMITH.
INSTALLATION. TSYS.
DATE-WRITTEN. 12/03/2011.
DATE-COMPILED. 12/03/2011.
ENVIRONMENT DIVISION
This division connects external DD file names with internal file names.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MSTRFILE ASSIGN TO MSTRFILE
SELECT CUSTOMER-FILE
ASSIGN TO CUSTMAST
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS COSTOMER-KEY
FILE STATUS IS CUSTOMER-FILE-STATUS.
Internal File
Name
External DD
File Name
DATA DIVISION
Used to create variables and constant fields
Only three data types
numeric
PIC 99999.
alphanumeric (text/string) PIC XXX.
alphabetic
PIC AAA.
Level numbers indicate subordination of fields. Use
levels 01-49
Alphabetic is seldom used
Level Numbers
Level Numbers
Level Numbers
01
77
XXX.
05 YYY.
10
10
05 ZZZ
AAA
AAA PIC X.
BBB PIC X.
PIC X(20).
PIC 999V99.
Condition Names
01
TRAN-CODE
PIC X.
88
GOOD
88
BAD
88
INDIFFERENT
Equivalent to
IF (GOOD)
VALUE G.
VALUE B.
VALUE I.
MOVE G TO TRAN-CODE
Equivalent to IF TRAN-CODE = G
Level 88
Condition Names
Picture Clauses
Picture clause values usually use 9, X, V,
S, A
9 a decimal digit
X any alphanumeric character
V an implied decimal point
S a sign
A A-Z, and blank
Picture Clauses
PIC 9(6)
PIC 9(6)V99
PIC 999999V99
PICTURE X(10)
PIC XXXXXXXXXX
PIC S9(4)V9(4)
PIC S9999V9999
PIC 9(18)
XXXBXXBXXXX
99/99/99
ZZ,ZZZ.99DB
***,***.99
----.99
$$$9.99
99999.99
USAGE Clause
Specifies the format in which data is stored in
memory
Normally, the phrase USAGE IS is omitted
01 COST
01 COST
01 FIRST-NAME
01 FIRST-NAME
DATA DIVISION
We define data used in input-output operations.
FILE SECTION.
FD CUSTOMER-FILE.
01 CUSTOMER-MASTER.
05 CUST-NUM
05 CUST-FNAME
05 CUST-LNAME
FD SALES-REPORT.
01
REPORT-AREA
PIC
PIC 9(2).
PIC X(20).
PIC X(20).
X(132).
Data Formats
Older terms:
Modern terms:
BINARY
BINARY
FLOATING POINT
FLOATING POINT
PACKED-DECIMAL
BINARY
BINARY (NATIVE)
PACKED-DECIMAL.
BINARY.
COMPUTATIONAL
COMP
COMP-1
COMP-2
COMP-3
COMP-4
COMP-5
05 XDATA PIC S9(5)
05 YDATA PIC S9(4)
EBCDIC
EBCDIC is an IBM format for storing alphanumeric
characters
A - XC1
J - XD1
B - XC2
K - XD2
S XE2
C - XC3
L - XD3
T XE3
D - XC4
M - XD4
U XE4
E - XC5
N - XD5
V XE5
F - XC6
O - XD6
W XE6
G - XC7
P - XD7
X XE7
H - XC8
Q - XD8
Y XE8
I - XC9
R - XD9
Z XE9
EBCDIC
EBCDIC is an IBM format for storing alphanumeric
characters
0 - XF0
SPACE X40
1 XF1
.
- X4B
2 - XF2
,
- X6B
3 XF3
*
- X5C
4 - XF4
- X60
5 XF5
6 - XF6
7 XF7
8 XF8
9 XF9
BINARY DATA
Stored in 2s Complement format
Leftmost bit is a sign ( 0 +, 1 - )
If the number is positive, interpret it as plain binary
01011 = 8 + 2 + 1 = + 11
If the number is negative, compute the complement
Invert. (Change all 1s to 0s and 0s to 1s.) Add 1. The
result is the additive complement
BINARY DATA
10011 is a negative number.
Inverting we have 01100.
Adding 1 we have 01100 + 1 = 01101. This is a positive
number. 01101 8 + 4 + 1 = 13, so the original number
is -13.
BINARY DATA
Declaring a data field as BINARY causes the
data to be stored in 2s complement format.
01
X-FIELD
PACKED-DECIMAL DATA
ZONED-DECIMAL DATA
A numeric field which is described as DISPLAY, or in
which the usage clause is omitted, is stored in a zoned
decimal format.
In zoned decimal, each digit takes up one byte, and a
sign is stored in the zone portion of the rightmost byte of
the field.
Z-FIELD PIC S999 VALUE -32
produces a 3 byte field containing XF0F3D2.
ZONED-DECIMAL DATA
Z-FIELD PIC S999 VALUE -32.
produces a 3 byte field containing XF0F3D2.
Z-FIELD PIC S999 VALUE 32.
produces a 3 byte field containing XF0F3C2.
W-FIELD PIC 999 VALUE 0.
MOVE -32 TO W-FIELD
produces a 3 byte field containing XF0F3C2.
DATA DIVISION
Define the data needed for internal processing in the
WORKING-STORAGE SECTION.
Storage is statically allocated and exists for the life of the
run unit.
WORKING-STORAGE SECTION.
01 TOTAL-FIELDS.
05 CUST-TOTAL
05 COST-TOTAL
01 DATE-AND-TIME.
05 CD-YEAR
05 CD-MONTH
DATA RELATIONSHIPS
BINARY
PACKEDDECIMAL
CHARACTER or
ALPHANUMERIC
ZONEDDECIMAL
DATA DIVISION
PIC X(40).
PIC 999.
DATA DIVISION
The LOCAL-STORAGE SECTION is used to have
storage allocated each time a program is
entered, and deallocated on return from the
program. Used for compatibility with C or Java.
LOCAL-STORAGE SECTION.
01 CUST-NO
PIC X(3).
01 COST
PIC 9(5)V99.
Initialization of Storage
WORKING-STORAGE for programs is allocated at
the start of the run unit.
Any data items with VALUE clauses are
initialized to the appropriate value at that time.
Initialization of Storage
For the duration of the run unit, WORKING-STORAGE
items persist in their last-used state. Exceptions are:
1) A program with INITIAL specified in the PROGRAMID paragraph In this case, WORKING-STORAGE data
items are reinitialized each time the program is entered.
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN IS INITIAL.
...
Initialization of Storage
For the duration of the run unit, WORKING-STORAGE
items persist in their last-used state. Exceptions are:
REDEFINES
01 MONTH-NAMES.
05 STRING-1 PIC X(15)
VALUE JANFEBMARAPRMAY.
05 MONTH REDEFINES STRING-1
OCCURS 5 TIMES PIC XXX.
MOVE MONTH(3) TO MONTH-OUT
REDEFINES
05
05
10
10
AMOUNT
AMOUNTX
XFIELD
YFIELD
20 A
20 B
PIC ZZ9.9-.
REDEFINES AMOUNT
PIC X(6).
PIC 9(5).
REDEFINES XFIELD.
PIC X(3).
PIC X(2).
Literals
String Literals enclosed in quotes (single
or double)
MOVE "INVALID" To CUST-NAME
Literals
Hexadecimal literals with X
MOVE XAF3B TO CUST-CODE
Constants
A constant is a data item that has only one value and it
can never change
Unfortunately, COBOL does not define a construct
specifically for constants
Moral: All values are subject to change
Data Division.
01 Report-Header pic x(50)
value "Company Report".
01 Interest
pic 9v9999
value 1.0265.
Figurative Constants
There are some figurative constants supplied by the
language:
ZERO
- an appropriate form of 0
SPACE
- x40
HIGH-VALUES - binary 1s
LOW-VALUES - binary 0s
QUOTE
- a single quote
NULL
- binary 0s used for pointers
ALL - Technically not a figurative constant:
X PIC X(5) VALUE ALL 3.
Tables (Arrays)
A table is a set of logically consecutive data items that
you define in the DATA DIVISION by using the
OCCURS clause.
01 TABLE.
05 TABLE-ENTRY OCCURS 10 TIMES.
10
NUM
PIC 99.
10
NAME
PIC X(30).
10
ITEM
PIC X(5) OCCURS 3 TIMES.
Referencing a Table
01 TABLE.
05 TABLE-ENTRY
10
NUM
10
NAME
10
ITEM
OCCURS 10 TIMES.
PIC 99.
PIC X(30).
PIC X(5) OCCURS 3 TIMES.
Subscripts vs Indexes
Subscripts are defined separately from the table
definitions.
01 MYTABLE.
05 ITEM PIC X(3) OCCURS 10 TIMES.
01 I
PIC 9(4) BINARY.
...
MOVE 1 TO I
MOVE ABC TO ITEM(I)
Subscripts are numeric fields choose BINARY fields for
efficiency, although packed and zoned fields also work
Subscripts vs Indexes
Subscripts can be easily printed
01
01
MYTABLE.
05 ITEM PIC X(3) OCCURS 10 TIMES.
I
PIC 9(4) BINARY.
...
MOVE 1 TO I
MOVE ABC TO ITEM(I)
DISPLAY I
Subscripts vs Indexes
Subscripts represent an occurrence number, 1 is
the first occurrence, 2 is the second,
01
01
MYTABLE.
05 ITEM PIC X(3) OCCURS 10 TIMES.
I
PIC 9(4) BINARY.
...
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
DISPLAY ITEM(I)
END-PERFORM
Subscript Program
Subscripts vs Indexes
Indexes are created when a table is defined
01 MYTABLE.
10
10
LETTERVALS
PIC X(10) VALUE 'ABCDEFGHIJ'.
LETTER REDEFINES LETTERVALS
PIC X(1) OCCURS 10 TIMES
INDEXED BY I.
Subscripts vs Indexes
Indexes are generally more efficient than subscripts
Indexes represent offsets from the beginning of the table
SET I TO 1
Causes I to have the binary value 0 internally.
It takes a bit of work to print them
Index Program
A Testy Program
PROCEDURE DIVISION
The PROCEDURE DIVISION is where you code the
executable statements in your COBOL program
Divided into Paragraphs (terminated with periods):
100-MAIN.
DISPLAY HELLO
PERFORM 200-SUB
GOBACK
.
200-SUB.
DISPLAY WORLD!
.
PROCEDURE DIVISION
To resolve ambiguity caused by not using
periods, we will use statement delimiters:
END-IF
END-PERFORM
END-COMPUTE
...
DISPLAY
DISPLAY
Handy for debugging and simple report
creation
DISPLAY X Y Z
DISPLAY "A = " A
Data is written to SYSOUT
MOVE
MOVE STATEMENT
Used to copy data from one field to
another
Example MOVE X-FIELD TO Y-FIELD Z-FIELD
MOVE STATEMENT
To move data from one field to another field, the two
fields should be compatible but dont have to be
identically pictured
Alphanumeric - PIC X(10)
Numeric
- PIC 999v99
Numeric-Edited - PIC 999.99Compatible moves:
-Alphanumeric to Alphanumeric
-Numeric to Numeric
-Numeric to Numeric edited
MOVE STATEMENT
Compatible moves:
-Alphanumeric to Numeric if the sending field is an
unsigned integer
-Alphanumeric to Numeric edited if the sending field is an
unsigned integer
-Numeric to Alphanumeric if the sending field is an
unsigned integer
MOVE STATEMENT
Compatible moves:
-Numeric edited fields can be sent to Numeric and
Numeric edited fields this causes a de-edit process to
occur
-Numeric edited fields can be sent to Alphanumeric and
Alphanumeric edited fields this causes a de-edit
process to occur
MOVE STATEMENT
Moving data can cause data conversions
to occur.
01
01
X
Y
MOVE X TO Y
MOVE STATEMENT
If the receiving field is larger than the sending
field, the receiving field is filled with leading 0s
in a numeric move:
01
01
X
Y
MOVE STATEMENT
If the receiving field is larger than the sending
field, the receiving field is filled with trailing
spaces in a alphanumeric move.
01
01
X
Y
MOVE STATEMENT
If the receiving field is smaller than the sending
field, data will be truncated on the left for
numeric moves and on the right for
alphanumeric moves
01
01
01
01
X
Y
A
B
WORK.
05
A-FIELD
05
B-FIELD
PIC X(3).
PIC S999V99.
A
B
C
PIC
PIC
PIC
MOVE A TO B
MOVE A TO C
S999V99.
ZZ9.99-.
S9(5)V9999 PACKED-DECIMAL.
Zoned to Numeric-edited
Zoned to Packed-decimal
MOVE CORRESPONDING
MOVE CORRESPONDING
identifier-1 TO identifier-2
CORR
Usually a bad idea
Both identifiers must name group items.
Elementary items with the same name are moved.
01
A-GROUP.
01 B-GROUP.
05 W
PIC X(3).
05
05 X
PIC X(2).
05
05 Y
PIC 999.
05
MOVE CORRESPONDING A-GROUP TO B-GROUP
W to W, X to X, Y to Y
W
X
Y
PIC X(3).
PIC X(2).
PIC 999.
MOVE CORRESPONDING
Subordinate items must not be identified by the keyword
FILLER
No reference modification for either identifier
Subordinate items must not include a REDEFINES,
RENAMES, OCCURS, INDEX or POINTER description
01
A-GROUP.
01 B-GROUP.
05 W
PIC X(3).
05
05 X
PIC X(2).
05
05 Y
PIC 999.
05
MOVE CORRESPONDING A-GROUP TO B-GROUP
W to W, X to X
P
X
W
PIC X(3).
PIC X(2).
PIC 999.
INITIALIZE
INITIALIZE
SPACE is the implied sending item for
receiving items of category alphabetic,
alphanumeric, alphanumeric-edited,
DBCS, national, or national-edited.
ZERO is the implied sending item for
receiving items of category numeric or
numeric-edited.
INITIALIZE
01 PRICE-FIELDS.
05 UNIT-PRICE
PIC 9(5)V9(2) PACKED-DECIMAL.
05 DISCOUNT
PIC V9(2).
05 UNIT-CODE
PIC XX.
05 SALES-PRICE
PIC S9(4) BINARY.
. . .
INITIALIZE PRICE-FIELDS
ADD
ADD Semantics
All identifiers or literals that precede the keyword
TO are added together, and this sum is added to
and stored in identifier-2. This process is
repeated for each successive occurrence of
identifier-2 in the left-to-right order in which
identifier-2 is specified.
ADD X Y Z TO P Q
Before X=1, Y=2, Z=3, P=4, Q=6
After X=1, Y=2, Z=3, P=10, Q=12
ADD EXAMPLES
ADD
ADD
ADD
ADD
ADD
ADD
X
X
X
1
X
X
TO Y
Y Z TO P
Y TO P Q
TO Z
TO Y ROUNDED
TO Y
ON SIZE ERROR
DISPLAY ADD ERROR
END-ADD
ADD GIVING
ADDGIVING Semantics
All identifiers or literals that precede the keyword TO are
added together, and this sum is added to identifier-2 to
obtain a temporary sum. (Identifier-2 is unchanged)
The the temporary sum is moved to identifier-3.
ADD X Y Z TO V GIVING P
Before X=1, Y=2, Z=3, V=4, P=6
After X=1, Y=2, Z=3, V=4, P=10
SUBTRACT
SUBTRACT
All identifiers or literals preceding the keyword FROM are
added together and their sum is subtracted from and
stored immediately in identifier-2. This process is
repeated for each successive occurrence of identifier-2,
in the left-to-right order in which identifier-2 is specified.
SUBTRACT X Y FROM P Q
Before: X=1,Y=2, P=3,Q=4
After: X=1,Y=2, P=0,Q=1
SUBTRACT
SUBTRACT Semantics
All identifiers or literals preceding the keyword FROM are
added together and their sum is subtracted from
identifier-2 to obtain a temporary value which is moved
to identifier-3.
MULTIPLY
MULTIPLY Semantics
In format 1, the value of identifier-1 or literal-1 is
multiplied by the value of identifier-2; the product is then
placed in identifier-2. For each successive occurrence of
identifier-2, the multiplication takes place in the left-toright order in which identifier-2 is specified.
MULTIPLY X BY P Q
Before: X=2,P=4,Q=5
After: X=2,P=8,Q=10
MULTIPLY
MULTIPLY
In format 2, the value of identifier-1 or literal-1 is
multiplied by the value of identifier-2 or literal-2. The
product is then stored in the data items referenced by
identifier-3. Identifier-2 is unchanged.
MULTIPLY X BY Y GIVING Z
Before: X=2, Y=3, Z=4
After: X=2, Y=3, Z=6
DIVIDE
DIVIDE
In format 1, the value of identifier-1 or literal-1 is divided
into the value of identifier-2, and the quotient is then
stored in identifier-2. For each successive occurrence of
identifier-2, the division takes place in the left-to-right
order in which identifier-2 is specified.
DIVIDE X INTO Y Z
Before: X=3, Y=7, Z=12
After: X=3, Y=2, Z=4
DIVIDE
DIVIDE
In format 2, the value of identifier-1 or literal-1 is divided
into the value of identifier-2 or literal-2. The value of the
quotient is stored in each data item referenced by
identifier-3.
DIVIDE
DIVIDE
In format 3, the value of identifier-1 or literal-1 is
divided by the value of identifier-2 or literal-2.
The value of the quotient is stored in each data
item referenced by identifier-3.
DIVIDE X BY Y GIVING Z
Before: X = 10, Y = 3, Z = 1
After: X = 10, Y = 3, Z = 3
DIVIDE
DIVIDE
In format 4, the value of identifier-1 or literal-1 is divided
into identifier-2 or literal-2. The value of the quotient is
stored in identifier-3, and the value of the remainder is
stored in identifier-4.
DIVIDE X INTO Y
GIVING Z
REMAINDER R
Before: X = 2, Y = 9, Z = 8, R = 7
After: X = 2, Y = 9, Z = 4, R = 1
COMPUTE
COMPUTE
05
PIC
Arithmetic Operators
Operation
Operator
Addition
Subtraction
Multiplication
Division
**
Exponentiation
Input Buffers
Output Buffers
Region
WRITE RECOUT
FROM MYREC
01 MYREC PIC X(80).
Input Buffers
Output Buffers
Region
QSAM
Queued Sequential Access Method
For input files, records are buffered when the file
is OPENed
For output, records are buffered before being
written
Records are processed from the beginning
record sequentially to the end of the file
Very efficient access method for sequential files
Sometimes referred to as flat files
OPEN
CLOSE
READ
File Reading
READ MY-INPUT-FILE INTO MY-REC
AT END MOVE NO TO MORE-RECS
END-READ WRITE
normal
end of file
invalid key
permanent i/o error
logic error
unsuccessful operation
Exercise #1
Create a file of 80 byte records
Each record has 3 fields
AFIELD ZONED DECIMAL
with 4 DIGITS & 2 DECIMALS
BFIELD PACKED DECIMAL
with 7 DIGITS & 3 DECIMALS
CFIELD - PACKED DECIMAL
with 7 DIGITS & 1 DECIMAL
Print a report with a column for each field and a column for the
computed value :
(AFIELD + BFIELD)/ CFIELD
Print the result with 2 decimals rounded.
Total each column.
FLOW OF CONTROL
There is a theoretical result in Computer Science by two
Italian mathematicians, Boehm and Jacopini, that states
that only 3 control structures are required to write any
program:
Sequence - Do this, now do this, now do this,
Selection - If something is true do this, else do that
Repetition While something is true, do this
Practice has shown that being able to create procedures
is helpful in overcoming complexity, but they arent
strictly necessary
One implication of this result is that GO TO statements
arent needed
FLOW OF CONTROL
F
?
T
T
IF
IF
The condition is tested and either the true
or false blocks are selected for execution
Dont use NEXT SENTENCE if you are
using END-IF as the delimiter (and you
should). Use of NEXT SENTENCE causes
execution to continue with the next closest
period, which is probably the end of the
paragraph.
IF Examples
IF
X < Y
ADD 1 TO X
DISPLAY AAA
ELSE
DISPLAY BBB
END-IF
IF
X > Y
DISPLAY X WAS BIGGER
END-IF
NESTED IFs
Each ELSE is matched with the nearest preceding IF
IF X < Y
DISPLAY XXX
IF Y < Z
DISPLAY ZZZ
ELSE
DISPLAY AAA
END-IF
MORAL: Indent properly and terminate all if statements
with END-IF
EVALUATE
EVALUATE
EVALUATE PLANET-NUMBER
WHEN 1 MOVE "Mercury" TO PLANET-NAME
WHEN 2 MOVE "Venus " TO PLANET-NAME
WHEN 3 MOVE "Earth " TO PLANET-NAME
WHEN 4 MOVE "Mars " TO PLANET-NAME
WHEN 5 MOVE "Jupiter" TO PLANET-NAME
WHEN 6 MOVE "Saturn " TO PLANET-NAME
WHEN 7 MOVE "Uranus " TO PLANET-NAME
WHEN 8 MOVE "Neptune" TO PLANET-NAME
WHEN 9 MOVE "Pluto " TO PLANET-NAME
WHEN OTHER MOVE " " TO PLANET-NAME
END-EVALUATE.
EVALUATE
EVALUATE PLANET-NAME
WHEN "Mercury"
WHEN "Venus "
WHEN "Earth "
WHEN "Mars
"
WHEN "Jupiter"
WHEN "Saturn "
WHEN "Uranus "
WHEN "Neptune"
WHEN "Pluto "
WHEN OTHER
END-EVALUATE.
MOVE
MOVE
MOVE
MOVE
MOVE
MOVE
MOVE
MOVE
MOVE
MOVE
1
2
3
4
5
6
7
8
9
0
TO
TO
TO
TO
TO
TO
TO
TO
TO
TO
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
EVALUATE
EVALUATE TRUE
WHEN PLANET-NAME = "Mercury" MOVE 1 TO PLANET-NUMBER
WHEN PLANET-NAME = "Venus " MOVE 2 TO PLANET-NUMBER
WHEN PLANET-NAME = "Earth " MOVE 3 TO PLANET-NUMBER
WHEN PLANET-NAME = "Mars " MOVE 4 TO PLANET-NUMBER
WHEN PLANET-NAME = "Jupiter" MOVE 5 TO PLANET-NUMBER
WHEN PLANET-NAME = "Saturn " MOVE 6 TO PLANET-NUMBER
WHEN PLANET-NAME = "Uranus " MOVE 7 TO PLANET-NUMBER
WHEN PLANET-NAME = "Neptune" MOVE 8 TO PLANET-NUMBER
WHEN PLANET-NAME = "Pluto " MOVE 9 TO PLANET-NUMBER
WHEN OTHER MOVE 0 TO PLANET-NUMBER
END-EVALUATE.
EVALUATE
EVALUATE Qty ALSO TRUE ALSO Member
WHEN 1 THRU 5 ALSO VOP < 501 ALSO "Y"
MOVE 2 TO Discount
WHEN 6 THRU 16 ALSO VOP < 501 ALSO "Y"
MOVE 3 TO Discount
WHEN 17 THRU 99 ALSO VOP < 501 ALSO "Y"
MOVE 5 TO Discount
WHEN 1 THRU 5 ALSO VOP < 2001 ALSO "Y"
MOVE 7 TO Discount
WHEN 6 THRU 16 ALSO VOP < 2001 ALSO "Y"
MOVE 12 TO Discount
WHEN 17 THRU 99 ALSO VOP < 2001 ALSO "Y"
MOVE 18 TO Discount
WHEN 1 THRU 5 ALSO VOP > 2000 ALSO "Y"
MOVE 10 TO Discount
WHEN 6 THRU 16 ALSO VOP > 2000 ALSO "Y"
MOVE 23 TO Discount
END-EVALUATE
EVALUATE
EVALUATE TRUE ALSO Position
WHEN L-Arrow ALSO 2 THRU 10
SUBTRACT 1 FROM Position
WHEN R-Arrow ALSO 1 THRU 9
ADD 1 TO Position
WHEN L-Arrow ALSO 1
MOVE 10 TO Position
WHEN R-Arrow ALSO 10
MOVE 1 TO Position
WHEN DelKey ALSO ANY
PERFORM DeleteChar
WHEN Char ALSO 1 THRU 9
PERFORM InsertChar
ADD 1 TO Position
WHEN Char ALSO 10
PERFORM InsertChar
WHEN OTHER PERFORM
DisplayErrorMessage
END-EVALUATE
PERFORM
PERFORM Paragraph
PERFORM paragraph name
Execute all instructions in the paragraph
Return control to the next instruction after the PERFORM
PERFORM 100-ROUTINE
PERFORM 200-ROUTINE
PERFORM 100-ROUTINE
100-ROUTINE.
200-ROUTINE.
300-ROUTINE.
PERFORM THRU
PERFORM x TIMES
MOVE 5 TO COUNT
PERFORM COUNT TIMES
DISPLAY XXX
END-PERFORM
PERFORM 100-DISPLAY COUNT TIMES
PERFORM UNTIL
PERFORM UNTIL
MOVE 0 TO X
PERFORM UNTIL X > 10
MOVE X TO X-EDITED
DISPLAY X-EDITED
ADD 1 TO X
END-PERFORM
PERFORM X-PARA UNTIL X > 10
PERFORM X-PARA WITH TEST AFTER
UNTIL X > 10
PERFORM VARYING
Inline Perform
PERFORM VARYING X FROM 1 BY 1
UNTIL X > 100
DISPLAY X
END-PERFORM
PRINTS:
1
2
3
100
Inline PERFORM
PERFORM VARYING X FROM 5 BY -1
UNTIL X =0
DISPLAY X
END-PERFORM
PRINTS:
5
4
3
2
1
0
Inline PERFORM
MOVE 10 TO X
PERFORM WITH TEST AFTER
UNTIL
DISPLAY X
SUBTRACT 1 FROM X
END-PERFORM
X = 0
PERFORM PARAGRAPH
PERFORM 100-RTN
WITH TEST AFTER
VARYING X FROM 1 BY 1
UNTIL X = 100
100-RTN.
.
Inline PERFORM
MOVE ZERO TO Y
PERFORM UNTIL X = 0
READ AT END MOVE 0 TO X
ADD X TO Y
DISPLAY Y
END-PERFORM
Alternate PERFORM
PERFORM 100-PARA VARYING I FROM 1 BY 1 UNTIL I > 5
AFTER J FROM 1 BY 1 UNTIL J > 3
END-PERFORM
100-PARA.
DISPLAY I J
.
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
MOVE 0 TO TOT
PERFORM VARYING SUB1 FROM 1 BY 1
UNTIL SUB1 > 12
ADD TEMP(SUB1) TO TOT
ENDPERFORM
MOVE 0 TO TOT
PERFORM VARYING K FROM 1 BY 1
UNTIL K > 12
ADD TEMP(K) TO TOT
END-PERFORM
Manipulating Indexes
Indexes cant be manipulated with
ordinary arithmetic commands. Instead
use SET.
SET
SET
SET
SET
K
K
K
K
TO 3
UP BY 1
UP BY 2
DOWN BY 3
CONTINUE
EXIT
STOP
GOBACK
GO TO
Exercise #2
Exercise #3
Repeat Exercise #2 using indexes
SEQUENTIAL SEARCH
SEARCH
Search performs a sequential search with an index
Rule of thumb: Use SEARCH for tables with 20 items or less
DEPENDING ON field must contain the number of table entries
01
01
RECCOUNT
PIC 9(2).
SALES-TAX.
05 TAB-ENTRIES OCCURS 20 TIMES
DEPENDING ON RECCOUNT
INDEXED BY K.
10 ZIPCODE
PIC 9(5).
10 RATE
PIC V999.
SET K TO 1
SEARCH TAB-ENTRIES
AT END MOVE 0 TO TAX
WHEN ZIPIN = ZIPCODE(K)
COMPUTE TAX = RATE(K) * AMOUNT
END-SEARCH
Exercise #4
Read the file from exercise #3
Store the data in a table
Read the file BCST.SICCC01.PDSLIB(DAT4EXER).
Each record has a LAST NAME, FIRST NAME field:
Last name columns 1 15
First name columns 16 30
Code a Sequential Search statement to find each name
in the file. Print each name, Found or Not Found, and
the customer balance if found.
SEARCH ALL
SEARCH ALL
01
01
Exercise #5
Read the file from exercise #3
Store the data in a table
Read the file BCST.SICCC01.PDSLIB(DAT4EXER).
Each record has a LAST NAME , FIRST NAME fields
Last name columns 1 15
First name columns 16 30
Code a binary search statement to find each name in the
file. Print each name, Found or Not Found, and the
customer balance if found.
STRING
STRING
Used to build string expressions by
concatenation (blanks not stored)
STRING FNAME DELIMITED BY
MNAME DELIMITED BY
LNAME DELIMITED BY
INTO NAME-OUT
STRING
Blanks stored
DELIMITED BY SIZE means include the entire literal
or variable contents
STRING FNAME DELIMITED BY
DELIMITED BY SIZE
MNAME DELIMITED BY
DELIMITED BY SIZE
LNAME DELIMITED BY
DELIMITED BY SIZE
INTO NAME-OUT
STRING
UNSTRING
UNSTRING
Separates a string into several component
strings
Sending field must be alphanumeric
UNSTRING NAME
DELIMITED BY ,
INTO LNAME
FNAME
MI
END-UNSTRING
UNSTRING
UNSTRING JCL-DATA
DELIMITED BY ALL SPACES OR ALL ','
INTO
WS-DATE-REQUESTED
WS-DATE1
WS-DATE2
END-UNSTRING
Exercise #6
Read the file
BCST.SICCC01.PDSLIB(STRINGS)
Print the first name, middle initial, and last
names in columns
Exercise #7
Read the file
BCST.SICCC01.PDSLIB(STRINGS1)
Print the digits followed by the letters. Use
/ as the delimiter of the two fields.
CALL
CALL PGMNAME
LINKAGE SECTION.
01 X
PIC
X(5).
01 Y
PIC
999V99.
PROCEDURE DIVISION USING X,Y.
GOBACK
.
Exercise #8
Write a main program that,
1) Prints I am in the main program,
2) Calls your subprogram,
3) Prints I am back in the main program
Write a subprogram that prints I am in the
subprogram.
Compile and link the programs, execute
the main program
Exercise #9
Write a main program that passes 3
packed decimal numbers to a subprogram
and a fourth variable in which to receive a
result.
Write a subprogram that accepts the 3
integers, computes their sum and returns
the answer to the main
Have the main print the result
Exercise #10
Write a main program that passes a
variable X by reference and Y by content
Have the subprogram add one to both
numbers
Have the main program print X and Y after
returning from the subprogram
Sign Test
Numeric data can be tested for positive, negative, and
zero values
IF AMOUNT-IN IS POSITIVE
ADD 1 TO AMOUNT-IN
END-IF
IF AMOUNT-IN IS NEGATIVE
DISPLAY AMOUNT-IN
END-IF
IF AMOUNT-IN IS ZERO
DISPLAY THE FIELD IS ZERO
END-IF
INSPECT (TALLYING)
INSPECT (TALLYING)
INSPECT MYLINE
TALLYING ECOUNT FOR ALL E
AFTER INITIAL START"
BEFORE INITIAL END
END-INSPECT
INSPECT WORK TALLYING
COUNT1 FOR LEADING *
COUNT2 FOR CHARACTERS
END-INSPECT
INSPECT WORK TALLYING
COUNT1 FOR ALL * BEFORE .
COUNT2 FOR ALL CHARACTERS AFTER .
END-INSPECT
INSPECT (REPLACING)
INSPECT (REPLACING)
INSPECT MYDATA REPLACING ALL X" BY Y
AFTER INITIAL A"
BEFORE INITIAL Z
INSPECT MYDATA REPLACING LEADING " BY +
INSPECT MYDATA REPLACING ALL A" BY +
AFTER INITIAL X"
INSPECT MYDATA REPLACING FIRST A" BY +"
AFTER INITIAL A"
BEFORE INITIAL Z
INSPECT MYDATA REPLACING ALL AAAA" BY ZZZZ"
INSPECT (CONVERTING)
INSPECT (CONVERTING)
INSPECT TEXTLINE CONVERTING
"abcdefghijklmnopqrstuvwxyz" TO
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
INSPECT FIELDA CONVERTING
1234567890 TO ABCDEFGHIJ
REFERENCE MODIFICATION
05
FIELDA(1:3) ABC
FIELDA(2:2) BC
FIELDA(4: ) DEFG
Qualification of Names
Intrinsic Functions
COBOL does not permit user-defined
functions or procedures
Intrinsic (built-in) Functions can be used in
your programs
Three broad categories of intrinsic
functions: date functions, numeric
functions and string functions.
Intrinsic Functions
Intrinsic Function values are replaced in the position where
they occur by the function result.
In COBOL, an Intrinsic Function is a temporary data item
whose value is determined at the time the function is
executed.
Functions that return a number value (numeric & integer)
are always considered to be signed.
A function that returns a number value can be used only in
an arithmetic expression or as the source of a MOVE
statement.
Intrinsic Functions
Intrinsic function pattern:
FUNCTION FunctionName(Parameters)
FunctionName is the name of the function and
Parameters is one or more parameters supplied
to the function.
COMPUTE NUM = FUNCTION RANDOM(99)
MOVE FUNCTION REVERSE(ABCD) TO NAME
Intrinsic Functions
REVERSE(Alph) Alphanumeric Returns a
character string with the characters in Alph
reversed.
LOWER-CASE(Alph) Alphanumeric Returns a
character string with the characters in Alph
changed to their lower case equivalents.
UPPER-CASE(Alph) Alphanumeric Returns a
character string with the characters in Alph
changed to their upper case equivalents
Condition Names
01 NO-OF-NEIGHBORS
PIC 9.
88 JUST-RIGHT VALUE 2 THRU 3.
88 TOO-FEW
VALUE 0 THRU 1.
88 TOO-MANY
VALUE 4 THRU 8.
01 MARITAL-STATUS PIC X.
88 VALID-STATUS
VALUE S M D W.
Exercise #11
31907
90003
30002
Capitalize the state name found in the table. Print message next to each
state name separated by a dash.
Use the SEARCH command perform a sequential search of the table for each look
up. After the program is working, modify it to perform a binary search with SEARCH
ALL
PIC X.
PIC 9(7).
PIC X(8).
PIC XX.
PIC
PIC
PIC
PIC
PIC
9(4).
99.
99.
X(4).
X.
PIC X.
PIC 9(7).
PIC 9(7).
PIC X(4).
CUST
RECORD IS VARYING IN SIZE
FROM 28 TO 408 CHARACTERS
DEPENDING ON RECORD-LEN.
01 CUST-REC.
05 ROOT-SEG.
10 CUST-NO
PIC X(6).
10 INVOICE-COUNT
PIC S99.
05 INVOICE-SEGMENT OCCURS 20 TIMES INDEXED BY NDX
10 INVOICE-DATE PIC X(8).
10 INVOICE-NO
PIC X(5).
10 INVOICE-AMT
pIC S9(5)V99.
You must set the RECORD-LEN before writing the record!
Alternative
FD CUSTFILE
RECORD CONTAINS 20 TO 80 CHARACTERS
01 REC.
05 FIXED-PART PIC X(20).
10
05 VARY-PART OCCURS 1 TO 6 TIMES
DEPENDING ON COUNT
INDEXED BY NDX
COUNT has to be initialized at the time of writing the record
CUST
RECORD IS VARYING IN SIZE
FROM 28 TO 408 CHARACTERS.
01 CUST-REC.
05 ROOT-SEG.
10 CUST-NO
PIC X(6).
10 INVOICE-COUNT PIC S99.
05 INVOICE-SEGMENT OCCURS 20 TIMES INDEXED BY NDX
10 INVOICE-DATE PIC X(8).
10 INVOICE-NO
PIC X(5).
10 INVOICE-AMT PIC S9(5)V99.
PERFORM
EXERCISE #12
Each A record for a given customer is
followed by one to five B records for that
customer.
For each A record, write out one variable
length record that contains the A record as
the fixed part and the associated B
records as the variable parts
EXERCISE #13
BCST.SICCC01.PDSLIB(COBDATA5) CONTAINS TWO
TYPES OF 80 BYTE RECORDS :
RECORD TYPE A
1 BYTE TYPE CODE PIC X CONTAINING A
5 BYTE CUSTOMER ID PIC X(5)
RECORD TYPE B
1 BYTE TYPE CODE PIC X CONTAINING B
5 BYTE PART NUMBER PIC X(5)
6 BYTE COST PIC 9(4)V99
EXERCISE #14
Read the variable length records you created in exercise #5.
Produce a report similar to the one below:
CUSTOMER ID PART #
10030
22322
23444
50043
TOTAL
20030
22322
23444
50043
TOTAL
COST
1,333.34
3.44
98.77
1435.55
1,333.34
3.44
98.77
1435.55
VSAM
VSAM data sets are known as Clusters
For ESDS or RRDS the cluster consists of
a data component
For KSDS the cluster consists of a data
component and an index component
VSAM data is stored on DASD in control
intervals which are grouped into control
areas
VSAM
The Control Interval (CI) is the unit of data
that transfers between the disk and virtual
storage
CI sizes are multiples of 2K with 4k being
common
CIs can be constructed with free space to
accommodate additions to the file
Control Areas (CA) can be constructed
with free space to accommodate additions
VSAM
VSAM dynamically manages the file by
maintaining information in each CI and CA
When a CI becomes too full the data it
contains is split into two CIs
When a CA becomes too full the data it
contains is split into two CAs
VSAM tries to keep records that are
logically close together, physically close as
well
VSAM Indexes
VSAM Components
DEFINE CLUSTER
PRINT
REPRO
LISTCAT
DELETE
DEFINE ALTERNATEINDEX
DEFINE PATH
BLDINDEX
VSAM JCL
Unlike QSAM files, VSAM files are usually
allocated in a separate job step before
data can be written to the file
A VSAM cluster is usually created by
deleting and then defining the cluster
After the cluster is defined, a job can run
which writes data to the file
VSAM JCL
Parameters:
INDEXED KSDS
NONINDEXED ESDS
NUMBERED RRDS
KEYS ( len off) primary key info
CISZ (size) control interval size
FREESPACE (ci ca) free space %s
MAKEKSDS
000100
000200
000300
000400
000500
000600
000700
000800
000900
001000
001100
001200
001210
001220
001230
001240
001250
001260
001270
001280
IDCAMS PRINT
000100 //TSYSAD2P JOB
'A.STUDENT',USER=TSYSAD2,REGION=2048K,MSGCLASS=V
000200 //*MAIN CLASS=TSYSC,USER=TSYSAD2
000210 //* THIS IS AN IDCAMS PRINT
000220 //PRINT
EXEC PGM=IDCAMS
000230 //SYSPRINT DD SYSOUT=*
000240 //SYSIN
DD *
000250
PRINT INFILE(IFILE) 000251
DUMP
000252 /*
000253 //IFILE
DD DSN=TSYSAD2.PAYROLL.MASTER,DISP=SHR
000254 //
IDCAMS REPRO
000100
000200
000210
000220
000230
000240
000250
000251
000252
000253
000254
000255
000256
000257
000258
000100
000200
000300
000400
000500
000600
000610
000620
000630
000640
000700
IDENTIFICATION DIVISION.
PROGRAM-ID. VSAM1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PAYROLL-MASTER-OUT ASSIGN TO PAYMASTO
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS ID-OUT
FILE STATUS IS PM-STATUS.
SELECT PAYROLL-MASTER-IN ASSIGN TO PAYMASTI.
004410 01 PM-STATUS.
004430
05 PM-STAT1
PIC X.
004440
05 PM-STAT2
PIC X.
004441 PROCEDURE DIVISION.
004450
OPEN INPUT PAYROLL-MASTER-IN
004460
OPEN OUTPUT PAYROLL-MASTER-OUT
004461
IF PM-STATUS NOT = '00'
004462
PERFORM 300-PRINT-STATUS
004463
END-IF
004470
PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO '
004480
READ PAYROLL-MASTER-IN
004490
AT END
004500
MOVE 'NO ' TO ARE-THERE-MORE-RECORDS
004600
NOT AT END
004700
PERFORM 200-READ-MODULE
004800
END-READ
004900
END-PERFORM
005000
CLOSE PAYROLL-MASTER-IN
005100
PAYROLL-MASTER-OUT
005110
GOBACK
005130
005410
005420
005430
005440
005500
005510
005520
005530
005600
005700
005800
005900
006000
200-READ-MODULE.
MOVE ID-IN TO ID-OUT
MOVE NAME-IN TO NAME-OUT
MOVE HOURS-IN TO HOURS-OUT
MOVE RATE-IN TO RATE-OUT
WRITE MASTER-REC-OUT
IF PM-STATUS NOT = '00'
PERFORM 300-PRINT-STATUS
END-IF
.
300-PRINT-STATUS.
DISPLAY 'FILE STATUS CODE:' PM-STATUS
GOBACK
.
OPEN
OPEN
OPEN
OPEN
OPEN
INPUT file-name
OUTPUT file-name
I-O file-name
EXTEND file-name
For EXTEND, access mode must be
sequential
Writing
WRITE record-name [FROM dataname]
[INVALID KEY imperative stmt]
[NOT INVALID KEY imperative
stmt]
[END-WRITE]
Exercise #15
Read BCST.SICCC01.PDSLIB(COBDATA6)
COL 1-5 KEY
COL 6-25 NAME
Allocate a KSDS with record size 25 and a key
field in cols 1-5
Write out a KSDS record for each record in the
file. Write out the records sequentially.
Exercise #16
Read
BCST.SICCC01.PDSLIB(COBDATA7)
COL 1-5 KEY
Read a KSDS record (randomly) for each
record in the file. Write out the names you
find sequentially. If the record doesnt
exist, print a message Not Found
Nested Programs
1)
2)
3)
4)
Nested Programs
Calling a nested program is as efficient as
performing a paragraph
Nested programs provide design flexibility and
encourage good program design
A nested program would be called a function or
subroutine in any other language
Nested programs unleash the power of COBOL
pointers and allow COBOL programmers to
design data structures that encourage efficient
programming techniques
Nested Programs
PROGRAM-ID. MAIN.
PROGRAM-ID. SUB1.
END PROGRAM
PROGRAM-ID.
END PROGRAM
END PROGRAM
SUB1.
SUB2
SUB2.
MAIN.
COBOL Pointers
05 PTR1
USAGE IS POINTER.
05 PTR2-P USAGE IS POINTER VALUE NULL.
SET PTR1 TO ADDRESS OF LINKAGE-THING
SET PTR2 TO PTR1
LINKAGE SECTION.
01 NAME-STRUCTURE.
05 FIRST-NAME PIC X(18).
05 LAST-NAME
PIC X(26).
SET ADDRESS OF NAME-STRUCTURE TO EXAMPLE-P.
COBOL Pointers
IF PTR1 NOT = NULL AND
PTR1 NOT = PTR2
PERFORM 2730-SOMETHING
END-IF
SYNCHRONIZED
The SYNCHRONIZED clause is sometimes used
with USAGE IS COMP or USAGE IS INDEX
items. It is used to optimize speed of processing
but it does so at the expense of increased
storage requirements.
The word SYNC can be used instead of
SYNCHRONIZED
SYNCHRONIZED causes slack bytes to be
generated when needed.