Enterprise COBOL Education Using Rational Developer for System Z COBOL General Language Rules

capybarabowwowΛογισμικό & κατασκευή λογ/κού

30 Οκτ 2013 (πριν από 4 χρόνια και 9 μέρες)

248 εμφανίσεις

®


IBM Software Group

© 2006 IBM Corporation

Enterprise COBOL Education Using Rational Developer for System Z


COBOL General Language Rules

Jon Sayles, IBM Software Group, Rational EcoSystems Team

2

IBM Trademarks and Copyrights


© Copyright IBM Corporation 2007,2008, 2009. All rights reserved.



The information contained in these materials is provided for informational purposes
only, and is provided AS IS without warranty of any kind, express or implied. IBM
shall not be responsible for any damages arising out of the use of, or otherwise
related to, these materials. Nothing contained in these materials is intended to, nor
shall have the effect of, creating any warranties or representations from IBM or its
suppliers or licensors, or altering the terms and conditions of the applicable license
agreement governing the use of IBM software. References in these materials to IBM
products, programs, or services do not imply that they will be available in all countries
in which IBM operates.



This information is based on current IBM product plans and strategy, which are
subject to change by IBM without notice. Product release dates and/or capabilities
referenced in these materials may change at any time at IBM’s sole discretion based
on market opportunities or other factors, and are not intended to be a commitment to
future product or feature availability in any way.



IBM, the IBM logo, the on
-
demand business logo, Rational, the Rational logo, and
other IBM Rational products and services are trademarks or registered trademarks of
the International Business Machines Corporation, in the United States, other
countries or both. Other company, product, or service names may be trademarks or
service marks of others.

3

Course Contributing Authors


Thanks to the following individuals, for assisting with this course:


David Myers/IBM,


Ka Yin Lam/IBM


John Fenyar


Wilbert Kho/IBM


Steven Wilcenski/Sabre Systems Inc.


Mike Wrzinski/Sentry Insurance



4

Course Description


Course Name:
COBOL Foundation Training
-

with RDz



Course Description:
Learn the COBOL language, RDz and learn z/OS terms, concepts and development skills in this course.



Pre
-
requisites:

Some experience in a 3rd or 4th Generation Language is expected. SQL is also recommended.



Course Length:
10 days



Topics (Agenda)


Getting Started
-

installing and configuring RDz
-

and the course materials, and using Eclipse to edit COBOL


COBOL General Language Rules


Basic COBOL Statements


Advanced record and table handling


Debugging Programs
-

Note: Deep dive on using RDz for common COBOL programming errors (001, 0C4, 0C7, infinite loops, fall
-
thru, etc.)


Input/Output and Report Writing Patterns


Sequential File Match/Merge Patterns


COBOL Subprograms and the Linkage Section


Structured Programming Concepts and Coding Patterns


Advanced Character Manipulation, COBOL Intrinsic Functions, Date and Time coding patterns, and Language Environment calls


OS/390 Concepts and JCL


Compile/Link & Run Procs on the mainframe


Indexed file Coding Patterns


Sort/Merge and Master File Update Coding Patterns


Accessing DB2 Data and Stored Procedures


COBOL in the Real World:


CICS
-

lecture only


IMS (DL/I and TM)
-

ditto


Batch processing
-

ditto


Java calling COBOL


COBOL and XML Statements


SOA and COBOL
-

creating and calling Web Services


Web 2.0 using Rich UI


5

Unit



Coding COBOL




Identifying Your Program




Describing Your Computing Environment




Describing the Data




Processing the Data




Java and .NET Equivalents

Topics:

COBOL General Language
Rules

6

Topic objectives

After completing this topic, you should be able to:


Describe the areas of COBOL structured coding


and what kind of COBOL
statements go into each


Define the four DIVISIONs of a COBOL program


List the kinds of alphanumeric characters that can be used in coding COBOL
programs


7

COBOL Character Set


COBOL code is written using:


Alphanumeric characters:


A


Z


0


9


The following special characters:

+
-

* / = $ : . , ; “ ‘ ( ) < >




Note


no underscore



Upper and lower case characters are equivalent (case insensitive), except
when they appear in a character string



On the mainframe, the character set


and collating sequence is
EBCDIC



On the PC and on AIX systems, the character set and collating sequence is
ASCII


The differences between EBCDIC manifest themselves at run
-
time in:


Alphanumeric sort routines and operations


IBM built
-
in functions that process sorted data (Median, etc.)

8

Statement Format (Basic COBOL Coding Rules)


COBOL program development uses a fixed
-
column or fixed
-
position coding style:


Statements must be coded within specified column
-
numbered area boundaries





Columns 1


6 contain an optional (rarely used) sequence number for ordering
statements


Column 7 indicates either:


COBOL comment line



by typing an
asterisk
*

in column 7


Multi
-
line continuation

of long literal values
-

by typing a
dash


in column 7


Columns 8


11 are called the “A” or area


with column 8 termed the “A” margin.
Columns 8


11 are reserved for:


Division statements


Procedure names


Certain types of variable declarations



Columns 12


72 are called the “B” area


you code all of your procedural
(business) logic here, as well as the rest of the variable declarations



C
olumns 73


80 are used by certain Source Code Management Systems for
identifying line changes in versions.

Sequence

Numbers

A Area

B Area
-------------------------------------------------------------------------------------------------------------------------
---

Version Control

Numbers

See Slide Notes

9

COBOL Program Units
(The Four Divisions)


1.
IDENTIFICATION
DIVISION
,

which identifies the
program with a name and, if optionally
gives other identifying information.

2.
ENVIRONMENT DIVISION
,
where you describe the aspects of your
program that depend on the computing
environment.

3.
DATA DIVISION
,

where the
characteristics of your data are defined

4.
PROCEDURE DIVISION
, where
the instructions related to your business
logic are specified.



The
PROCEDURE DIVISION

of a program is
sub
-
divided into
sections

or
paragraphs
, which
contain executable COBOL sentences and
statements





The Divisions must be coded in the
order described



There are many optional
statements and keywords in the
Divisions



Division and procedure (Paragraph
and Section) names: Begin in the A
margin, must end with a period,
and must be the only statement on
that line



Your program ends when either a
GOBACK

or
STOP RUN

statement is executed

Identification

Division

Environment

Division

Data

Division

Procedure

Division

See

Notes

10

Unit



Coding COBOL




Identifying Your Program




Describing Your Computing Environment




Describing the Data




Processing the Data




Java and .NET Equivalents

Topics:

COBOL General Language
Rules

11

Topic objectives

After completing this topic, you should be able to:


Describe the purpose of the IDENTIFICATION DIVISION


List the required elements of it


Code a syntactically correct IDENTIFICATION DIVISION


List a few optional paragraphs and sections of the IDENTIFICATION DIVISION

12

Identifying Your Program


Standard Grammar

IDENTIFICATION DIVISION.

Program
-
ID. PROGRAM1.

Author. Bill Hudson.

Installation. Computing Labs Inc.

Date
-
Written. 12/21/2009.

Date
-
Compiled. 12/30/2009.



Use the
IDENTIFICATION DIVISION

to name a program and optionally
provide other identifying information.


Required statements:


IDENTIFICATION DIVISION.


PROGRAM
-
ID. PROGNAME.



You can use the optional
AUTHOR, INSTALLATION, DATE
-
WRITTEN
,
and
DATE
-
COMPILED

paragraphs for descriptive information about a
program.



The data you enter in the DATE
-
COMPILED paragraph is replaced with the
latest compilation date.

13

IDENTIFICATION DIVISION


PROGRAM
-
ID


COBOL
Program
-
ID

names must follow these rules:


For mainframe (z/OS) COBOL programs:


Traditionally eight characters


That restriction has been lifted in current language levels


Name must start with an alphabetic letter


Name can contain only alpha
-
numerics (letters and 0


9)



For PC (“standard”) COBOL programs:


Maximum of thirty characters


Name can contain:


Alpha
-
numerics


The dash


Cannot start or end the Program
-
ID name with a dash



Note:
It is good programming practice to add in
-
line comments to the top of your
COBOL program. Comments that document:


The business/technical purpose of the program


A program maintenance log


Any unusual or complex coding patterns

z/Series COBOL

PC

COBO
L

14

IDENTIFICATION DIVISION


Optional Statements and Constructs

You can extend the
PROGRAM
-
ID

paragraph, with the following optional
keywords that compile programs to allow for extended functionality:



RECURSIVE


allows a program to be reentrant and called recursively


PROGRAM
-
ID MYPROG IS RECURSIVE.



INITIAL


tells the compiler to reset variables to specified initial values upon
program execution


PROGRAM
-
ID MYPROG IS INITIAL.



CLASS
-
ID.


Enterprise COBOL provides facilities for developing object
-
oriented programs
using the COBOL programming language.


OO COBOL programs contain a CLASS
-
ID paragraph, for defining an OO class,
instead of PROGRAM
-
ID, which defines a structured program


OO COBOL will be touched on later in this course, as it is considered an
“emerging z/Series technology” in most I/T installations


See slide notes for additional information on OO COBOL

15

Unit



Coding COBOL




Identifying Your Program




Describing Your Computing Environment




Describing the Data




Processing the Data




Java and .NET Equivalents

Topics:

COBOL General Language
Rules

16

Topic objectives

After completing this topic, you should be able to:


Describe the purpose of the ENVIRONMENT DIVISION


List the required elements of it


Code a syntactically correct ENVIRONMENT DIVISION


List a few optional paragraphs and sections of the ENVIRONMENT DIVISION

17

Environment Division


Standard Grammar

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.



SOURCE
-
COMPUTER. IBM
-
3081.



OBJECT
-
COMPUTER. IBM
-
3081.

INPUT
-
OUTPUT SECTION.



SELECT PRINTFILE ASSIGN TO UPDPRINT



ORGANIZATION IS SEQUENTIAL.




Use the
ENVIRONMENT DIVISION

to identify input and output files for the program,
and to describe aspects of the program that depend on the computing platform.


ENVIRONMENT DIVISION

and its sections can be optional.


That is, you will code them when you need to specify elements of your program that
connect to the operational environment your program runs in (example. Reading and
writing to external files)



The
ENVIRONMENT DIVISION

has two sections:


CONFIGURATION SECTION



Optional section, used to specify the development and run:


Source
-
Computer


the compiling computer.


Object
-
Computer



the run
-
time computer


INPUT
-
OUTPUT SECTION



Used to define external files used by the program
(files are
considered part of your program’s “environment”)


The SELECT/ASSIGN statement is covered on the next slide




Note that:
ENVIRONMENT DIVISION, CONFIGURATION SECTION

and

INPUT
-
OUTPUT SECTION

are coded in the “
A
” area of your source code

18

Select/Assign


Input/Output Files and Datasets/Devices


The
FILE
-
CONTROL

part of the
INPUT
-
OUTPUT SECTION

identifies the external
files your program reads and writes using COBOL “logical” name identifiers



These logical file names in your program, are mapped to a physical input/output
storage device attached to a mainframe (typically disk storage)



By using Select/Assign in your program, you achieve Physical/Logical data
independence
-

so you won’t have to change your code:


If you move the physical file to a different device


If you point to an altogether different file entirely


All your program cares about is the internal (logical) file name in Select/Assign


The system is responsible to find and attach the correct external dataset


Your program source…

ENVIRONMENT DIVISION.

INPUT
-
OUTPUT SECTION.

FILE
-
CONTROL.


SELECT INPUT ASSIGN TO INPUT1

(your program’s internal COBOL filename)





The mainframe “job” that runs your program



//GO.INPUT1 DD DSN=MY.INPUT,DISP=SHR


MY.INPUT

Mainframe

Your

Program

External z/OS Dataset (file)

3990

Disk Pack

19

ENVIRONMENT DIVISION


Assigning Input/Output Files to Your PC


For the programs in this course, you will use the
ENVIRONMENT DIVISION
’s

SELECT/ASSIGN

clauses to point to ASCII files on your machine.


So your “environment” is the PC, not the mainframe



You will code your
Select/Assign

clauses for PC files like the following:



ENVIRONMENT DIVISION.


INPUT
-
OUTPUT SECTION.


FILE
-
CONTROL.



SELECT CUST
-
FILE



ASSIGN TO "D:
\
CUST.DAT"



ORGANIZATION IS LINE SEQUENTIAL



ACCESS MODE IS SEQUENTIAL



FILE STATUS IS CFCODE.




SELECT PRINT
-
FILE



ASSIGN TO "D:
\
CUSTOUT.DAT"



ORGANIZATION IS LINE SEQUENTIAL



ACCESS MODE IS SEQUENTIAL




FILE STATUS IS PRTCODE.




See Slide Notes, for additional File Read/Write performance tips


Internal file name


PC file spec:
Drive:
\
folder
\
filename.extension


Tells the compiler how to treat the file's I/O





Names a variable you can check for I/O errors



Internal file name


PC file spec:
Drive:
\
folder
\
filename.extension


Tells the compiler how to treat the file's I/O





Names a variable you can check for I/O errors


20

Unit



Coding COBOL




Identifying Your Program




Describing Your Computing Environment




Describing Your Program’s Data




Processing the Data




Java and .NET Equivalents

Topics:

COBOL General Language
Rules

21

Topic objectives

After completing this topic, you should be able to:


Describe the purpose of the DATA DIVISION


List the required elements of it


Code a syntactically correct DATA DIVISION


List a few optional paragraphs and sections of the DATA DIVISION

22

Identifying and Describing Your Program’s Data


Standard Grammar

DATA DIVISION.

FILE SECTION.



WORKING
-
STORAGE SECTION.



LINKAGE SECTION.


One of COBOL’s fortes is file handling and data manipulation


which COBOL makes:


Simple



easy to code


Efficient



as pertains to run
-
time processing efficiency)


Sophisticated



allowing for complex data types and structures



Define the characteristics of your data, and group your data definitions into one of the
sections in the
DATA DIVISION
.



You can use these sections of the
DATA DIVISION

to define the following types of
data:


Data used in input
-
output operations (
FILE SECTION
)


Data defined for internal processing purposes, calculation fields, flags, counters, etc.


(
WORKING
-
STORAGE SECTION
)


Data passed as parameters from another program (
LINKAGE SECTION
)

23

FILE SECTION


Data used in File Input/Output Operations

DATA DIVISION.

FILE SECTION.

FD internalFileName



RECORD CONTAINS n CHARACTERS



DATA RECORD IS recordVariable
.

01 recordVariable.


05 Field1 PIC X(40).


05 Field2 PIC X(40).




Every Select/Assign file in the

INPUT
-
OUTPUT SECTION

of the

ENVIRONMENT
DIVISION

must have a matching

FD

(
F
ile
D
efinition
) e
ntry in the

FILE SECTION


You follow an

FD

by the declaration of the data record the file is read into/written from


The
RECORD CONTAINS

clause is optional


but if explicitly coded, it causes the compiler
to verify the
DATA RECORD
’s overall length with the “
n CHARACTERS
” specified


The
DATA RECORD

clause is also optional, and specifies the COBOL variable that will be
read into,
or

written

from.
.



FILE SECTION
,
FD

and the

01
level
-
number for the record variable are coded in
the “A” area


All other clauses and level
-
numbers > 01 must begin in the “B” area




Note that we'll cover COBOL level
-
numbers in a few slides

24

FILE SECTION


Optional Clauses

DATA DIVISION.

FILE SECTION.

FD internalFileName







BLOCK CONTAINS n RECORDS



LABEL RECORDS ARE STANDARD.



BLOCK CONTAINS



defines a “blocking factor” for the file.


Blocking factors are I/O efficiency features of COBOL and z/OS


We will cover them later in the course


For mainframe COBOL programs:


Recommendation
-

code:
BLOCK CONTAINS 0 RECORDS


This allows the compiler to obtain the Blocking Factor directly from the program’s JCL


For PC COBOL programs:


Recommendation


leave this clause out



LABEL RECORDS ARE STANDARD



determines whether there is a label on the
z/OS dataset (external file).



-
LABEL RECORDS ARE STANDARD

is the default if you omit the clause



-
Leave this clause off for PC files, print files and non
-
labeled datasets

See Notes

25

WORKING
-
STORAGE SECTION


Declaring Program
Work

Variables

DATA DIVISION.

FILE SECTION.



WORKING
-
STORAGE SECTION.

77

RECORD
-
KTR


PIC 9(4).

77 END
-
OF
-
FILE
-
SW


PIC X(1) VALUE 'N'.

01 StructureVariable.


05 Elementary
-
num
-
field

PIC 9(4).


05 Elementary
-
char
-
field

PIC X(40).




The
WORKING
-
STORAGE SECTION

is where you code variables used
throughout your program



All of the variables in the
WORKING
-
STORAGE SECTION

are global in
scope
(can be referenced throughout your program)



Use
WORKING
-
STORAGE
variables for:


Temporary fields used in calculations


Variable names for constant values


Record structures used in data manipulation processing


Switches and flags (i.e. end
-
of
-
file)


Print file records and structures

26

WORKING
-
STORAGE SECTION


Coding COBOL Variables

WORKING
-
STORAGE SECTION.

77

RECORD
-
KTR


PIC 9(4) COMP VALUE ZERO.

77 KTR
-
2



PIC 9999 COMP VALUE ZERO.

01 STRUCTURE
-
VARIABLE.


05 ELEM
-
FIELD


PIC 9(7)V99 COMP
-
3 VALUE ZERO.


05 END
-
OF



PIC X(40).




COBOL variable names:


Can use alphanumerics and the dash (but don’t start or end a variable name with a dash)


Must be shorter than 30 characters, no embedded blanks

,
and contain at least one letter


Code individual (
single
) COBOL variables as follows:


Type a
77

in the “A” area


Type the variable name in the “B” area


Follow the variable name with a “picture clause”


typically abbreviated as:
PIC



that describes
the type, size and scale of your variable


End with the variable declaration with a period


Code
(related
) variables grouped into a structure as follows:


Type a
01

in the “A” area


Type the structure name in the “B” area


Type the remaining variables starting with a number > 01 in the “B” area and follow the above rules
for coding
77

variables



See Slide Notes



for an explanation of
COMP

and
COMP
-
3

27

WORKING
-
STORAGE SECTION


PICTURE Clause Definitions


Every COBOL data variable must have a picture clause


typically abbreviated as:
PIC



A variable’s picture clause determines:


The type and format of the data stored in the field


Size of the data


Numeric precision
(scale, or # of digits to the right and left of the decimal place)


You can code a number inside a parenthesis, which means that the preceding character is repeated “n”
times (see below)




Different
PIC

clauses are used for input and intermediate calculation results or holding areas
and for output.




Note: We’ll cover input and output picture clauses in
-
depth, in a subsequent unit of this
course.


The Input or
WORKING
-
STORAGE PIC

clauses consist of:


X

Any character (Alpha, numeric, or special)


9


Only numeric data
-

digits (0





V

Part of numeric definition. Assumed decimal point


S

Part of numeric definition. Sign


allows for negative numbers

01 WS
-
EMP
-
RECORD.


05 FILLER PIC XXX.


05 ER
-
LAST
-
NAME PIC X(20).


05 ER
-
FIRST
-
NAME PIC X(20).


05 ER
-
SOC
-
SEC
-
NUM PIC 9(9).


05 ER
-
PAY
-
RATE PIC S999V99.


05 ER
-
HOURS
-
WORKED PIC 999.


05 FILLER PIC X(28).

What's with FILLER?


See Slide Notes

28

Level

Numbers

COBOL Records


Level Numbers and Hierarchically Structured Group Variables

01 ADDRESS
-
REC.



05 STREET


PIC X(40).



05 ADDRESS
-
2.



10 CITY


PIC X(17).



10 STATE
-
CODE

PIC XX.



10 POSTAL
-
CODE1

PIC 9(5).



10 FILLER

PIC X VALUE "
-
".



10 POSTAL
-
CODE2

PIC 9(4).




You may want to sub
-
divide data into groups of fields that show relationships, or that
organize variables in a hierarchical structure known as a COBOL “record”.


Records are COBOL variables that begin with a “level
-
number” in the range of
01

(which denotes the record as a whole)



49
.


COBOL records consist of:


Group fields:


Which do
NOT

have a picture clause


Typically contain elementary fields (lower level fields that sub
-
divide a group variable)


And have a lower level number (representing higher “parentage” level” in the structure) than their
children (elementary) fields


Elementary fields:


Which must have a picture clause


And have a higher level number (representing lower or dependent level in the structure) than their
group field “parent” variables


You can manipulate both group and elementary fields in COBOL


COBOL Group Structure variable


Elementary field


Another group variable


Elementary character field





Elementary numeric field


Value

clause


initializes variable

29

LINKAGE SECTION


Declaring Variables Passed as Parameters by Other Programs

DATA DIVISION.

FILE SECTION.

WORKING
-
STORAGE SECTION.

LINKAGE SECTION.

01 StructureVariableName.


05 BinaryIntVariable

PIC 9(4) COMP.


05 FixedStringVariable

PIC X(40).




The
LINKAGE SECTION

is where you define variables that are used to
receive parameters passed by other programs in your application


All of the variables in the
LINKAGE SECTION


are global in scope


LINKAGE SECTION

variables adhere to the same COBOL variable coding rules
as data in the
FILE SECTION

and
WORKING
-
STORAGE SECTION
:


COBOL variable naming rules


Picture clause definitions


Group/Structure record declaration rules



LINKAGE SECTION

variables obtain their values through data passed as
parameters in “calling” COBOL programs


This will be described in greater detail later in the course

30

Wow… This Seems Like a Lot to Remember!

Have no fear… the compiler and Workbench
tools will remind you!

Memorizing all these
rules seems like a
steep ladder to
climb…

31

Unit



Coding COBOL




Identifying Your Program




Describing Your Computing Environment




Describing the Data




Processing the Data




Java and .NET Equivalents

Topics:

COBOL General Language
Rules

32

Topic objectives

After completing this topic, you should be able to:


Describe the purpose of the PROCEDURE DIVISION


List the required elements of it


Code a syntactically correct PROCEDURE DIVISION statement


List a few optional paragraphs and sections of the PROCEDURE DIVISION

33

Processing Your Data


the PROCEDURE DIVISION

IDENTIFICATION DIVISION.



ENVIRONMENT DIVISION.



DATA DIVISION.



PROCEDURE DIVISION.



In the
PROCEDURE DIVISION

of a program, you code the executable
statements (business logic) that process data.



The division header for a program can be:


PROCEDURE DIVISION.


PROCEDURE DIVISION USING VAR1, VAR2…


PROCEDURE DIVISION RETURNING VAR1, VAR2…




--

Note that
VAR1, VAR2
, etc. are variables defined in a program’s
LINKAGE SECTION

Accepting parameter values into VAR1, VAR2

Returning parameter values to a program that called this program

34

How Logic is Divided in the PROCEDURE DIVISION

Section


Logical subdivision of your processing logic.


A section has a procedure name


declared in the “A” area,
and is optionally followed by one or more paragraphs.


A section can be the subject of a PERFORM statement.


A section can have zero
-
to
-
many paragraphs


Paragraph


Subdivision of a section, procedure, or program.


A paragraph has a procedure name


declared in the “A”
area, followed by a period and zero or more sentences.


A paragraph can be the subject of a PERFORM or THRU
statement.


Sentence


Series of one or more COBOL statements


coded in the
“B” area, that ends with a period.


Statement


Performs a defined step of COBOL processing, such as
adding two numbers.


A statement is a valid combination of words, and begins with
a COBOL verb.


Statements are



Imperative (indicating unconditional action)


Conditional


Compiler
-
directing


Statements are terminated by a period


Some statements can be terminated by an explicit Scope
Terminator (see screen capture)


Using explicit scope terminators instead of periods to
show the logical end of a statement is a “Best Practice”

The
PROCEDURE DIVISION

of a COBOL
program is divided into sections and
paragraphs, which contain sentences,
statements, and phrases.


Section and Paragraph procedure names
follow the same COBOL naming rules as
variables

S

E

C

T

I

O

N

P

A

R

A

R

A

P

H

Imperative

Statements

Scope Terminator

Conditional

Statement

S

E

N

T

N

C

E

35

PROCEDURE DIVISION


Imperative Statements


An imperative statement
-

such as


ADD


MOVE


INSPECT


COMPUTE


DISPLAY


ACCEPT


GOBACK


READ


WRITE


CLOSE



…indicates an
unconditional action

to be taken.




End an imperative statement with a
period


Or if allowed (READ, WRITE, COMPUTE),
end with a scope
-
terminator


Examples of Imperative Statements


More examples can be found in:


http://publibfp.boulder.ibm.com/epubs/pdf/igy3lr40.pdf

36

PROCEDURE DIVISION


Conditional Statements


A conditional statement is either a
simple conditional statement


IF


EVALUATE


SEARCH


or a conditional statement made up of
an imperative statement that includes a
conditional phrase or option


PERFORM … UNTIL


READ … AT END



You can end a conditional statement
with an implicit or explicit scope
terminator.


37

PROCEDURE DIVISION


Imperative Statements


ACCEPT



1 of 2

WORKING
-
STORAGE SECTION.

77 INPUT
-
DATA

PIC X(40).



PROCEDURE DIVISION.





ACCEPT INPUT
-
DATA.



Accept waits for user data entry from your keyboard (and waits for you to
press


Enter
.



The alpha
-
numeric value you enter is assigned to the variable following
ACCEPT.




It is not necessary to make the accepted input variable of type
PIC X



but
is probably a best practice, to avoid user
-
entry and data
-
type errors



On the Mainframe,
ACCEPT

retrieves its value from a reserved JCL “DD
card” named:
SYSIN
.


We will discuss this in more depth later in the course

38

PROCEDURE DIVISION


Imperative Statements


ACCEPT



2 of 2













ACCEPT

is often used to retrieve certain useful operating system
"special
-
register" values:



Current Date


formatted with a number of different date masks:


Date


DAY
-
OF
-
WEEK

(a value 1


7, representing Monday


Sunday)


Day



the ordinal day of the year (1


365)



Time



formatted from the operating system's time: hhmmssss



Date/Time Structure Variables



ACCEPT

statements, that return the current date


and time in different formats

39

PROCEDURE DIVISION


Imperative Statements


DISPLAY

WORKING
-
STORAGE SECTION.



PROCEDURE DIVISION.







DISPLAY "Something on the console".




Display presents a literal or the contents of a
PIC X(nn)

field in the
console:


On the mainframe
DISPLAY

writes to a reserved JCL DD statement named:
//SYSOUT


On your PC a DOS window is opened, and the data is displayed inside it


Also


a new line is opened and your cursor moves to the new line (which works, in
case you want to
ACCEPT

data into the program)


You can
DISPLAY
:


A literal


A PIC X field


A PIC 9 field


A record


Multiple fields and/or literals, separated by commas



Code
DISPLAY

in the "B" area of your program

Something
on the
console.

See Slide Notes

40

PROCEDURE DIVISION


Imperative Statements


MOVE

FILE SECTION



01 OUT
-
REC PIC X(80).



WORKING
-
STORAGE SECTION.

77 INPUT
-
DATA

PIC X(40).



PROCEDURE DIVISION.







ACCEPT INPUT
-
DATA.



MOVE INPUT
-
DATA TO OUT
-
REC.


Syntax:

MOVE <to
-
variable> TO <from
-
variable>.



MOVE
copies the value in the
to
-
variable

to the
from
-
variable

(receiving
field),

over
-
writing the
from
-
variable
’s

storage contents.


Alpha
-
numeric values are copied from left
-
to
-
right, byte
-
for
-
byte


If the receiving field is longer than the sending field blanks (spaces) pad the receiving field
to the end of its PIC X length


If the receiving field is shorter than the sending field truncation occurs



We will study
MOVE

variations, and COBOL internal storage contents in subsequent
chapters

41

PROCEDURE DIVISION


Sequential File Input/Output Routines


FILE SECTION


FD


EXTFILE …


01 OUT
-
REC PIC X(80).





WORKING
-
STORAGE SECTION.





PROCEDURE DIVISION.







OPEN <input/output> EXTFILE.







WRITE OUT
-
REC.







CLOSE EXTFILE.



COBOL file I/O consists three steps (done roughly in this order):

1.
OPEN

the
file

you wish to read from or write to


for
INPUT

or
OUTPUT

(see example next slide)

2.
Begin an iterative processing loop. Somewhere in the loop:


READ

the input
file

-

until “end
-
of
-
file” …and/or…


WRITE

the
record

in the COBOL
FILE SECTION FD



for all records that should be written out

3.
CLOSE

the
file



In subsequent chapters we will cover COBOL loop statements

Steps/Order:


1. OPEN <input/output> <
FILE
NAME>.

2. WRITE <OUTPUT
-
RECORD
>.

3. CLOSE <
FILE
NAME>.

L

O

O

P



See Slide Notes,
for additional
learning content

42

PROCEDURE DIVISION


Imperative Statements


OPEN

ENVIRONMENT DIVISION.

INPUT
-
OUTPUT SECTION.



SELECT INTFILENAME ASSIGN TO …

DATA DIVISION.

FILE SECTION

FD


INTFILENAME…

01


OUT
-
REC.





PROCEDURE DIVISION.



OPEN OUTPUT INTFILENAME.



WRITE OUT
-
REC.



CLOSE INTFILENAME.


Syntax:

OPEN OUTPUT INPUT EXTEND <InternalFileName>.



OPEN
connects your program to the external device assigned in your
ENVIRONMENT DIVISION.


Your program must issue an OPEN statement, before you can READ to or WRITE from the file


OPEN INPUT



for files you
READ

from


OPEN OUTPUT



for files you
WRITE

to from scratch (over
-
writing the contents of any existing records in the file)


OPEN EXTEND



for files you
WRITE

to and append new records starting at the end of the file


CLOSE

all
OPEN

files before ending your program


You can
OPEN

multiple files in one statement (see Slide Notes)


External

Device


External File


COBOL (internal) File Name



See Slide Notes, for
additional learning content

43

PROCEDURE DIVISION


Imperative Statements


WRITE

ENVIRONMENT DIVISION.

INPUT
-
OUTPUT SECTION.



SELECT INTFILENAME ASSIGN TO …

DATA DIVISION.

FILE SECTION

FD


INTFILENAME…

01


OUT
-
REC.





PROCEDURE DIVISION.



OPEN OUTPUT INTFILENAME.



WRITE OUT
-
REC.



CLOSE INTFILENAME.


Syntax:

WRITE <OUTPUT
-
RECORD>.



WRITE
adds one record to the end of an
OPEN OUTPUT

file.


You write:


The
record

in the
FD
for the file in the

FILE SECTION


The
record

in the
FD
for the file in the

FILE SECTION FROM
a record in the

WORKING
-
STORAGE
SECTION


External

Device


External File




See Slide Notes,
for additional
learning content

44

PROCEDURE DIVISION


Imperative Statements


CLOSE

ENVIRONMENT DIVISION.

INPUT
-
OUTPUT SECTION.



SELECT INTFILENAME ASSIGN TO …

DATA DIVISION.

FILE SECTION

FD


INTFILENAME…

01


OUT
-
REC.





PROCEDURE DIVISION.



OPEN OUTPUT INTFILENAME.



WRITE OUT
-
REC.



CLOSE INTFILENAME.


Syntax:

CLOSE <InternalFileName>.



Issue a
CLOSE
statement for every file you are using before your program
ends.

CLOSE:


Closes an external file


And makes it unavailable for processing


Unless/until a subsequent OPEN statement occurs


You can close multiple files in a single statement


External

Device


External File


F

I

L

E


C

L

O

S

E



See Slide Notes,
for additional
learning content

®


IBM Software Group

© 2006 IBM Corporation

Enterprise COBOL Education Using Rational Developer for System Z


Module 3


Basic COBOL Statements

Jon Sayles, IBM Software Group, Rational EcoSystems Team

46

Course Description


Course Name:
COBOL Foundation Training
-

with RDz



Course Description:
Learn the COBOL language, RDz and learn z/OS terms, concepts and development skills in
this course.



Pre
-
requisites:

Some experience in a 3rd or 4th Generation Language is expected. SQL is also recommended.



Course Length:
10 days



Topics (Agenda)


Getting Started
-

installing and configuring RDz
-

and the course materials, and using Eclipse to edit COBOL


COBOL General Language Rules


Basic COBOL Statements


Advanced record and table handling


Debugging Programs
-

Note: Deep dive on using RDz for common COBOL programming errors (001, 0C4, 0C7, infinite loops, fall
-
thru, etc.)


Input/Output and Report Writing Patterns


COBOL Subprograms and the Linkage Section


Structured Programming Concepts, professional COBOL development practices and Coding Patterns


Advanced Character Manipulation, COBOL Intrinsic Functions, Date and Time coding patterns, and Language Environment calls


OS/390 Concepts and JCL
-

Compile/Link & Run Procs on the mainframe


Indexed file Coding Patterns


Sort/Merge, Sequential File Match/Merge and Master File Update Coding Patterns


Accessing DB2 Data and DB2 Stored Procedures


COBOL in the Real World:


CICS
-

lecture only


IMS (DL/I and TM)
-

ditto


Batch processing
-

ditto


Java calling COBOL


COBOL and XML Statements


SOA and COBOL
-

creating and calling Web Services


Web 2.0 using Rich UI


47

Unit



Assignment Statements and Internal Data


Representation




Math Operations




Conditional Logic




Transfer of control




COBOL Looping Constructs




Sequential File Processing Patterns




Java and .NET Equivalents

Topics:

COBOL General Language
Rules

48

Topic objectives

After completing this topic, you should be able to:


Describe the COBOL MOVE and assignment operation


List the three types of COBOL assignment statements


and what causes the
COBOL compiler to choose one type over the other


Define the rules for:


Alphanumeric moves


Numeric moves


Group or structure moves


List a few optional MOVE clauses, and describe what they do, including:


MOVE CORRESPONDING


Code MOVE statements that are syntactically correct


Describe the underlying COBOL storage representation for:


Alphanumeric data


Numeric data


List the common COBOL Figurative Constants


Describe what the COBOL INITIALIZE statement does

49

COBOL Picture Clauses and Internal Data Representation

COBOL Data types are
PIC

clause dependent, and come in several broad categories:


Character Data:


Fixed Length:

PIC X(nn)


or PIC A(nn)

… Note, A


"Alphabetic data"


Stored as EBCDIC bytes


examples:
A



Hex:

C1
,
B



Hex:

C2
,
9



Hex:

F9
, etc



http://theamericanprogrammer.com/code7302/ebcdic.txt




Numeric Data:


Display

numeric (aka Zoned Decimal):
PIC 9(5), PIC S9(5)V99


Stored as EBCDIC bytes, with "assumed" decimal place


one byte per digit


Hex values:
F0





With
V

in declaration


COBOL takes care of decimal alignment in math/
MOVE

operations


With
S

(sign) in declaration, the last byte of internal storage holds the sign (C or D):


C

-

is positive number:


PIC S9(5)V99
-

value: 321.19


䘰⁆〠䘳⁆㈠䘱⁆ㄠ䌹


D

-

is negative number


PIC S9(5)V99
-

value: 321.19


䘰⁆〠䘳⁆㈠䘱⁆ㄠ䐹



Binary (COMP) numeric


used to improve run
-
time COBOL arithmetic instruction performance:


Small binary:
PIC S9(4) COMP



stored in two bytes, pure binary (base 16) data


Medium binary:
PIC S9(9) COMP



stored in four bytes


Large binary:
PIC S9(18) COMP



stored in eight bytes


With or without Signs, and assumed decimal places:

Ex.


PIC S9(4) COMP Value 123.
Stored as:
0123


PIC S9(4) COMP Value
-
123.
Stored as twos complement of

123: FF85




Packed decimal (COMP
-
3) numeric


frequently used to reduce the size of a file:


Two digits per/byte. Sign bit


in last "nibble" (bottom 1/2 of last byte). "Slack bytes" (other high
-
order/leading
zeroes) may need to be added to the internal storage to account for evenly divisible PIC clause + sign nibble

Ex.


PIC S9(5)V99 COMP
-
3 Value 123.99.
Stored as:

00 12 39 9C


PIC S9(5)V99 COMP
-
3 Value
-
123.99.
Stored as:

00 12 39 9D

Important


See Slide Notes

EBCDIC Internal

Data Representation

50

COBOL Picture Clause Editing and External (Output) Field Values


There are a number of other
PIC

clauses used for output result data on reports and green
-
screen I/O. They are simple to understand, and very convenient. Note that the numeric
PIC

clauses
force decimal
-
point alignment. The alphanumeric
PIC

clauses all align the data left.



Z Suppress leading zeroes


insert floating blanks




PIC ZZ,ZZZ,Z99.99


B Alphanumeric "mask"


Positionally insert a blank into alphanumeric data





PIC XBXBXB



0 Numeric "mask"
-

Positionally insert a zero into a numeric field.





PIC 990990


/ Data mask
-

Insert a slash into alphanumeric data


typically used for dates




PIC XX/XX/XX


, Insert a comma into numeric data




PIC Z,ZZZ,Z99.99


. Insert a decimal point, into numeric data




Z,ZZZ,Z99.99


+ Insert a plus sign


IF the value in the variable is > 0



PIC Z,ZZZ,Z99.99+


-

Insert a negative sign


IF the value in the variable is < 0



PIC Z,ZZZ,Z99.99
-


CR Insert a "CR" literal


IF the value in the variable is < 0



PIC Z,ZZZ,Z99.99CR


DB Insert a "DB" literal


IF the value in the variable is < 0



PIC Z,ZZZ,Z99.99DB


* Insert asterisks instead of leading zeros for numeric values



PIC ********9999



$ Dollar
-
sign suppressions
-

Insert a floating, left
-
justified $ sign for numeric values



PIC $$,$$$,$99.99

51

ILE Reference Manual


Picture Clause Editing Examples


1 of 2


The ILE reference manual has an excellent sample guide to Picture Clause editing


http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/c0925395267.htm





52

ILE Reference Manual


Picture Clause Editing Examples


2 of 2


The ILE reference manual has an excellent sample guide to Picture Clause editing


http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/c0925395267.htm





53

COBOL Storage Representation


Reference Chart


Picture Clause


Meaning


Value


Internal Value

Internal Storage
(EBCDIC bytes)

PIC X(10)

Fixed alpha
-
numeric data

'ABC'

ABCbbbbbbb
(seven trailing
blanks)

CCC4444444

12300000000

PIC 9(5)

Display numeric (Zoned Decimal) numbers

-
321

00321
-

Note
-

without S (sign)
value get "absolute value"

FFFFF

00321

PIC S9(5)

Zoned decimal
-

with sign

-
321

00321
-

Value is negative in math
operations

FFFFD

00321

PIC S9(5)V99

Zoned decimal with implied decimal place

321.5

00321V50

FFFFFFC

0032150

PIC S9(4) COMP

Small binary number
-

2 byte
-

note, negative
binary #s stored as two's complement

321

0321

04

11

PIC S9(5) ==> S9(9) COMP

Binary number
-

4 bytes

321

00000321

0004

0011

PIC S9(9) ==> S9(18) COMP

Large Binary number

321

000000000000000321

00000004

00000011

PIC 9(4) COMP
-
3

Packed decimal number

321

0321

031

02F

PIC S9(5)V99 COMP
-
3

Packed decimal number

321.99

00321.99

0319

029C

PIC S9(5)V99 COMP
-
3

Packed decimal number

-
321.99

00321.99


Value is negative in
math operations

0319

029D

External PIC clauses Value
Output/Display Value

Notes

PIC Z(3)

Zero
-
suppressed

-
2.99

2

No decimal places, no sign

PIC ZZ,ZZZ,ZZZ.99
-

Zero
-
suppressed, with commas and minus sign

-
321.99

321.99
-

Sign on right (can be on left). Zeroes suppressed

PIC $$,$$$,$99.99

Dollar
-
sign suppressed

-
321.99

$321.99

One resulting $, if need sign, must use dash (
-
)

PIC XBXBXB

Blank insertion

ABC

AbBbCb

Positional blank (b) inserted

PIC 990990

Zero insertion

-
321.99

030210

High
-
order inserted, numbers inserted @ PIC 9

PIC XX/XX/XX

Slash insertion

012099

01/10/99

Date mask

PIC Z,ZZZ,Z99.99CR

Zero
-
suppressed, with commas and Credit sign

-
321.99

321.99CR

If number was positive, CR would not appear

PIC ****9999

Asterisk suppressed

-
333321.99

**321

Like Z and $
-

asterisk suppresses leading zeros

PIC Z,ZZZ,Z99.99DB

Zero
-
suppressed, with commas and Debit sign

321.99

321.99

If number was negative, DB would appear

54

88
-
Level Variables in the Data Division


88
-
level variables in COBOL programs are "conditionals, built in to the variable
declarations"


They:


Have no
PIC

clause


Have one or more
VALUES



specified as literals (see a few valid expressions below)


Are used effectively to make COBOL
IF

conditional expressions readable/maintain
-
able



01 MONTH
-
IND PIC XXX.


88 SHORTEST
-
MONTH VALUE 'FEB'.


88 THIRTY
-
DAY
-
MONTH




VALUES ARE 'JUN', 'SEP', 'APR', 'NOV'.


88 THIRTY
-
ONE
-
DAY MONTH




VALUES ARE 'JAN' 'MAR' 'MAY' 'JUL' 'AUG' 'OCT' 'DEC'.






IF THIRTY
-
DAY
-
MONTH









01 STUDENT
-
GRADE
-
RANGE PIC S9(3).


88 A
-
OK

VALUE 90 THRU 100.


88 NOT
-
BAD

VALUE 80 THRU 89.


88 PRETTY
-
GOOD VALUE 70 THRU 79.


88 RUH
-
ROH

VALUE 60 THRU 79.


88 FAIL

VALUE 0 THRU 59.






IF NOT
-
BAD




MOVE 'DOIN ALRIGHT' TO MSG
-
TEXT




DISPLAY GRADE
-
MSG

Pluses:



Make code more "English" read
-
able



Allow you to centralize conditions in one place
-

in
the DATA DIVISION


Minuses:



Force you to refer to the DATA DIVISION for the
actual values tested in the code in the PROCEDURE
DIVISION

55

Review


Imperative Statements


MOVE

FILE SECTION



01 OUT
-
REC

PIC X(80).



WORKING
-
STORAGE SECTION.

77 INPUT
-
DATA

PIC X(40).



PROCEDURE DIVISION.







ACCEPT INPUT
-
DATA.



MOVE INPUT
-
DATA TO OUT
-
REC.


Syntax:

MOVE <to
-
variable> TO <from
-
variable>.



MOVE
copies

the value in the
to
-
variable

to the
from
-
variable

(receiving
field),

over
-
writing the
from
-
variable
’s

storage contents.


There are two types of
MOVE operations:


Alphanumeric
MOVE


Numeric
MOVE


The receiving field's
datatype

(
PIC

clause) dictates the type of
MOVE

operation


It is important to understand how the contents of variable storage are affected by
MOVE
. So, let's drill
-
down now, on
PIC

clauses and COBOL internal data
storage representation

56

Alphanumeric
MOVE

Statements

Alphanumeric
MOVE

statements:


Occur when the receiving field is considered Alphanumeric by the compiler


PIC X


PIC A


The Alphanumeric formatted output PIC clauses:


PIC with B, etc.


Proceed from left
-
to
-
right


and copy byte
-
for
-
byte



When the sending and receiving fields are not the same size:


Space (blank) fill to the right, if the receiving field is longer than the sending field


Truncate if the receiving field is shorter than the sending field (typically you will get a
W
-
level

diagnostic
when you compile a program with a
MOVE

statement that truncates data)


Except


can
JUSTIFY

the receiving field data
RIGHT


FLD1 PIC X(4) VALUE 'ABCD'.


FLD2 PIC X(10) JUSTIFY RIGHT.





MOVE FLD1 TO FLD2.


Results in
FLD2

value of:
bbbbbbABCD


If
JUSTIFY RIGHT
, and the receiving field is shorter than the sending field, truncation occurs in the right
-
most
characters


If
JUSTIFY RIGHT
, and the receiving field is longer than the sending field, blanks are added to the left
-
most
positions


Note: if,
FLD1 PIC X(10) VALUE 'ABCD'

result of
MOVE

is:
ABCDbbbbbb
.


Why?


Copy:


Internal storage values byte for byte


For the # of bytes in the receiving field



Group data (structures) are considered Alphanumeric


in
MOVE

operations

57

Numeric
MOVE

Statements


Numeric
MOVE

statements:


Occur when the receiving field's
PIC

clause is numeric


9 …
anywhere

in the

PIC
clause



Input field declaration:


V, S


Output (formatted numeric
PIC

clause in declaration)


Z, $, 0, DB, CR, +,
-
, period (.), comma (,)



MOVE

copies the algebraic value of the sending field to the
receiving field as follows:


Automatically aligns at the decimal place


Or assumed decimal place if no
V

in
PIC

clause



Pad to the left with leading, and to the right with trailing
zeroes
, if the receiving field has
more decimal digits to the left or the right of the (assumed) decimal positions



If sign (S) or numeric edited sign
MOVE

will value the receiving positively or negatively
depending on the sending field's value. If no sign, the receiving field gets absolute value



Will numerically truncate


if receiving field has fewer decimal digits to the left of the
decimal place, or less digits (decimal precision) to the right of the decimal place

What about mixed datatype
MOVE

statements? (next slide…)

58

MOVE Statements When the Datatypes Are Not The Same


Obviously you can move
PIC X

fields to other
PIC X



and
PIC 9

fields to other
PIC 9

fields, but sometimes, due to business requirements you will have to code
MOVE

statements of unlike (mixed) datatypes.



Here are the general rules:


Anything can be moved to an alphanumeric PIC X field


Only other PIC 9 (input) fields can be moved to PIC 9 fields


And only PIC 9 fields can be moved to numeric edited fields


PIC 9 numeric edited fields can be moved to PIC 9 fields


Group moves are considered exactly the same as an alphanumeric PIC X field move by the
compiler


And all the rules you just learned about truncation, padding and decimal alignment also apply



This table may help clarify the rules for mixed data type
MOVE

Sending
Fields

Receiving Fields


PIC X


PIC 9

Numeric
Edited

PIC X

Y

N

N

PIC 9

Y

Treated as an alphanumeric MOVE
(digits may be lost, trailing blanks
may be added, etc.)

Y

Y

Numeric Edited

Y

Y

N

59

Allowable

MOVE Statements


Please
consult this
verbose (!)
table to see
the complete
list of COBOL
allowable
MOVE
statements


based on
sending and
receiving field
PIC types.



Don't worry if
there are
terms you're
not up to
speed on just
yet


we'll get
you there.



Patience


60

Alphanumeric
MOVE

Statements
-

Examples

Sending Field

Receiving Field

Sending Field Value

Result in Receiving
Field

PIC X(5)

PIC X(5)

ABCDE

ABCDE

PIC X(5)

PIC X(3)

ABCDE

ABC

PIC X(5)

PIC X(8)

ABCDE

ABCDEbbb

PIC 9(5)

PIC X(8)

12345

12345bbb

PIC X(5)

PIC 9(5)

Not allowed by the compiler

N/A

PIC S9(5)V99 COMP
-
3

PIC X(8)

Not allowed by the compiler


as is non
-
integer numeric (see table of moves)

13574444 (hex)

246C0000

PIC S9(4) COMP

PIC X(4)

1234

1344

2400

PIC X(4)

PIC X(10) JUSTIFY RIGHT.

ABCD

bbbbbbABCD

Numeric Value

PIC 9(5)V99

PIC 9(3)V9

12345.67

345.67

PIC S9(7)V99

PIC 9(4) COMP

123456789.99

6789

PIC S9(5)V99

PIC S9(7)V999 COMP
-
3

-
12345.67

0012345.670 (minus carried in sign bit)

PIC S9(9) COMP

PIC 9(5)V99

1234567912

67912.00

PIC S9(5)V99 COMP
-
3

PIC 9

-
12345.67

5

Group Moves

CUSTOMER
-
NAME

PIC X(18)

SMITHbbbbbAALEXAND

CUSTOMER
-
RECORD

PIC X(40)

1201SMITHbbbbbAALEXANDERbbbbbbbbbbbbbbbbbbbbbb

01 CUSTOMER
-
RECORD.


05 ID
-
NUMBER PIC 9(2) VALUE 12.


05 CUST
-
TYPE PIC 9(2) VALUE 01.


05 CUSTOMER
-
NAME.


10 FIRST
-
NAME PIC X(10) VALUE 'SMITH'.


10 MIDINIT PIC X(01) VALUE 'A'.


10 LAST
-
NAME PIC X(10) VALUE 'ALEXANDER'.

61

MOVE With OF Modifier


FILE SECTION.





01 CUSTOMER
-
RECORD
-
IN.


05 ID
-
NUMBER PIC 9(2).


05 CUST
-
TYPE PIC 9(2).


05 CUSTOMER
-
NAME.


10 FIRST
-
NAME PIC X(10).


10 MIDINIT PIC X(01).


10 LAST
-
NAME PIC X(10).



01 CUSTOMER
-
RECORD
-
OUT.


05 ID
-
NUMBER PIC 9(2).


05 CUST
-
TYPE PIC 9(2).


05 CUSTOMER
-
NAME.


10 FIRST
-
NAME PIC X(10).


10 MIDINIT PIC X(01).


10 LAST
-
NAME PIC X(10).






PROCEDURE DIVISION.



MOVE FIRST
-
NAME OF CUSTOMER
-
RECORD
-
IN TO FIRST
-
NAME OF CUSTOMER
-
RECORD
-
OUT.




Syntax:

MOVE <from
-
variable> of <from
-
Group> to <to
-
variable>.



The OF modifier allows you to:


Fully
-
qualify elementary field names


making large programs with 1,000's variables easier to maintain


Compile programs with duplicate elementary field names

62


FILE SECTION.





01 CUSTOMER
-
RECORD
-
IN.


05 ID
-
NUMBER PIC 9(2).


05
CUST
-
TYPE

PIC 9(2).


05 CUSTOMER
-
NAME.


10
FIRST
-
NAME

PIC X(10).


10 MIDINIT PIC X(01).


10 LAST
-
NAME PIC X(10).



01 CUSTOMER
-
RECORD
-
OUT.


05 ID
-
NUMBER PIC 9(2).


05
CUSTTYP

PIC 9(2).


05 CUSTOMER
-
NAME.


10
FIRSTNAME

PIC X(10).


10 MIDINIT PIC X(01).


10 LAST
-
NAME PIC X(10).






PROCEDURE DIVISION.



MOVE CORRESPONDING CUSTOMER
-
RECORD
-
IN TO CUSTOMER
-
RECORD
-
OUT.




Syntax:

MOVE CORRESPONDING <from
-
group
-
variable> to <to
-
group
-
variable>.



The
CORRESPONDING

modifier allows you to
MOVE

group records:


When COBOL variable names match between the from and to groups, data is copied


And only when COBOL variable names match

MOVE CORRESPONDING

Field's data not moved

Field's data not moved

See Notes

63

COBOL
INITIALIZE

Statement


INITIALIZE values selected types of data fields. The default is:


Numeric data to zeros


alphanumeric data (PIC X or Group Data Items) to spaces.




You can also INITIALIZE fields, replacing classes of datatypes with specific values


This is used less frequently, and is shown in the
slide notes



Initializing a structure (INITIALIZE)


You can reset the values of all subordinate data items in a group item by applying the
INITIALIZE statement to that group item.

64

COBOL Figurative Constants


FILE SECTION.





01 CUSTOMER
-
RECORD
-
IN.





77 COBOL
-
CHAR
-
FIELD

PIC X(4).


77 COBOL
-
NUM
-
FIELD

PIC X(4).


77 COBOL
-
QT


PIC X(1) VALUE QUOTE.




PROCEDURE DIVISION.



MOVE LOW
-
VALUES to CUSTOMER
-
RECORD
-
IN.




MOVE HIGH
-
VALUES to COBOL
-
CHAR
-
FIELD.




MOVE SPACES


to COBOL
-
CHAR
-
FIELD.




MOVE ZEROS to COBOL
-
NUM
-
FIELD.



MOVE ALL '_' TO COBOL
-
CHAR
-
FIELD.




Syntax:

MOVE <figurative constant> to <Cobol variable>.



There are a number of COBOL figurative constants available to fill the storage of
alphanumeric fields (or in the case of ZEROS), either:


With
MOVE

statements


In VALUE clauses of field declarations


The figurative constants are reserved words, and there are a number of alternative spellings
(see the COBOL language reference, or RDz Help system)


They are sometimes used by convention to signal some processing event:


READ <fileName> AT END MOVE HIGH
-
VALUES TO <inputRecord>.

65

Unit

Topics:

COBOL General Language
Rules



Assignment Statements and Internal Data


Representation




Math Operations




Conditional Logic




Transfer of control




COBOL Looping Constructs




Sequential File Processing Patterns




Java and .NET Equivalents

66

COBOL Mathematical Operators


As you'd expect from a a
business oriented language
, COBOL's math
capabilities are simple, flexible, deep and robust.


Simple operations:


ADD


SUBTRACT


MULTIPLY


DIVIDE

…with a number of variations of operand/operator expressions and automatic decimal
alignment across all numeric types


Algebraic statements:



COMPUTE



uses arithmetic operators (next slide) to perform math operations


COBOL Intrinsic "Built
-
in" math Functions


Here are a few of the math
-
oriented COBOL intrinsic functions:


ACOS

ANNUITY

ASIN

ATAN

CHAR

COS

FACTORIAL

LOG

LOG10

MAX

MEAN

MEDIAN

MIDRANGE

MIN

MOD

PRESENT
-
VALUE

RANDOM

RANDOM

RANGE

REM

REVERSE

SIN

SQRT

STANDARD
-
DEVIATION

SUM

TAN

VARIANCE

67

Fixed
-
Point Arithmetic Statements***


ADD


WORKING
-
STORAGE SECTION.


01 WORK
-
FIELDS.



05 ITEM
-
1 PIC S9(3)V99 VALUE 85.52.



05 ITEM
-
2 PIC S9(3)V99 VALUE 2.34.



05 SUM PIC S9(5)V99 VALUE 0.


PROCEDURE DIVISION.







ADD ITEM
-
1, ITEM
-
2 TO SUM ROUNDED



ON SIZE ERROR MOVE 0 TO SUM



END
-
ADD




ADD ITEM
-
1 ITEM
-
2 GIVING SUM ROUNDED



ON SIZE ERROR MOVE 0 TO SUM



DISPLAY '** ON SIZE ERROR **'



END
-
ADD



Format 1


Adds the addend to the sum


Format 2


The values of all operands preceding the word GIVING are added together, and the
sum is stored as the new value.


None of the values in the operands preceding GIVING are changed



Note: There is a Format 3 statement:
ADD CORRESPONDING


Rounded


Rounds to the decimal digits in
the PIC clause (See Slide Notes)


ON SIZE ERROR


Allows you to handle arithmetic
overflow conditions with
imperative statements.


Additional note:
You can
substitute a numeric literal for the
variables in the
ADD

and
TO

(but
not
GIVING
) clauses in the
statement

***See Notes

68

Arithmetic Statements


ROUNDED

and
ON SIZE ERROR


WORKING
-
STORAGE SECTION.


01 WORK
-
FIELDS.



05 ITEM
-
1 PIC S9(5)V99 VALUE 85.56.



05 RESULT PIC S9(6) VALUE 100.


PROCEDURE DIVISION.







ADD ITEM
-
1 TO RESULT.





MOVE 100 TO RESULT.





ADD ITEM
-
1 TO RESULT ROUNDED.





MOVE 140156.33 TO ITEM
-
1.




MOVE 900000 TO RESULT.






ADD ITEM
-
1 TO RESULT ROUNDED.



††
MOVE 900000 TO RESULT




ADD ITEM

1 TO RESULT



低⁓䥚䔠䕒剏删䵏噅M〠呏0单䴠




DISPLAY '** ON SIZE ERROR **'.

Value: 185


V慬ue: ㄸ6

The
ROUNDED

phrase allows you to specify rounding up to the
nearest low
-
order decimal digit of precision. This means that
ROUNDED

adds 1 to the absolute value of the low
-
order digit of
the result variable
IF
the absolute value of the next least
significant digit of the
intermediate

variable value is >= to 5.

Value: 040156


V慬ue: 〰〰〰

69

Arithmetic Statements


SUBTRACT


WORKING
-
STORAGE SECTION.


01 WORK
-
FIELDS.



05 ITEM
-
1 PIC S9(3)V99 VALUE 85.52.



05 ITEM
-
2 PIC S9(3)V99 VALUE 2.34.



05 DIFFERENCE PIC S9(5)V99 VALUE 0.


PROCEDURE DIVISION.







SUBTRACT ITEM
-
1, ITEM
-
2 FROM DIFFERENCE ROUNDED



ON SIZE ERROR MOVE 0 TO DIFFERENCE



END
-
SUBTRACT




SUBTRACT ITEM
-
1 FROM ITEM
-
2 GIVING DIFFERENCE ROUNDED



ON SIZE ERROR MOVE 0 TO DIFFERENCE



DISPLAY '** ON SIZE ERROR **'



END
-
SUBTRACT



Format 1


Subtracts all minuends from the sum


Format 2


The values of all operands preceding the word GIVING are added together, and the
sum is subtracted from the difference (in the above)


None of the values in the operands preceding GIVING are changed



Note: There is a Format 3:
SUBTRACT CORRESPONDING


Rounded


Rounds to the decimal
digits in the PIC clause


ON SIZE ERROR


Allows you to handle
arithmetic overflow
conditions with imperative
statements.



Additional note:
You
can substitute a numeric
literal for the variables in
the
SUBTRACT

and
FROM

(but not
GIVING
)
clauses in the statement



70

Arithmetic Statements


MULTIPLY


WORKING
-
STORAGE SECTION.


01 INPUT
-
FIELDS.



05 ITEM
-
1


PIC S9(3)V99 VALUE 85.52.



05 ITEM
-
2


PIC S9(3)V99 VALUE 2.34.



05 ITEM
-
3


PIC S9(3)V99 VALUE 6.01.



05 RESULT


PIC S9(5)V99.


PROCEDURE DIVISION.




MULTIPLY ITEM
-
1 BY ITEM
-
3 ROUNDED



ON SIZE ERROR MOVE 0 TO DIFFERENCE



END
-
MULTIPLY




MULTIPLY ITEM
-
1 BY ITEM
-
2 GIVING RESULT ROUNDED



ON SIZE ERROR MOVE 0 TO DIFFERENCE



DISPLAY '** ON SIZE ERROR **'



END
-
MULTIPLY



Format 1


The values of the operand preceding the word BY is multiplied by the operand after
the word BY


Format 2


The value of the operand after the word
GIVING

is replaced by the multiplication of
ITEM
-
1 BY ITEM
-
2


None of the values in the operands preceding
GIVING

are changed


71

Arithmetic Statements


DIVIDE


Common Usage and Formats


WORKING
-
STORAGE SECTION.


01 INPUT
-
FIELDS.



05 ITEM
-
1


PIC S9(3)V99 VALUE 85.52.



05 ITEM
-
2


PIC S9(3)V99 VALUE 2.34.



05 ITEM
-
3


PIC S9(3)V99 VALUE 6.01.



05 RESULT


PIC S9(5)V99.


PROCEDURE DIVISION.




DIVIDE ITEM
-
1 INTO ITEM
-
3 ROUNDED



ON SIZE ERROR MOVE 0 TO DIFFERENCE



END
-
DIVIDE




DIVIDE ITEM
-
1 INTO ITEM
-
2 GIVING RESULT ROUNDED



ON SIZE ERROR MOVE 0 TO DIFFERENCE



DISPLAY '** ON SIZE ERROR **'



END
-
DIVIDE





DIVIDE ITEM
-
1 BY ITEM
-
3 ROUNDED



REMAINDER ITEM
-
3



END
-
DIVIDE




DIVIDE ITEM
-
1 INTO ITEM
-
2 GIVING RESULT ROUNDED



REMAINDER ITEM
-
3



END
-
DIVIDE



Five separate Formats for
DIVIDE
(see Slide Notes)


Rounded


Rounds to the decimal
digits in the PIC clause


ON SIZE ERROR


Allows you to handle
arithmetic overflow
conditions with imperative
statements.



Additional note:
You
can substitute a numeric
literal for the variables in
the
DIVIDE, BY

and
INTO
, (but not
GIVING
)
clauses in the statement



72

Arithmetic Statements


COMPUTE


WORKING
-
STORAGE SECTION.





01 INPUT
-
FIELDS.



05 ITEM
-
1


PIC S9(3)V99 VALUE 85.52.



05 ITEM
-