Background and History COBOL is an acronym for:
COmmon COmmon Business Oriented Language COBOL Design Features o
COBOL was first developed in 1959. It was the second compiled language. (The first was O!T!"#.$
o
COBOL was designed to solve %usiness pro%lems& something its predecessor wasn't ver good at& eg. O!T!"# couldn't do alpha%etic sorts or )eep monetar values in even cents.
o
COBOL was designed to %e *nglish+li)e& so t hat the reader& even a non+programmer& could get an idea of what was going on in the program& simpl % reading the code.
o
COBOL was designed to %e porta%le& so that programs could %e adapted from one computer sstem to run on another& simpl % ma)ing m a)ing a few changes in one isolated section of the code.
Great Moments in COBOL History Year
Event
1 9 59
Origin of of CO COBOL
1 9 ,-
Inten tended #et !elease
1 9 ,
!elease of COBOL ,
1 9 ,5
Inten tended #et !elease
1 9 ,3
!elease of COBOL ,3
1 9 4
Inten tended #et !elease
Description
" new *nglish+li)e programming language& designed for writing %usiness applications.
irst effort at a standardi/ed version ... and loo)& it's onl - ears late0
" few minor improvements over the original ... and loo)& it's onl 2 ears late0
" few more minor improvements ... and loo)& it's onl ears late0 1 9 4
!elease of COBOL 4
1 9 3
Inten tended #et !elease
1 9 35
!elease of COBOL 35
1 9 9
Inten tended #et !elease
-77
!elease of of CO COBOL 7777
6omewhat controversial version + straed from true *nglish+li)e constructs ... and loo)& it's onl 5 ears late0
*verthing ou could ever want and more ... and loo)& it's onl 777 ears late0
Hierarcy The organi/ation of a COBOL program is hierarchical. This is the %rea)down of the levels8
1
rogram
:ivision
6ection
aragraph
6entence
6tatement
Clause ; hrase
Character
It's not necessaril re=uired for all of components co mponents to %e present for the hierarchical relationship to eist. or eample& the !OC*:>!* :I?I6IO# can contain paragraphs which are su%ordinate to it& even if there are no sections used in that division& however& if sections are included in that division& the paragraphs will %e su%ordinate to them.
For more information on how this t his hierarchy is used to create a program, see the Organization section of this tutorial
Layout " line of COBOL code is divided into five distinct areas8 !osition "ame Description 1+, 6e=uence This area was originall designed for entering line num%ers on each individual line li ne #um%er of code. Once cards were replaced with dis) drives& this area fell into i nto disuse. @ore recentl this area has %een used % programmers as a wa to mar) individual lines of code& such as placing a revision num%er on each line of a %loc) of changed code to show at a glance what changes go with an individual program modification. Note: some compilers compilers sequence check this area, area, in which which case, change modification notices are typically entered in the Program Identification area. 4 Comments ; This area is used designate a line as either a comment or a continuation. Continuation Caracter !urpose A This line is a comment. ; This li line is is a commen ment. On a so source li listin ting& it it wi will appear on on th the to top of a new page. + This line is a continuation of the previous line. Note: COBO COBO is freeform freeform and in most cases cases it isn!t necessary necessary to use this construct, the entry may simply "e continued on the ne#t line. $he continuation feature is used to continue literals or words when you run out of room on the line. Note: If this this line is not a comment or a continuation, this this area should should "e left "lank. 3+1 3+11 "rea "rea " This This area is use used to to en enter ter div divis isio ion n hea heade derrs& sec section tion head headeers& par paragra graph name namess& fil filee that "egins in %rea %rea % is considered considered to "e in indicators and level 1 items. %nything that %rea %. %. 1-+4"rea B This area is is us used to to en enter ter an an statement th that do doesn't go go in in "rea ". ". %nything that "egins in %rea B is considered to "e in %rea B. -
rogram
:ivision
6ection
aragraph
6entence
6tatement
Clause ; hrase
Character
It's not necessaril re=uired for all of components co mponents to %e present for the hierarchical relationship to eist. or eample& the !OC*:>!* :I?I6IO# can contain paragraphs which are su%ordinate to it& even if there are no sections used in that division& however& if sections are included in that division& the paragraphs will %e su%ordinate to them.
For more information on how this t his hierarchy is used to create a program, see the Organization section of this tutorial
Layout " line of COBOL code is divided into five distinct areas8 !osition "ame Description 1+, 6e=uence This area was originall designed for entering line num%ers on each individual line li ne #um%er of code. Once cards were replaced with dis) drives& this area fell into i nto disuse. @ore recentl this area has %een used % programmers as a wa to mar) individual lines of code& such as placing a revision num%er on each line of a %loc) of changed code to show at a glance what changes go with an individual program modification. Note: some compilers compilers sequence check this area, area, in which which case, change modification notices are typically entered in the Program Identification area. 4 Comments ; This area is used designate a line as either a comment or a continuation. Continuation Caracter !urpose A This line is a comment. ; This li line is is a commen ment. On a so source li listin ting& it it wi will appear on on th the to top of a new page. + This line is a continuation of the previous line. Note: COBO COBO is freeform freeform and in most cases cases it isn!t necessary necessary to use this construct, the entry may simply "e continued on the ne#t line. $he continuation feature is used to continue literals or words when you run out of room on the line. Note: If this this line is not a comment or a continuation, this this area should should "e left "lank. 3+1 3+11 "rea "rea " This This area is use used to to en enter ter div divis isio ion n hea heade derrs& sec section tion head headeers& par paragra graph name namess& fil filee that "egins in %rea %rea % is considered considered to "e in indicators and level 1 items. %nything that %rea %. %. 1-+4"rea B This area is is us used to to en enter ter an an statement th that do doesn't go go in in "rea ". ". %nything that "egins in %rea B is considered to "e in %rea B. -
42+3
rogram Identification
This area was originall designed for entering the program identificationon each individual line of code. Once cards were replaced with dis) drives& this area fell into disuse. @ore recentl this area has %een used % programmers as a wa to mar) individual lines of code& such as placing a revision num%er on each line of a %loc) of changed code to show at a glance what changes go with an individual program modification& especiall with compilers that still se=uence chec) the se=uence num%er area.
#dentifiers COBOL Identifiers are 1 to 2 characters in length. The characters ma %e8 •
•
"lpha%etic ("+$ #umeric (+9$
•
phen (+$
1.
The hphens hphens are are used used to Doin multiple multiple words words togethe togetherr to create create one meanin meaningful gful name. name.
-.
"n hphen hphenss used used should should %e %e em%edded em%edded %etween %etween other other chara characters cters
2.
phen phenss sho should uld not not %e adDa adDacen centt to one one anoth another er
.
:ata names names should should descri%e descri%e the the content content of the field field in a meaningful meaningful wa wa..
5.
Onl common common a%%r a%%revi eviatio ations ns shoul should d %e used used
Constants Figurative Constants COBOL has several %uilt+in constants. These constants are called figurative constants. The are named according to their values. @ost of them have alternative spellings availa%le. "n of these spellings m a %e used to produce the same result. This is a list of all of the figurative constants8 $E%O "lternate 6pellings8 $E%O&& $E%OE& This figurative constant can %e used to load a data item with as man /eros as it ta)es to fill the data item. *!O6 ma %e used to fill data items of an tpe. &!'CE "lternate 6pelling8 &!'CE& This figurative constant can %e used to load a data item with as man spaces as it ta)es to fill the data item. 6"C*6 ma not %e used to fill numeric data items. LO()*'L+E "lternate 6pelling8 LO()*'L+E& " LO<+?" LO<+?"L>* L>* is the lowest possi%le character in our m achine's collating se=uence. On most machines& that would mean that all of the %its in the character are set to 's. This figurative constant can %e used to load a data item with as man of this value as it ta)es to fill the data item. LO<+?"L LO<+?"L>*6 >*6 ma not %e used to fill numeric data items. H#GH)*'L+E "lternate 6pelling8 H#GH)*'L+E& " IE+?"L>* IE+?"L>* is the highest possi%le character in our machine's collating se=uence. On most machines& that would mean that all of the %its in the character are set to 1's. This figurative
2
constant can %e used to load a data item with as man of this value as it ta)es to fill the data item. IE+?"L>*6 ma not %e used to fill numeric data items. ,+O-E "lternate 6pelling8 ,+O-E& " F>OT* is whatever character our compiler uses as a =uote. In most COBOLs& this is the =uotation mar). In some COBOLs& this is the apostrophe. This figurative constant can %e used to load a data item with this character. F>OT*6 ma not %e used to fill numeric data items. 'LL literal The literal is an non+numeric literal. This figurative constant can %e used to load a data item with this character or character string. The character or character string will %e repeated as often as necessar to fill the data item. The "LL literal ma not %e used to fill numeric data items. Litera.s
"umeric Litera.s #umeric literals can consist of an com%ination of numeric characters. #o more than 13 total digits are allowed. One decimal point ma %e included and a sign (either a plus or minus sign$ ma %e included in the front of the item. If the sign is not included& the value is assumed to %e positive.
"on)"umeric Litera.s #on+numeric literals must %e enclosed in =uotes. "n character ma %e included ecept a =uote. If it is necessar for a =uote to appear in the literal& code two =uotes adDacent to each other in the literal. The will %e interpreted % the compiler as one =uote and onl one =uote will appear in the resultant literal. @ost COBOLs use the =uotatation mar) for the =uote. 6ome use the apostrophe. 6ome COBOLs allow either to %e usedG in such COBOLs& it possi%le to include a =uotation mar) in the literal % enclosing the literal in apostrophes and to include an apostrophe in the literal %e enclosing the literal in =uotation mar)s.
Data #tems Leve. "um/ers
Leve.s 01)23 4Data &tructuring #tems5 Levels 1+9 are used to create hierarchical data items. The highest level item in the hierarch will alwas %e the 1 item and ever item contains the items %eneath it with hi gher level num%ers (up until the net item with a lower level num%er or the end of the section or division$. Items which contain other data items are called group items and those which don't contain other data items are called elementar items.
"lthough all of the num%ers from 1+9 are availa%le& % convention usuall onl the levels 1& 5& 1& 15& -& -5& etc. are used. &.e 01 A. 05 B. 10 C PIC X(20). 10 D PIC X(10). 05 E. 10 F PIC X(5). 10 G PIC 9(10).
In this eample& there are three group level items& "& B& and * and there are four elementar items& C& :& & and E. The si/es of the elementar items are determined % their picture clauses. 6ee picture clauses %elow for more information on this. The si/es of the group items are determined % the si/es of the elementar items %elow them. 6ince B consists of the elementar items C (- characters$ and : (1 characters$& B is 2 characters in length. 6ince * consists of the elementar items (5 characters$ and E (1
characters$& * is 15 characters in length. 6ince " consists of the su%+groups B (2 characters$ and * (15 characters$& " is 5 characters in length.
Oter Leve. "um/ers Leve. 66 4%enaming #tems5 6ometimes it is desira%le to group items from two separate groups into a group of their own. 6a we want to create a new group of items consisting of items : and & %ut we don't want to interfere with these data items also %elonging to groups B and *. &.e 01 A. 05 B. 10 C PIC X(20). 10 D PIC X(10). 05 E. 10 F PIC X(5). 10 G PIC 9(10).
In this eample& we could add a line after the last line& to create a new group from items : and . To do this we would code8 66 H RENAMES D THRU F.
following the last line of the 1 level item. &In this case, following the line which defines '.(
Leve. 77 4&tand '.one #tems5 e)el ** items ha)e "een designated for deletion from the COBO language and ha)e "een included here only in case you encounter one in an e#isting program. e)el ** items should not "e coded in new programs. " level 44 item is used to define an item which is not to %e su%divided an further. Hou can accomplish the same thing with a level 1 item& Dust don't su%divide it.
Leve. 88 4Condition "ame #tems5 " level 33 item is used to give a name to a condition. >sing a condition name has two primar %enefits8 •
•
It allows a descriptive name to %e assigned to what otherwise might not %e an o%vious reason for a test. If a condition that is su%Dect to change due to conditions %eond the control of the programmer and that condition is tested multiple places in the !OC*:>!* :I?I6IO#& using the condition name in the !OC*:>!* :I?I6IO# instead of the actual condition allows the programmer maintaining the program to change the values defining the condition in onl one place in the :"T" :I?I6IO# rather than having to hunt down all of the individual occurences in the !OC*:>!* :I?I6IO#. &.e
05 COMPANY 88 DOING-BUSINESS-IN-CALIFORNIA
PIC 9(4). VALUES 3 15 39 THRU 42 76.
To test for a compan doing %usiness in California& wit hout the 33+level item& ou would have to code8
IF COMPANY = 3 OR 15 OR 39 OR 40 OR 41 OR 42 OR 76
It would not %e apparent to the person reading the code what condition ou were testing for.
5
IF DOING-BUSINESS-IN-CALIFORNIA
"n%od reading the code could tell what condition ou were testing for and if the chec) was done several times throughout the !OC*:>!* :I?I6IO# and the list of companies doing %usiness in California changes& it is onl necessar to change the list of values associated with the 33+level item.
!icture C.auses The icture clause is used to specif the tpe and si/e of an elementar data item. It consists of the word IC or ICT>!* followed % the actual picture clause& where the tpe and si/e are specified. The tpe is specified using a specific character selected from the t a%le %elow. *ach picture character represents one position in the data item. :ifferent characters ma %e com%ined within the same icture clause. @ultiple occurrences of the same tpe of character are indicated % repeating the character or including a repetition factor after the character. or eample& three alpha%etic characters ma %e represented either with t he clause IC """ or IC "(2$. Tpicall& repetitions of less than four characters are done % repeating the character and repetitions of four or more characters are done wit h the num%er enclosed in parentheses. Caracter !urpose
"otes Data %epresentation Caracters
X
"n character
@a contain an character including special or unprinta%le characters
9
#umeric character
@a contain onl the characters through 9
A
"lpha%etic character
@a contain onl the characters " + & a + /& and space 'ritmetic !ositioning Caracters
S
#umeric sign :oes not use an actual storage space& without this character a numeric item would %e treated as unsigned
V
Implied :oes not use an actual storage space& separates the integer portion of a numeric decimal point item from its decimal portionG used to )eep trac) of the decimal point for arithmetic calculationsG this character will not print or displaG use the actual decimal point character& .& for printing
P
#umeric :oes not use an actual storage spaceG used to change precision& e.g. 99 will place holder hold values in amounts of even 1& from /ero to 99& without actuall storing anthing for the three lowest order positionsG can also %e used to represent positions after the decimal& e.g. 9 will hold values in amounts in even thousandths& from to .9 without actuall storing anthing for the two highest order positions "umeric Editing Caracters
-
@inus sign
>sed to displa a sign in a printed item& the + character will %e showed onl if the numeric value is negative& the position will %e left %lan) for positive values
+
lus sign
>sed to displa a sign in a printed item& + will %e shown for negative values& J will %e shown for positive values
.
"ctual 6eparates the integer portion of a numeric item from its decimal portionG this decimal point character cannot %e used in an item involved in a calculationG use the implied ,
decimal point character& ?& for calculations Z
ero 6uppress
!eplaces leading /eros with %lan)sG has no effect on non+/ero positions or /eros not in leading positions
,
Comma
Inserts a comma into the data item in this positionG commonl used as a separator in numeric data items
$
:ollar sign
Inserts a dollar sign into the first position in the data itemG commonl used for currenc
*
"steris)
!eplaces leading /eros with asteris)sG has no effect on non+/ero positions or /eros not in leading positionsG commonl used in chec) writing
CR
Credit
>sed at the end of a ICT>!* clause& the C! will appear in the position indicated if the item is negativeG will %e %lan) for a positive num%er
DB
:e%it
>sed at the end of a ICT>!* clause& the :B will appear in the position indicated if the item is negativeG will %e %lan) for a positive num%er
/
6lash
Inserts a slash into the data item in this positionG commonl used as a separator in date fields
0
ero
Inserts a /ero into the data item in this positionG not commonl used
F.oating !#C-+%E Caracters Certain of the characters a%ove (K& +& J$ have the a%ilit to float. To float a character& simpl repeat it in the leading positions of the item& along with separator commas& if desired. If the item has leading /eros& when it is filled& the leading positions will %e filled with spaces& and the floating character will essentiall move up to meet the actual data area. or eample& filling an item with a picture of KK&KKK& KKK.99 with a value of 14.52 will ield a value of K1&4.52 with four leading spaces.
Conditions There are three different )inds of conditions that can %e tested for in COBOL. !elational tests are used to determine the relationship %etween two items& for eample& if the are e=ual. Class tests are used to determine the tpe of data in an item. Condition name tests are a special tpe of relational test& which have had a condition name associated with a data item. "n of these conditions will %e either true or false upon their evaluation and code can %e eecuted conditionall depending upon the test results. These conditions ma %e com%ined to form comple conditions with logical operators. %e.ationa. -ests " relational test is used to compare t wo items. The test consists of two items& either identifiers or constants& separated % a relational operator& & M& or N. or eample& the condition
ABC-TOT = ZERO
is true when "BC+TOT has a value of /ero. C.ass -ests " class test is used to determine whether a specific tpe of data is stored in an item. The test consists or an identifier followed % the word I6 and a class name& such as "L"B*TIC or #>@*!IC. or eample& the condition
H I6 "L"B*TIC is true when H consists of onl upper and lower case letters and spaces. Condition "ame -ests " condition name test is used to determine whether a pre+defined conditi on from the :"T" :I?I6IO# is true. This conditions will have %een defined in the :"T" :I?I6IO# with an 33+level data name. The test
4
consists of the condition name onl. The condition is true when the data item associated with the condition name has the value associated with the condition names. Creating Comp.e9 Conditions it Logica. Operators "n of the conditions descri%ed a%ove can %e com%ined to form more comple conditions through the use of an of the following logical operators. These logical operators ma %e com%ined as needed to create a multitude of comple conditions.
'"D If two conditions are com%ined wi th an "#:& then %oth conditions must %e true for the com%ined epression to %e true. or eample&
ABC > DEF AND GHI IS NUMERIC
is true onl if the current value of the data+item "BC eceeds the current value of the data item :* and EI contains onl values in the range + 9.
O% If two conditions are com%ined with an O!& then either condition must %e true for the com%ined epression to %e true. or eample&
JKL < MNO OR PQR IS ALPHABETIC
is true if either the current value of the data+item @#O eceeds the current value of the data item PQL or F! contains onl values in the range " + & a + /& or spaces or %oth.
"O"n condition ma %e preceded with the word #OT in order to negate that condition. In relational and class tests& the word #OT ma %e moved into the middle of the condition for reada%ilit. or eample&
STU NOT NUMERIC
is true if there are an characters not in the range + 9 in the data item.
COBOL !rocedures " COBOL procedure is a series of eecuta%le statements used to o%tain a given result. The procedure consists of a paragraph name followed % a period and as man eecuta%le statements as desired. It is even permissa%le (although usuall not terri%l useful$ to have no eecuta%le statements in a procedure. Guide.ines for !rocedure Design •
•
•
•
" procedure should %e designed to perform onl one function. "ll the statements in the procedure should %e related to accomplishing that function. Choose a name for the procedure that descri%es its complete functionalit. rocedures can eecute other procedures within the program % using the *!O!@ statement. " *!O!@ statement causes the program to t ransfer control to another paragraph& eecute all of the statements within that paragraph& and return to the statement following the *!O!@ statement. !epeated eecution of another paragraph can %e accomplished wit h the optional >#TIL phrase that can %e associated with the *!O!@ statement. Qeep the procedure eas to understand. " good length for a procedure is a%out one screen. This allows the programmer attempting to anal/e the program to see the entire procedure at once. This length is Dust a guideline. *tremel simple procedures& such as one whi ch moves a series of fields from an input record to a detail line for printing could %e longer than one screen and still %e understanda%le& while etremel comple procedures& such as one which perform a series of
3
intricate calculations& would %e %etter as a shorter routine especiall one with lots of eplanator comments.
COBOL !rogram Organi;ation There are four divisions in ever COBOL program& I:*#TIIC"TIO#& *#?I!O#@*#T& :"T"& and !OC*:>!*. The must appear in that order. This section of the tutorial descri%es each of these divisions and how to construct them. #DE"-#F#C'-#O" D#*#O" This division consists of the division header and other descriptive paragraphs which are used primaril to document the program. *ach of these paragraphs consists of a paragraph name and a comment entr. These paragraphs are descri%ed %elow in the order of their appearance.
!%OG%'M)#D This paragraph is used to name the program. Code the program's name following the paragraph name. "lthough the program name must follow the rules for identifiers& the name provided is fre=uentl used % the operating sstem& so there ma %e additional restrictions on the name depending on the computer sstem involved. This i s the onl paragraph that is re=uired in the I:*#TIIC"TIO# :I?I6IO#. or eample8
PROGRAM-ID. ABCPROG.
'+-HO% This optional paragraph was removed from the standard in 1935& %ut most COBOL compilers still support its use. Code the programmer's name following the paragraph name. There are no formatting rules for the programmer's name. or eample8
AUTHOR. JANE DOE.
#"&-'LL'-#O" This optional paragraph was removed from the standard in 1935& %ut most COBOL compilers still support its use. Code the programmer's emploer's name following the paragraph name. There are no formatting rules for the programmer's emploer's name. or eample8
INSTALLATION. MAJOR CORPORATION.
D'-E)(%#--E" This optional paragraph was removed from the standard in 1935& %ut most COBOL compilers still support its use. Code the date the program was written following the paragraph name. There are no formatting rules for the date. or eample8
DATE-WRITTEN. JUNE 2001.
D'-E)COM!#LED This optional paragraph was removed from the standard in 1935& %ut man COBOL compilers still support its use. Code the paragraph name onl . The sstem date will %e inserted in this area for documentation purposes. or eample8
DATE-COMPILED.
&EC+%#-Y
9
This optional paragraph was removed from the standard in 1935& %ut most COBOL compilers still support its use. Code the securit level of the program following the paragraph name. There are no formatting rules for the securit level and this entr will have no actual effect on access to the program. The paragraph is for documentation purposes onl. or eample8
SECURITY. CLASSIFIED.
%EM'%<& This optional paragraph was removed from the standard in 1935& %ut man COBOL compilers still support its use. Code the paragraph name foll owed % an desired comments& such as the primar purpose of the program. or eample8
REMARKS. THIS IS THE MAIN SCREEN HANDLER FOR EMPLOYEE FILE MAINTENANCE. E"*#%O"ME"- D#*#O" The *#?I!O#@*#T :I?I6IO# is where all of the hardware to %e used % a program is assigned. It is divided into two sections8 the CO#IE>!"TIO# and I#>T+O>T>T 6*CTIO#s. The purpose of each of the sections follows8
CO"F#G+%'-#O" &EC-#O" This optional section is used to specif the tpe of computer e=uipment which will %e involved in eecuting the program. There are three paragraphs which are commonl used8
&O+%CE)COM!+-E% This optional paragraph specifies the tpe of computer that is used to compile the program. This paragraph is snta+chec)ed % some compilers& %ut most Dust treat it as a comment. or eample8
SOURCE-COMPUTER. IBM-PC.
OB=EC-)COM!+-E% This optional paragraph specifies the tpe of computer that is used to eecute the program. This paragraph is snta+chec)ed % some compilers& %ut most Dust treat it as a comment. or eample8
OBJECT-COMPUTER. IBM-PC.
&!EC#'L)"'ME& This optional paragraph ma %e used to associate a name with a variet of items& such as sstem functions& alpha%ets& sm%olic characters& classes& special currenc signs or decimal point character. or eample8
SPECIAL-NAMES. CLASS HEX-CHARACTER IS "0" THRU "9" "A" THRU "F".
#"!+-)O+-!+- &EC-#O" This section is primaril used to code the IL*+CO#T!OL paragraph.
F#LE)CO"-%OL This paragraph is used to associate the fil es to %e used in the program with specific I;O devices. or eample8 FILE-CONTROL. SELECT PRINTER-FILE ASSIGN TO SYS015-UR-1403-S.
In the eample& !I#T*!+IL* is the name of the file to %e used throughout the program. 6H615+>!+12+6 is a sstem name& which will %e used to lin) the
1
file to something in the operating sstem. The method of such connection varies from sstem to sstem. D'-' D#*#O" The :"T" :I?I6IO# is where all of the data used % a program is defined. It is divided into five sections8 the IL*& #IC"TIO# 6*CTIO#s& although most programs onl include the IL* and
F#LE &EC-#O" The IL* 6*CTIO# is used to define the files that will %e used in the program. There will %e on file description (:$ for each file followed % a record description for each different record format associated with the file. or eample8 FD FILE-NAME. 01 RECORD-NAME. 05 FIELD-NAME-1 PIC X(5). 05 FIELD-NAME-2 PIC 99V99. 05 FIELD-NAME-3 PIC X(20).
In the eample& the file is named IL*+#"@*+1. It is the same file name that was specified in the 6*L*CT statement for the file. The 1 following the : is called a record description entr. @ultiple 1 entries can %e included if there is more than one applica%le record format for the file. 6ee :ata Items for further information on the record description and its su%ordinate items.
(O%<#"G)&-O%'GE &EC-#O" The
L#"<'GE &EC-#O" The LI#Q"E* 6*CTIO# is used in a su%program to define data that will %e passed as arguments to the routine. The structure of the items is as descri%ed in the :ata Items section of this tutorial. *ach 1 level item in the LI#Q"E* 6*CTIO# should %e included in the !OC*:>!* :I?I6IO# >6I#E heading. or eample8 PROCEDURE DIVISION USING PARAMETER-1 PARAMETER-2 PARAMETER-3.
In this eample& the su%program would have 2 passed parameters called "!"@*T*!+1& "!"@*T*!+-& and "!"@*T*!+2. *ach of them would %e defined as an 1 level item in the LI#Q"E* 6*CTIO#. The order that the items are defined in the LI#Q"E* 6*CTIO# is unimportant.
%E!O%- &EC-#O" The !*O!T 6*CTIO# can %e used to produce standard control %rea) driven reports. It is not commonl used.
COMM+"#C'-#O" &EC-#O" The CO@@>#IC"TIO# 6*CTIO# is used for communicating %etween two programs running simultaneousl on a computer which supports such operations. It is not commonl used.
11
!%OCED+%E D#*#O" The !OC*:>!* :I?I6IO# consists of a series of procedures called paragraphs& each designed to perform a specific function. " paragraph consists of a paragraph name coded in "rea " and a series of procedural statements designed to perform a desired function coded i n "rea B. rocedures fre=uentl eecute other procedures as indicated in the following eampl e8 0000-MAIN-DRIVER. PERFORM 1000-INITIALIZATION-ROUTINE. PERFORM 2000-MAIN-PROCESSING UNTIL END-OF-FILE. PERFORM 3000-FINALIZE-ROUTINE. STOP RUN.
#otice the use of punctuation in the preceding paragraph. *ach statement should %e a sentence& that is& it should end with a period unless it is part of a conditional sentence. 6entences which are continued across multiple lines should %e indented on the succeeding lines. hrases which modif the %ehavior of a statement such as >#TIL in the *!O!@ statement should %e coded on a new line so that the stand out. or more tips on ma)ing our code eas to follow& see 6tle.
#DE"-#F#C'-#O" D#*#O" • • • • • • •
">TO! :"T*+CO@IL*: :"T*+!ITH
E"*#%O"ME"- D#*#O" • • • • • • • • • • •
"L"B*T CL"66 CO#IE>!"TIO# 6*CTIO# C>!!*#CH :*CI@"L+OI#T IL*+CO#T!OL I#>T+O>T>T 6*CTIO# OBP*CT+CO@>T*! 6*L*CT 6O>!C*+CO@>T*! 6*CI"L+#"@*6 and 6H@BOLIC.
D'-' D#*#O" •
C: (Communication :escription$ 1-
• • • • • • • • • • • • • •
CO@@>#IC"TIO# 6*CTIO# Condition #ame :ata :escription : (ile :escription$ IL* 6*CTIO# LI#Q"E* 6*CTIO# !ecord :escription !: (!eport :escription$ !eference @odification 6C!**# 6*CTIO# 6: (6ort :escription$ 6u%scripting Ta%le andling
!OC*:>!* :I?I6IO# • • • • • • • • • • • • • • • • • • • • • • •
'CCE!'DD 'L-E% C'LL C'"CEL CLO&E COM!+-E CO"-#"+E DELE-E D#&!L'Y D#*#DE E"-E% E*'L+'-E E>#GE"E%'-E GO GOB'C< #F #"#-#'L#$E #"#-#'-E #"&!ECME%GE MO*E
• • • • • • • • • • • • • • • • • • • •
M+L-#!LY O!E" !E%FO%M %E'D %ECE#*E %ELE'&E %E-+%" %E(%#-E &E'%CH &E"D &E&O%&-'%&-O! &-%#"G &+B-%'C-E%M#"'-E +"&-%#"G +&E (%#-E
12
#DE"-#F#C'-#O" D#*#O" • • • • • • •
">TO! :"T*+CO@IL*: :"T*+!ITH
The ">TO! aragraph &ynta9
Format AUTHOR. comment+entry.
Description The ">TO! paragraph is used to document the name of a writer of a COBOL programmer. The actual snta of the author name is not chec)ed. ">TO! is in "rea " followed % a period and the comment entr is in "rea B followed % a period. -ips
1.
This paragraph has %een mar)ed for removal from the COBOL language& even though it does provide useful information a%out the original programmer to the maintenance programmer.
-.
Hou can include this paragraph in a program to %e compiled on a compiler from which the paragraph has %een removed % including A's in column 4.
The :"T*+CO@IL*: aragraph &ynta9
Format DATE-COMPILED. comment+entry.
Description The :"T*+CO@IL*: paragraph is used to document the last time that the program was compiled. :"T*+CO@IL*: is in "rea " followed % a period. The actual date will %e supplied % the compiler at compile time& so no additional entr is necessar. -ips
1.
This paragraph has %een mar)ed for removal from the COBOL language& %ecause the information is usuall provided in the heading on the listing produced % the compiler.
-.
Hou can include this paragraph in a program to %e compiled on a compiler from which the paragraph has %een removed % including A's in column 4.
The :"T*+
Format DATE-WRITTEN. comment+entry.
Description The :"T*+
1.
This paragraph has %een mar)ed for removal from the COBOL language& even though it does provide useful information a%out the original time frame to the maintenance programmer.
-.
Hou can include this paragraph in a program to %e compiled on a compiler from which the paragraph has %een removed % including A's in column 4.
The I#6T"LL"TIO# aragraph &ynta9
Format INSTALLATION. comment+entry.
Description The I#6T"LL"TIO# paragraph is used to document the name of the installation for whom the COBOL program was written. The actual snta of the installation is not chec)ed. I#6T"LL"TIO# is in "rea " followed % a period and the comment entr is in "rea B followed % a period.
-ips
1.
This paragraph has %een mar)ed for removal from the COBOL language& even though it does provide useful information a%out the original compan to the maintenance programmer.
-.
Hou can include this paragraph in a program to %e compiled on a compiler from which the paragraph has %een removed % including A's in column 4.
The !OE!"@+I: aragraph &ynta9
Format PROGRAM-ID. program+name.
Description The !OE!"@+I: paragraph is used to document the name of the COBOL program. The program name must %e a valid identifier. !OE!"@+I: is in "rea " followed % a period and the program name is in "rea B followed % a period. -ips
1.
This is the onl paragraph that is re=uired in the I:*#TIIC"TIO# :I?I6IO#.
-.
"lthough the program name must follow the rules for identifiers& the name provided is fre=uentl used % the operating sstem& so there ma %e additional restrictions on the name depending on the computer sstem involved.
The !*@"!Q6 aragraph &ynta9
Format REMARKS. comment+entry.
Description The !*@"!Q6 paragraph is used to document the COBOL program. The actual snta of the remar)s is not chec)ed. !*@"!Q6 is in "rea " followed % a period and the comment entr is in "rea B followed % a period. -ips
1.
This paragraph tpicall contains two or three sentences descri%ing the function ; purpose of the program& although some installations go into great detail descri%ing files used& etc.
-.
This paragraph has %een mar)ed for removal from the COBOL language& even though it does provide useful information a%out the programs purpose to the maintenance programmer.
2.
Hou can include this paragraph in a program to %e compiled on a compiler from which the paragraph has %een removed % including A's in column 4.
-e &EC+%#-Y !aragrap &ynta9
Format SECURITY. comment+entry.
Description The 6*C>!ITH paragraph is used to document the l evel of securit assigned to the CO BOL program. The actual snta of the securit is not chec)ed. 6*C>!ITH is in "rea " followed % a period and the comment entr is in "rea B followed % a period. -ips
-.
This paragraph has %een mar)ed for removal from the COBOL language& even though it does provide useful information a%out the securit level to the maintenance programmer.
2.
Hou can include this paragraph in a program to %e compiled on a compiler from which the paragraph has %een removed % including A's in column 4.
!%OCED+%E D#*#O" • • • • • • • • • • • • • • •
'CCE!'DD 'L-E% C'LL C'"CEL CLO&E COM!+-E CO"-#"+E DELE-E D#&!L'Y D#*#DE E"-E% E*'L+'-E E>#GE"E%'-E
• • • • • • • • • • • • • • •
GO GOB'C< #F #"#-#'L#$E #"#-#'-E #"&!ECME%GE MO*E M+L-#!LY O!E" !E%FO%M %E'D %ECE#*E %ELE'&E %E-+%"
• • • • • • • • • • • • •
%E(%#-E &E'%CH &E"D &E&O%&-'%&-O! &-%#"G &+B-%'C-E%M#"'-E +"&-%#"G +&E (%#-E
-e 'CCE!- &tatement &ynta9
Format 1 ACCEPT identifier+ RFROM
U S mnemonic+name+ en)ironment+name
Format ? ACCEPT identifier+ FROM
S
DATE DAY DAY-OF-WEEK TIME
Description The "CC*T statement is used to ac=uire data from the primar input device or various sstem fields. *ach of the two formats wor)s slightl differentl and is descri%ed in the correspondingl num%ered area.
1.
The first format is used to accept data from the primar input or other specified device. The field(s$ listed after "CC*T are loaded
-.
The second format is used to accept one of various sstem value(s$. *ach of the items to %e accept has a different format. The are8 D'-E
HH@@:: + Two+digit ear of centur followed % two+digit month of ear followed % two+digit da of month. &'regorian -ate( D'Y HH::: + Two+digit ear of centur followed % three+digit da of ear. &ulian -ate( D'Y)OF)(EE< : + One+digit da of wee)& where @onda is 1& Tuesda is -& etc. -#ME @@66 + Two+digit hour of da followed % two digit minute of hour followed % two+digit second of minute followed % two digit hundredths of a second. &$wenty+four hour clock( -ips
1.
>se the "CC*T statement to ac=uire small amounts of data such as parameters to %e entered at runtime.
-.
The chief advantage of ac=uiring data with the "CC*T statement is that no file needs to %e esta%lished to use it.
2.
The chief disadvantage of ac=uiring data with the "CC*T statement is that there is no mechanism to recogni/e end of file.
.
@an COBOLs have added enhancements to allow access to a four+digit ear due to the H-Q pro%lem. Chec) our sstem manual for details.
-e 'DD &tatement &ynta9
Format 1 ADD
S
identifier+ literal+
... TO Sidentifier+/ [ROUNDED]} ...
[ON SIZE ERROR imperati)e+statement+U [NOT ON SIZE ERROR imperati)e+statement+/U [END-ADD]
Format ? ADD
S identifier+ literal+
... TO
S identifier+/ literal+/
GIVING {identifier+0 [ROUNDED]}...
[ON SIZE ERROR imperati)e+statement+U [NOT ON SIZE ERROR imperati)e+statement+/U [END-ADD]
Format @ ADD
S
CORRESPONDING CORR
identifier+
TO identifier+/ [ROUNDED]
[ON SIZE ERROR imperati)e+statement+U [NOT ON SIZE ERROR imperati)e+statement+/U [END-ADD]
Description The ":: statement is used to add numeric items together. *ach of the three formats wor)s slightl differentl and is descri%ed in the correspondingl num%ered area.
1.
The first format is used to add field(s$ or value(s$ to another field. The field(s$ or value(s$ listed %etween ":: and TO are summed and added to the value of the field(s$ following the TO& where the answer is stored. or eample& in ":: " TO B& the value in " is added to the value i n B and the result is stored in B.
The value in " is unchanged. urthermore& in ":: C : TO * & the sum of the values in C and : is added to the value in *& storing the answer in * and the sum of C and : is also added to storing the value in . The values in C and : are unchanged. -.
The second format is used to add field(s$ or value(s$ to one another& storing the answer in a different field. The field(s$ or value(s$ listed %etween ":: and EI?I#E are added and stored in the field following the field(s$ following the EI?I#E. or eample& in ":: " TO B EI?I#E C& the value in " is added to the value in B and the result is stored in C. The values in " and B are unchanged.
2.
The third format is used to add su%ordinate field(s$ of one group to su%ordinate field(s$ in another& storing the answer in those fields. Those su%ordinate field(s$ of the group item identifier+1 are added to and stored in those with eactl the same name in the group item identifier+-. The names of the su%ordinate items must %e spelled eactl the same wa in %oth groups to =ualif to participate in the addition.
-ips
1.
The fields to %e added must have numeric pictures& that is& the can onl have the characters 9& 6& and ? in their IC clauses.
-.
!eceiving fields ma %e either numeric or numeric edited fields.
2.
>se the 6I* *!!O! clause to detect field overflow on the receiving field.
.
In all formats& the mathematicall correct results are computed& %ut if the receiving field is too short in either the integer or decimal portion&the result will %e truncated& the integer on the left and the decimal on the right. Including the !O>#:*: phrase will result the answer field %eing rounded instead of truncating. !ounding is alwas done in the least significant portion of the answer.
-e 'L-E% &tatement &ynta9
Format ALTER { procedure-name-1 TO [PROCEED TO] procedure-name-2} ...
Description The "LT*! statement is used to modif the %ranching %ehavior of a EO TO statement. In order to use the "LT*! statement ou must have a paragraph in the !OC*:>!* :I?I6IO# that consists of eactl one EO TO statement. The EO TO must %e the onl statement in the paragraph. rocedure+name+1 is the name that paragraph and procedure+name+- is the name of an other paragraph in the !OC*:>!* : I?I6IO#. If the EO TO in procedure+name is su%se=uentl eecuted& control will pass to procedure+name+- instead of the name originall coded in the EO TO statement. -ips
1.
This statement was eliminated from the COBOL language and it was onl included in this manual in case ou encounter a previousl coded program that uses this statement.
-.
>se of this statement is considered to %e a ver %ad idea.
2.
If in spite of tips 1 and -& ou still elect to code an "LT*! statement& please don't address our pro%lems to the "s) The *pert section of this we%site.
-e C'LL &tatement &ynta9
Format 1 CALL
S identifier+ literal+ BY REFERENCE
R
USING
S S S
BY CONTENT
identifier+/ literal+/
S S
identifier+/ ADDRESS OF identifier+0
file+name+
RLENGTH OFU identifier+/ ADDRESS OF identifier+0 literal+/
U ...
...
...
...
RON OVERFLOW imperati)e+statement+U REND-CALLU
Format ? CALL
S
identifier+ literal+
BY REFERENCE
R
USING
S S S
BY CONTENT
identifier+/ literal+/
S S
identifier+/ ADDRESS OF identifier+0
file+name+
RLENGTH OFU identifier+/ ADDRESS OF identifier+0 literal+/
U ...
...
...
...
RON EXCEPTION imperati)e+statement+U RNOT ON EXCEPTION imperati)e+statement+/U REND-CALLU Description The C"LL statement is used to call other programs. 6I#E represents a parameter which is to %e passed to the called su%program. BH !**!*#C* means the actual item will %e passed and the item ma %e modified % the su%program. BH CO#T*#T means that onl the value of the item will %e passed and the item ma not %e modified % the su%program. If neither option is specified& the items are passed as % reference parameters.
1.
The first format is used to chec) for overflow errors. "n overflow occurs when there is not enough memor availa%le to hold the called su%program. If such an error occurs& the code %etween O# O?*!LO< and *#:+C"LL will %e eecuted. If no error occurs& control will pass to t he statement following the *#:+C"LL.
-.
The second format is used to chec) for the occurrence of an error. If such an error occurs& the code %etween O# *C*TIO# and #OT O# *C*TIO# or *#:+C"LL will %e eecuted. If no error occurs& code %etween #OT O# *C*TIO# and the *#:+C"LL will %e eecuted.
-ips
1.
The C"LL statement is used to eecute code outside of our program. To eecute code within our program& use the *!O!@ statement.
-e !E%FO%M &tatement &ynta9
Format 1
RS
PERFORM R procedure+name+
THROUGH THRU
procedure+name+/ U U
Rimperati)e+statement+ END-PERFORMU
Format ?
RS
PERFORM R procedure+name+
S Identifier+ integer+
THROUGH THRU
procedure+name+/ U U
TIMES Rimperati)e+statement+ END-PERFORMU
Format @ PERFORM R procedure+name+
R
WITH TEST
S
BEFORE AFTER
RS U
THROUGH THRU
procedure+name+/ U U
UNTIL condition+
Rimperati)e+statement+ END-PERFORMU
Format 2 PERFORM R procedure+name+
R
WITH TEST
VARYING
S
S
BEFORE AFTER
identifier+/ inde#+name+
RS U
THROUGH THRU
FROM
S
procedure+name+/ U U
identifier+0 inde#+name+/ literal+
BY
S identifier+1 literal+/
UNTIL condition+/ [AFTER
S
identifier+1 inde#+name+0
FROM
S
identifier+2 inde#+name+1 literal+0
BY
S identifier+2 literal+/
UNTIL condition+0U ...
Rimperati)e+statement+ END-PERFORMU
Description The *!O!@ statement is used to eecute code. There are two different modes of operation.
The first mode is used to eecute code in another area of the program and is invo)ed % mentioning a procedure or paragraph name. In this mode& the code in the other paragraph(s$ is eecuted and control is returned to the statement following the *!O!@ statement.
The second mode is called an in+line *!O!@& %ecause the code to %e eecuted is actuall coded in place inside of the *!O!@ statement. In the second mode& the code which is eecuted is that which lies %etween the *!O!@ and the *#:+*!O!@. *ach of the four formats wor)s slightl differentl and is descri%ed in the correspondingl num%ered area. 1.
The first format is used to eecute the referenced code one time.
-.
The second format is used to eecute the referenced code multiple times. The actual num%er of times is indicated % identifier+1 or integer+1.
2.
The third format is also used to eecute the referenced multiple times. The actual num%er of times is dependent upon condition+1. The code will %e repeatedl eecuted until condition+1 is true. The condition will %e tested %efore the code is actuall tested unless the
.
The fourth format is used to eecute the referenced multiple times with the actual num%er of times %eing dependent upon a condition li)e the third format. The primar difference %etween format 2 and format is the ?"!HI#E phrase& which is used to count in a varia%le during the repetitions. The ?"!HI#E varia%le is set to the !O@ value %efore the referenced code is eecuted. Before each su%se=uent pass& the ?"!HI#E varia%le is incremented % the amount of the BH value. The optional "T*! phrase ma %e included to var the value of an additional varia%le. If this option is used& "T*! varia%le will %e incremented through its entire ccle for each time the ?"!HI#E varia%le is ccled.
-ips
1.
>se the *!O!@ statement to access code which needs to %e eecuted from more than one location.
-.
>se the *!O!@ statement to structure our code. @a)e a main routine that *!O!@s other routines which in turn *!O!@ other routines which themselves *!O!@ other routines until the level of compleit of the *!O!@ed routines is easil comprehended.
2.
>se the
.
>se the ?"!HI#E ... "T*! option to pass through all the entries in multiple dimensional ta%les.
5.
If the num%er of lines in the routine is relativel small& use an in+line *!O!@ instead of *!O!@ing a separate paragraph. The net programmer will appreciate not having to loo) all through the program to find the one or two lines of code in the paragraph ou *!O!@ed
-e C'"CEL &tatement &ynta9
Format CANCEL
S identifier+ literal+
Description The C"#C*L statement is used to remove su%routines from memor when the are no l onger needed. The su%routine to %e cancelled is determined % the operand. "ll memor allotted to the given program is relin=uished. -ips
1.
The C"#C*L C"#C*L statemen statementt can %e used used to reset reset all the varia%les varia%les in a su%rou su%routine tine %etween %etween C"LLs C"LLs of the routineG however& this is inefficient in that it re=uires the program to %e loaded %ac) into memor %efore eecution can occur. It is %etter to initi ali/e such items at the %eginning of the !OC*:>!* :I?I6IO# instead.
-.
>se the C"#C*L to free free up memor memor when there isn't enough enough availa% availa%le le to load another another su%program.
-e CLO&E &tatement &ynta9
Format
CLOSE
S
file+name+
R
S
REEL UNIT
WITH
R S
WITH NO REWIND FOR REMOVAL NO REWIND LOCK
U
U
...
Description The CLO6* statement is used to terminate processing on a file. "ll files should %e closed %efore eiting a program. The optional clauses perform the following functions8 •
•
•
•
!**L and >#IT are interchangea%le and are onl useful for multiunit files& such as multi+reel tape files. Closing the reel doesn't actuall close the file& Dust the current volume. O! !*@O?"L !*@O?"L is onl useful for files residing on remova%le media. Closing for removal wil l cause the volume to %e dismounted. IT #O !*
1.
If there there is a file file status status associate associated d with the file& file& it will %e %e set after after the CLO6* CLO6* is eecuted eecuted to indicate the result of the CLO6*. " value of means successful.
-.
If ou ou omit the CLO6* CLO6* statement& statement& files files will %e %e closed closed automaticall automaticall when ou ou eit the program& %ut this considered %ad form.
2.
" closed closed file file must must %e re+open re+opened ed if it it is necess necessar ar to access access it again. again.
.
If a file file is not neede needed d for part part of the duration duration of a progra program& m& closing closing it will free free the sste sstem m resources it occupies.
-e COM!+-E &tatement
&ynta9
Format COMPUTE Sidentifier+/ [ROUNDED]} ...
S
= EQUAL
arithmetic+e#pression
[ON SIZE ERROR imperati)e+statement+U [NOT ON SIZE ERROR imperati)e+statement+/U [END-COMPUTE]
Description The CO@>T* statement is used to evaluate arithmetic formulas. The formula is evaluated and the result is stored in the identifier(s$ %etween CO@>T* and N.
The format for the formula is a standard alge%raic epression with addition& su%traction& multiplication& and division represented as J& +& A& and ;. The eponentiation operator is AA.
1.
If onl one one mathematica mathematicall operation operation is to %e perfor performed& med& use the stateme statement nt that performs performs that that operation ( ":: "::&& 6>BT!"CT 6>BT!"CT&& @>LTILH @>LTILH&& or :I?I:* or :I?I:*$. $. !eserve the CO@>T* statement for formulas or eponentiation& since it has no separate statement associated with it.
-.
!eceiving !eceiving fields fields ma %e %e either either numeri numericc or numeric numeric edited edited fields. fields.
2.
>se the the 6I* 6I* *!!O! clause clause to detect detect field field overflow overflow on the receiv receiving ing field. field.
.
In all formats& formats& the mathemati mathematicall call correc correctt results are are computed& computed& %ut if the the receiving receiving field field is too short in either the integer or decimal portion&the result will %e truncated& the integer on the left and the decimal on the right. Including the !O>#:*: phrase will result the answer field %eing rounded instead of truncating. !ounding is alwas done in the least significant portion of the answer.
-e CO"-#"+E &tatement &ynta9
Format CONTINUE
Description The CO#TI#>* statement is used as a place holder in conditional code. It has no effect on the eecution of the program. -ips
1.
>se the CO#TI#>* CO#TI#>* stateme statement nt in conditional conditional code code when no action action is re=uir re=uired ed for that that particular particular condition.
-.
It is not not necess necessar ar to code code a CO#TI CO#TI#>* #>* statem statement ent in an an *L6* when no action is re=uired for that t hat particular condition. If no action is re=uired for an *L6*& do not code the *L6*& simpl code the *#:+I.
-e DELE-E &tatement &ynta9
Format DELETE file+name+ RECORD [INVALID KEY imperati)e+statement+U [NOT INVALID KEY imperati)e+statement+/U [END-DELETE]
Description The :*L*T* statement is used to delete records from indeed and relative files. (!ecords cannot %e deleted from se=uential files.$ The sstem will delete the record associated with the record )e for indeed files or the relative )e for relative files. If such a record does not eist prior to the :*L*T* %eing eecuted& the I#?"LI: I#?"LI: Q*H imperative statement& if an& will %e eecuted. If the record actuall did eist& the #OT I#?"LI: I#?"LI: Q*H imperative statement& if an& will %e eecuted. -ips
1.
6et the recor record d )e or relativ relativee )e to the the record record to %e set set prior prior to the eecut eecution ion of the the :*L*T* :*L*T* statement.
-.
It isn't isn't necessa necessar r to actuall actuall read the record record prior to deleting deleting it. it.
-e D#&!L'Y &tatement &ynta9
Format 1 DISPLAY identifier+ RUPON
UR S mnemonic+name+ en)ironment+name
WITH NO ADVANCINGU
Description The :I6L"H statement is used to ehi%it ehi%i t data upon the primar output device. The field(s$ listed after :I6L"H are ehi%ited. -ips
1.
>se the :I6L" :I6L"H H stateme statement nt to ehi%it ehi%it small amounts amounts of data data such as error error messag messages. es.
-.
The chief chief advantage advantage of ehi%itin ehi%iting g data with the the :I6L"H :I6L"H statement statement is that that no file needs needs to %e esta%lished to use it.
2.
The chief chief disadvantag disadvantagee of ehi%iting ehi%iting data with with the :I6L" :I6L"H H statement statement is that that there there is no mechanism to suppl carriage control.
-e D#*#DE &tatement &ynta9
Format 1 DIVIDE
S identifier+ ... literal+
INTO Sidentifier+/ RROUNDEDU ...
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-DIVIDEU
Format ? DIVIDE
S identifier+ literal+
INTO
S identifier+/ literal+/
GIVING Sidentifier+0 RROUNDEDU...
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-DIVIDEU
Format @ DIVIDE
S identifier+ S identifier+/ literal+ literal+/ BY
GIVING Sidentifier+0 RROUNDEDU...
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-DIVIDEU
Format 2 DIVIDE
S
identifier+ literal+
INTO
S
identifier+/ literal+/
GIVING identifier+0 RROUNDEDU REMAINDER
identifier+1
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-DIVIDEU
Format A DIVIDE
S
identifier+ literal+
S BY
identifier+/ literal+/
GIVING identifier+0 RROUNDEDU REMAINDER
identifier+1
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-DIVIDEU Description The :I?I:* statement is used to divide numeric items together. Both formats wor) slightl differentl and each is descri%ed in the correspondingl num%ered area.
1.
The first format is used to divide a field(s$ or value(s$ % another field or value. The field or value listed %etween :I?I:* and I#TO is divided into the value of the field(s$ following the I#TO. The answer is stored if the individual field. or eample& in :I?I:* " I#TO B& the value in " is divided into the value in B and the result is stored in B. The value in " is unchanged. urthermore& in :I?I:* C I#TO : *& the value in C is divided into the value in :& storing the answer in :G the value of C is also divided into *& storing the value in *. The value in C is unchanged.
-.
The second format is used to divide field(s$ or value(s$ % one another&storing the answer in a different field. The field(s$ or value(s$ listed %etween :I?I:* and EI?I#E are divided and stored in the field following the field(s$ following the EI?I#E. or eample& in :I?I:* " I#TO B EI?I#E C& the value in " is divided into the value in B and the result is stored in C. The values in " and B are unchanged.
2.
The third format is also used to divide field(s$ or value(s$ % one another& storing the answer in a different field. The field(s$ or value(s$ listed %etween :I?I:* and EI?I#E are divided and stored in the field following the field(s$ following the EI?I#E. or eample& in :I?I:* " BH B EI?I#E C& the value in " is divided % the value in B and the result is stored in C. The values in " and B are unchanged.
.
The fourt% format is used to divide field(s$ or value(s$ % one another& storing the answer in a different field. The field(s$ or value(s$ listed %etween :I?I:* and EI?I#E are divided and stored in the field following the field(s$ following the EI?I#E. or eample& in :I?I:* " I#TO B EI?I#E C !*@"I#:*! :& the value in " is divided into the value in B and the resultis stored in C with the remainder %eing stored in :. The values in " and B are unchanged.
5.
The fifth format is also used % divide field(s$ or value(s$ % one another& storing the answer in a different field. The field(s$ or value(s$ listed %etween :I?I:* and EI?I#E are divided and stored in the field following the field(s$ following the EI?I#E. or eample& in :I?I:* " BH B EI?I#E C !*@"I#:*! :& the value in " is divided % the value in B and the result is stored in C with the remainder %eing stored in :. The values in " and B are unchanged.
-ips
1.
The fields to %e divided must have numeric pictures& that is& the can onl have the characters 9& 6& and ? in their IC clauses.
-.
!eceiving fields ma %e either numeric or numeric edited fields.
2.
>se the 6I* *!!O! clause to detect field overflow on the receiving field.
.
In all formats& the mathematicall correct results are computed& %ut if the receiving field is too short in either the integer or decimal portion&the result will %e truncated& the integer on the left and the decimal on the right. Including the !O>#:*: phrase will result the answer field %eing rounded instead of truncating. !ounding is alwas done in the least significant portion of the answer
-e E*'L+'-E &tatement &ynta9
Format
EVALUATE
S
identifier+ literal+ arith+e#pr+ condition+ TRUE FALSE
R S ALSO
identifier+/ literal+/ arith+e#pr+/ condition+/ TRUE FALSE
U
...
ANY
condition+0
SSWHEN
S
TRUE FALSE [NOT]
SS
identifier+0 literal+0 arith+e#pr+0
R S
THROUGH THRU
S
identifier+1 literal+1 arith+e#pr+1
U
ANY
condition+1
RALSO
S
TRUE FALSE
[NOT]
SS
identifier+ 2 literal+2 arith+ e#pr+2
imperati)e+statement+...
R<*# OT*! imperati)e+statement+/U
R S
THROUGH THRU
S
identifier+ 3 literal+3 arith+ e#pr+3
U
U ..... .
R*#:+*?"L>"T*U Description The *?"L>"T* statement is used to test a varia%le for multiple different values. "s such& it is an implementation of the standard case construct. The "L6O clause is used t o test for values of additional varia%les as the same time. To eecute the same code for multiple conditions& code all the applica%le conditions on multiple <*# clauses. "T* is compared to the each of the values in the <*# clauses in turn. "T*. If no matching <*# clause is found& the code following the <*# OT*! statement is eecuted. If the "L6O clause is used& t hen the value following the "L6O in the *?"L>"T* must match the value in the "L6O associated with the <*# clause. or eample8 EVALUATE FILE-STATUS WHEN "00" PERFORM SUCCESSFUL-ROUTINE WHEN "10" PERFORM END-OF-FILE-ROUTINE WHEN OTHER PERFORM IO-ERROR-ROUTINE END-EVALUATE.
In this eample& if the file status has a value of & 6>CC*66>L+!O>TI#* will %e performed& %ut if the file status has a value of 1& the *#:+O+IL*+!O>TI#* will %e eecuted. If neither value is in file status& then IO+*!!O!+!O>TI#* will %e eecuted. ere is the same test using condition names instead of relational tests8 EVALUATE TRUE WHEN I-O-OK PERFORM SUCCESSFUL-ROUTINE WHEN END-OF-FILE PERFORM END-OF-FILE-ROUTINE WHEN OTHER PERFORM IO-ERROR-ROUTINE END-EVALUATE.
-ips
1.
>se multiple <*# clauses to implement code that wor)s li)e an O!.
-.
>se the "L6O clause to implement code that wor)s li)e an "#:.
2.
>se "#H if the value of one of the varia%les is not needed in one of the conditions.
-e E>#- &tatement &ynta9
Format 1 EXIT
Format ? EXIT PROGRAM
Description The *IT statement is used to eit routines or programs. *ach of the t wo formats wor)s slightl differentl and is descri%ed in the correspondingl num%ered area.
1.
If the first format is used& it must %e the onl entr in a paragraph. The *IT statement itself does nothing. The same effect can %e accomplished % leaving the paragraph empt.
-.
The second format is used to eit a su%program. If the statement is eecuted from within a called program& control is returned to the calling program. If the statement is eecuted from within a main program& it has the same effect as the *IT statement& that is& it does nothing.
-ips
1.
:o not use the *IT statement.
-.
>se the *IT !OE!"@ statement onl in a called su%program.
2.
If our sstem supports the EOB"CQ statement& use it in place of the *IT !OE!"@ statement in a called su%program.
-e GO &tatement &ynta9
Format 1 GO TO procedure+name+
Format ? GO TO S procedure+name+... DEPENDING ON identifier+
Format @ GO TO.
Description The EO statement is used to transfer control to another part of the program. *ach of the three formats wor)s slightl differentl and is descri%ed in the correspondingl num%ered area.
1.
The first format unconditionall transfers control to the referenced procedure. There is no wa to determine from where the control was t ransferred and there is no wa to return to the transferring location.
-.
The second format unconditionall transfers control to one of the referenced procedures. The eact procedure control is transferred to depends on the value of the identifier. If the identifier has a value of one& control will %e transferred to the first procedure listedG if the value is two& control will %e transferred to the second procedure& and so on. If there is not a procedure name listed to correspond with the value of the identifier at run time& the EO TO statement will have no effect and control will pass to the net consecutive statement. There is no wa to determine from where the control was transferred and there is no wa to return to the transferring location.
2.
The third format is used in conDunction with the "LT*! statement. The actual location for the control transfer is determined at run time using the "LT*! statement. It must %e the onl statement in its respective paragraph. If no "LT*! statement affecting the EO TO has %een eecuted prior to eecuting a format 2 EO TO& the statement will have no effect. There is no wa to determine from where the control was transferred and there is no wa to return to the transferring location.
-ips
1.
:o not use the EO statement. " well+structured& eas to follow program is accomplished via the *!O!@ statement.
-.
The functionalit once reali/ed via the EO TO statement with the :**#:I#E O# phrase is %etter implemented with an *?"L>"T* statement.
2.
The third format of the EO statement should never %e used as it is mar)ed for deletion from the net release of the COBOL standard. It is also responsi%le for creating programs that are almost impossi%le to de%ug.
-e GOB'C< &tatement &ynta9
Format GOBACK
Description The EOB"CQ statement is used to eit programs. If the statement is eecuted from within a called program& it has the same effect as the *IT !OE!"@ statement& that is& control is returned to the calling program. If the statement is eecuted from within a main program& it has the same effect as the 6TO !># statement& that is& it returns control to the operating sstem. -ips
1.
The EOB"CQ statement is not part of standard COBOL& %ut it is availa%le in man versions. Chec) our sstem manual to verif its availa%ilit.
-.
>se the EOB"CQ statement in a called su%program instead of the *IT !OE!"@ and 6TO !># statements& if it is availa%le. It will allow the program to %e used as either a main program or a su%program.
-e #F &tatement &ynta9
Format IFcondition-1 THEN
S statement+
NEXT SENTENCE
RELSE
S statement+
NEXT SENTENCE
U
REND-IFU
Description The I statement is used to test various conditions. The *L6* clause is used to provide code to %e eecuted for the alternative condition.
1.
The I statement can %e used to test for various )inds of conditions& relational tests& class tests& sign tests& or condition name tests. 6ee conditions for more detail.
-.
The logical operators "#:& O!& and #OT ma %e used to com%ine conditions into comple conditions. If these operators are used& the order of precedence is from highest to lowest8 "#:& O!& and finall #OT& unless the order is overridden with the use of parentheses.
-e #"#-#'L#$E &tatement
&ynta9
Format INITIALIZE Sidentifier+ ...
RREPLACING {
S
ALPHABETIC ALPHANUMERIC NUMERIC ALPHANUMERIC-EDITED NUMERIC-EDITED
DATA BY
S identifier+/ ...U literal+
Description The I#ITI"LI* statement is used to initiali/e data items. :ata items are initiali/ed according to their tpe8 •
•
•
•
"lpha%etic and alphanumeric items are initiali/ed to spaces. #umeric items are initiali/ed to /eros. or group items& the su%ordinate elementar items are initiali/ed depending on their individual data tpes. Onl named items are initiali/ed. The !*L"CI#E phrase can %e used to initiali/e data items to different values other than the default 6"C*6 or *!O6. -ips
1.
>se the initiali/e statement instead of moving *!O6 or 6"C*6 to a group. That wa& each su%ordinate item will %e set to an appropriate value.
-.
#otice that unnamed and ILL*! items are not initiali/ed. This allows the varia%le portion of a detail line& for eample& to %e initiali/ed without the constant values inserted into the unnamed fields %eing affected.
-e #"#-#'-E &tatement &ynta9
Format INITIATE report+name+
Description The I#ITI"T* statement is used to prepare a report for processing. It sets all of the reports sum counters as well as its line and page counters to /ero. The I#ITI"T* statement is part of the COBOL !eport
1.
!eport+name+1 must %e the name of a report in the !*O!T 6*CTIO# of the :"T" :I?I6IO#.
-.
The file associated with the report must %e O*# prior to the eecution of the I#ITI"T* statement.
2.
>nfortunatel& man implementors have elected not to implement COBOL's !eport
-e #"&!EC- &tatement &ynta9
Format 1 INSPECT identifier+ TALLYING identifier+/ FOR CHARACTERS
SS
ALL LEADING
RS
BEFORE AFTER
SS
identifier+ 1 literal+/
INITIAL
S
RS
BEFORE AFTER
RS
BEFORE AFTER
identifier+ 0 literal+ INITIAL
U ... S
identifier+ 2 literal+0
U ... ...
...
Format ? INSPECT identifier+3 REPLACING CHARACTERS BY
SS
ALL LEADING FIRST
S
identifier+* literal+1
S
identifier+ 5 literal+3
S BY
identifier+ 6 literal+*
INITIAL
S
RS
BEFORE AFTER
identifier+4 literal+2
INITIAL
U ... S
identifier+ literal+4
U ...
Format @ INSPECT identifier+/ TALLYING identifier+0 FOR CHARACTERS
SS
ALL LEADING
CHARACTERS BY
SS
ALL LEADING FIRST
RS
BEFORE AFTER
SS S
INITIAL
identifier+ 2 literal+6
RS
BEFORE AFTER
identifier+* literal+/
RS
BEFORE AFTER
S
identifier+ 5 literal+1
S BY
S
identifier+ 1 literal+5 INITIAL
identifier+ /6 literal+2
U ... S
identifier+ 3 literal+
U ... ...
INITIAL
S
identifier+4 literal+0
RS
BEFORE AFTER
INITIAL
Format 2 INSPECT identifier+// CONVERTING
S identifier+0 S identifier+/0 literal+ literal+* RS TO
BEFORE AFTER
INITIAL
S identifier+/1 U ... literal+4
Description The I#6*CT statement is used to perform various operations on string data. *ach of the four formats wor)s slightl differentl and is descri%ed in the correspondingl num%ered area.
1.
The first format is used to count occurrences of characters or strings within another string. The num%er of occurrences is stored in identifier+-. The value in identifier+1 is unchanged.
... REPLACING
U ... S
identifier+ / literal+3
U ...
-.
The second format is used to replace occurrences of characters or strings within another string. The replacement strings must %e of the same length as the original string.
2.
The third format is used to count while replacing occurrences of characters or strings within another string.. It is %asicall a com%ination of the first two formats.
.
The fourth format is used to replace occurrences of characters within another string using onl one I#6*CT. "n occurrence on the first character of the CO#?*!TI#E in the inspected string will %e replaced % the first character in the TO string& while occurrences of the second character in the CO#?*!TI#E string will %e replaced % the second character in the TO string& and so on. The CO#?*!TI#E string must %e of the same length as the to string.
-ips
1.
ormat - can %e used to convert leading spaces to /eros& in a numeric item& e.g. INSPECT LEADING-SPACE-ITEM REPLACING LEADING SPACES BY ZEROS.
-.
ormat can %e used to convert lowercase to uppercase& e.g. INSPECT MIXED-CASE-ITEM CONVERTING "abcde..." TO "ABCDE...".
-e ME%GE &tatement &ynta9
Format MERGE file+name+SON
S
ASCENDING DESCENDING
KEY Sdata+name+......
RCOLLATING SEQUENCE IS alpha"et+name+U USING file+name+/ S file+name+0...
S
ROUTPUT PROCEDURE IS procedure+name+ R
S
THROUGH THRU
RGIVING S file+name+1 ...
procedure+name+/U
Description The @*!E* statement is used to merge data files. The records must %e in the files to %e merged in the same order in which the are entered in the merged file& that is in se=uential order specified in the "6C*#:I#E;:*6C*#:I#E Q*H clause. Two options are availa%le for the output of t he operation8 •
•
If the EI?I#E phrase is specified& the records to %e merged are copied in appropriate "6C*#:I#E;:*6C*#:I#E Q*H order into the EI?I#E file. If the O>T>T !OC*:>!* phrase is specified& the records to %e merged are copied in t he appropriate "6C*#:I#E;:*6C*#:I#E Q*H order into the merge file. !ecords can %e o%tained from the file using the !*L*"6* statement. -ips
1.
"ll of the files referenced must %e closed when the @*!E* statement is eecuted.
-.
The )e fields must all %e defined in the record description(s$ for the merge file.
2.
The merge file cannot %e opened or closed.
.
If the EI?I#E option is used& the EI?I#E file must have the same record laout as the merge file.
-e MO*E &tatement &ynta9
Format 1 MOVE
S identifier+ literal+
TO Sidentifier+/ ...
Format ? MOVE
S
CORRESPONDING CORR
identifier+
TO Sidentifier+/ ...
Description The @O?* statement is used to cop data items to other data items. *ach of the two formats wor)s slightl differentl and is descri%ed in the correspondingl num%ered area.
1.
The first format is used to cop a field or value to another field(s$. The field or value listed %etween @O?* and TO is copied to the value of the field(s$ following the TO. or eample in @O?* " TO B& the value in " is copied t o B. The value in " is unchanged. urthermore& in @O?* C TO * & the sum or the values in C and : is moved to the value in *& storing the answer in * and the sum of C and : is also moved to storing the value in . The values in C and : are unchanged.
-.
The second format is used to cop su%ordinate field(s$ of one group to su%ordinate field(s$ in another. Those su%ordinate field(s$ of the group item identifier+1 are copied to and those with eactl the same name in the group item identifier+-. The names of the su%ordinate items must %e spelled eactl the same wa in %oth groups to =ualif to participate in the move.
-ips
1.
If the receiving field is edited& an editing re=uired will %e done at the time of the @O?*.
-.
If the receiving field is unedited and the sending field is edited& the field will %e de+edited at the time of the @O?*.
2.
If the sending field is shorter than the receiving field8
.
o
#umeric fields are padded on the left with /eros.
o
#on+numeric fields are padded on the right with spaces.
If the sending field is longer than the receiving field8 o
#umeric fields are truncated on the left.
o
#on+numeric fields are truncated on the right.
-e M+L-#!LY &tatement &ynta9
Format 1 MULTIPLY
S
identifier+ literal+
...
BY Sidentifier+/ RROUNDEDU ...
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U
REND-MULTIPLYU
Format ? MULTIPLY
S
identifier+ literal+
... BY
S
identifier+/ literal+/
GIVING Sidentifier+0 RROUNDEDU...
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-MULTIPLYU Description The @>LTILH statement is used to multipl numeric items together. Both formats wor) slightl differentl and each is descri%ed in the correspondingl num%ered area.
1.
The first format is used to multipl a field or value % another field or value. The product of the field(s$ or value(s$ listed %etween @>LTILH andBH is computed and multiplied % the value of the field(s$ following the BH. The answer is stored in the individual field. or eample& in @>LTILH " BH B& the value in " is multiplied % the value in B and the result is stored in B. The value in " is unchanged. urthermore& i n @>LTILH C BH : *& the value in C is multiplied % the value in :& storing the answer in : and the value of C is also multipli ed % *& storing the value in . The value in C is unchanged.
-.
The second format is used % multipl field(s$ or value(s$ % one another field or value& storing the answer in a different field. The field(s$ or value(s$ listed %etween @>LTILH and EI?I#E are multiplied and stored in the field(s$ following the EI?I#E. or eample& in @>LTILH " BH B EI?I#E C& the value in " is multiplied % the value in B and the result is stored in C. The values in " and B are unchanged.
-ips
1.
The fields to %e multiplied must have numeric pictures& that is& the can onl have the characters 9& 6& and ? in their IC clauses.
-.
!eceiving fields ma %e either numeric or numeric edited fields.
2.
>se the 6I* *!!O! clause to detect field overflow on the receiving field.
.
In %oth formats& the mathematicall correct results are computed& %ut if the receiving field is too short in either the integer or decimal portion&the result will %e truncated& the integer on the left and the decimal on the right. Including the !O>#:*: phrase will result the answer field %eing rounded instead of truncating. !ounding is alwas done in the least significant portion of the answer
-e O!E" &tatement &ynta9
Format OPEN
S
INPUT S file+name+ ... OUTPUT S file+name+/ ... I-O S file+name+0 ... EXTEND S file+name+1 ...
...
Description The O*# statement is used to prepare a file for processing. " file must %e open %efore an other operation is performed on it. I#>T means the records which eist in the file will %e made availa%le to the program. O>T>T means that records will %e created % the program and entered into the file. I+O means %oth input and output operations will %e performed. *T*#: means that records will %e created % the program and added to the end of an eisting file.
-ips
1.
If there is a file status associated with the file& it will %e set after the CLO6* is eecute to indicate the result of the CLO6*. " value of means successful.
-.
"n file mentioned must %e the su%Dect of a 6*L*CT statement and a file description (:$. iles opened for input& input;output& or etend should eist prior to eecuting the O*# statement.
2.
6I#E and EI?I#E file& if an& ma not %e open at the time the 6O!T statement is eecuted.
.
>se the CLO6* statement to terminate processing on the files when ou are through with them.
-e %E'D &tatement &ynta9
Format 1 READ file+name+
R
NEXT PREVIOUS
U
RECORD
RSV
WITH RNOU LOCK INTO identifier+
VU
RAT END imperati)e+statement+U RNOT AT END imperati)e+statement+/U REND-READU
Format ? READ file+name+ RECORD
RSV
WITH RNOU LOCK INTO identifier+
VU
[KEY IS Sdata+name+U
RINVALID KEY imperati)e+statement+U RNOT INVALID KEY imperati)e+statement+/U REND-READU Description The !*": statement is used to ac=uire a record from a file. The record read is loaded into the 1 (record description$ following the : (file description$ associated with the file. If the I#TO phrase is included& the record read will also %e copied into the I#TO data item.
The first format performs a se=uential read of the file. If the last operation performed on a file was an O*#& the first record of the file is read. If the previous operation was a !*":& the net se=uential record in the file is read. If the end of file is reached as a result of the read& no record is returned and the code %etween "T *#: and #OT "T *#:& if an& will %e eecuted. If the end of the file is not reached& the code %etween #OT "T *#: and *#:+!*":& if an& will %e eecuted.
-.
The second format performs a random read of the file. The record matching the current value of the file )e& if an& will %e read. If the file has m ultiple )es& the desired )e is specified in the Q*H I6 clause. If the file as multi ple )es and the Q*H I6 clause is omitted& the file's primar )e will %e used. If no matching record eists& no record is returned and t he code %etween I#?"LI: Q*H and #OT I#?"LI: Q*H& if an& will %e eecuted. If the a matching record is found& the code %etween #OT I#?"LI: Q*H and *#:+!*":& if an& will %e eecuted.
-ips
1.
If there is a file status associated with the file& it will %e set after the !*": is eecuted to indicate the result of the !*":. " value of means successful& 1 means t he end of file was encountered for the se=uential !*":& and -2 means there was no matching record for the )e supplied for the random !*":.
-.
"n file mentioned must %e the su%Dect of a 6*L*CT statement and a file description (:$. iles must %e open for input or input;output prior to eecuting the !*": statement.
2.
.
-e %ECE#*E &tatement &ynta9
Format RECEIVE cd-name-1
S @*66"E* 6*E@*#T
INTO identifier+
[NO DATA imperati)e+statement+U [WITH DATA imperati)e+statement+/U [END-RECEIVE]
DescriptionF The !*C*I?* statement is used to receive data through the COBOL communications facilit . The received data is moved into identifier+1. If no data is availa%le& the code %etween #O :"T" and
1.
The fields to %e received must %e defined in the :"T" :I?I6IO#.
-.
The communications facilit is fre=uentl not implemented. Be sure to chec) our individual manual to see if it is availa%le.
-e %ELE'&E &tatement &ynta9
Format RELEASE record-name-1
R
FROM
S identifier+ U literal+
Description The !*L*"6* statement is used within the contet of a 6O!T I#>T !OC*:>!* to release data records to the sort process. The released data is m oved into the sort file associated with record+name+1. If the !O@ clause is included& the !O@ item is copied into record+name+1 %efore the data record is released.
-ips
1.
The !*L*"6* statement is the e=uivalent of a
-.
"n I#>T !OC*:>!* must contain at least one !*L*"6* statement.
2.
The !*L*"6* statement ma onl %e used within the contet of an I#>T !OC*:>!*.
.
The !*T>!# statement is used to read 6O!T files.
-e %E-+%" &tatement &ynta9
Format RETURN file+name+ RECORD RINTO identifier+ U
RAT END imperati)e+statement+U RNOT AT END imperati)e+statement+/U REND-RETURNU Description The !*T>!# statement is used within the contet of a 6O!T O>T>T !OC*:>!* to o%tain data records from the sort process. The returned data is moved into the sort record associated with file+name+1. If the I#TO clause is included& the sort record is copied into the I#TO item after the data record has %een returned. If the end of the sort file is reached& the code %etween "T *#: and #OT "T *#:& if an& will %e eecuted. If the end of the sort file is reached& the code %etween #OT "T *#: and *#:+!*T>!#& if an& will %e eecuted. -ips
1.
The !*T>!# statement is the e=uivalent of a !*": statement& ecept that is used eclusivel for 6O!T files.
-.
"n O>T>T !OC*:>!* must contain at least one !*T>!# statement.
2.
The !*T>!# statement ma onl %e used within the contet of an O>T>T !OC*:>!*.
.
The !*L*"6* statement is used to write 6O!T files.
-e %E(%#-E &tatement &ynta9
Format 1 REWRITE record+name+
R
FROM
S identifier+ U literal+
Format ? REWRITE record+name+
R
FROM
S identifier+ U literal+
RINVALID KEY imperati)e+statement+U RNOT INVALID KEY imperati)e+statement+/U REND-REWRITEU
Description The !*
1.
The first format performs a se=uential rewrite of the file. The last record read is replaced % the contents of record+name+1.
-.
The second format performs a random rewrite of the file. The last record read& will %e rewritten. If the record )e has %een changed& no record is replaced and the code %etween I#?"LI: Q*H and #OT I#?"LI: Q*H& if an& will %e eecuted. If the rewrite is successful& the code %etween #OT I#?"LI: Q*H and *#:+!*
-ips
1.
If there is a file status associated with the file& it will %e set after the !*
-.
"n file mentioned must %e the su%Dect of a 6*L*CT statement and a file description (:$. iles must %e open for input;output and read prior to eecuting the !*
2.
.
Be sure to !*": the record ou wish to update and change the desired fields %efore eecuting the !*
-e &E'%CH &tatement &ynta9
Format 1 SEARCH identifier+
R
VARYING
S identifier+/ inde#+name+ U
[AT END imperati)e+statement+U
S
WHEN condition+
S
imperati)e+statement+/ NEXT SENTENCE
...
[END-SEARCH]
Format ? SEARCH ALL identifier+0 [AT END imperati)e+statement+0U
WHEN
S
R S AND
S
identifier+0
S
IS EQUAL TO IS =
S
identifier+1 literal+ arithmetic+ e#pression+
S
identifier+3 literal+/ arithmetic+ e#pression+/
condition+name+ identifier+2
S
condition+name+/
imperati)e+statement+ 1
IS EQUAL TO IS =
U
NEXT SENTENCE [END-SEARCH]
Description The 6*"!C statement is used to locate items in ta%les. ormat 1 performs a serial search and format performs a %inar search
Identifier+ must %e defined with an OCC>!6 clause including the I#:**: BH phrase. Identifier+0 must %e defined with an OCC>!6 clause including the I#:**: BH and "6C*#:I#E or :*6C*#:I#E Q*H phrases.
1.
-.
:ue to the overhead re=uired to implement a %inar search& it is recommended that a serial search %e used for ta%les with less than 1 entries.
2.
If the ta%le to %e searched contains more elements than are actuall in use& define the ta%le with the OCC>!6 :**#:I#E O# phrase to avoid including the unused ta%le elements in the 6*"!C process.
-e &E- &tatement &ynta9
Format 1 SET
S
inde#+name+ identifier+
...
TO
S
inde#+name+/ identifier+/ integer+
Format ? SET Sinde#+name+0 ...
S
UP DOWN
S BY
identifier+0 integer+/
Format @ SET
S
Smnemonic+name+ ... TO
Format 2
S
ON OFF
...
SET Scondition+name+ ... TO TRUE...
Description The 6*T statement is used to set values for indices and conditions. *ach of the four formats wor)s differentl and is descri%ed %elow in the correspondingl num%ered item.
1.
The first format of the 6*T statement is used to assign values to indices or assign the value of an inde to another varia%le. The varia%le(s$ %etween 6*T and TO is set to the value of the data item or literal following TO.
-.
The second format of the 6*T statement is used to increase or decrease the value of an inde. The varia%le(s$ %etween 6*T and > or :O<# is I#C!*"6*: (for >$ or :*C!*"6*: (for :O<#$ % the value of the data item or literal following BH.
2.
The third format of the 6*T statement is used to set the value of an eternal switch. The mnemonic(s$ %etween 6*T and TO is set to O# or O according to the O# or O option supplied.
.
The fourth format of the 6*T statement is used to set the condition+name conditions to true. The condition(s$ %etween 6*T and TO is set to T!>*.
-ips
1.
!egular @O?* and mathematical statements do not wor) on indices. >se the 6*T statement to manipulate an inde.
-.
or format & if more than one value is associated with the condition+name %eing true& the first value listed is the one that will %e used.
-e &O%- &tatement &ynta9
Format SORT file+name+ SON
S
ASCENDING DESCENDING
KEY Sdata+name+......
RWITH DUPLICATES IN ORDERU RCOLLATING SEQUENCE IS alpha"et+name+U
S S
RINPUT PROCEDURE IS procedure+name+ R
S
THROUGH THRU
procedure+name+/U
S
THROUGH THRU
procedure+name+1U
RUSING S file+name+/ ... ROUTPUT PROCEDURE IS procedure+name+0 R RGIVING S file+name+0 ...
Description The 6O!T statement is used to sort a data fil e. The )es to %e sorted on must %e in the sort file definition& that is in the record associated with the 6: (sort description$. Two options are availa%le for the input phase of the sort operation as well as two options for the output phase of the operation8 •
•
•
If the >6I#E phrase is specified& the records to %e sorted are copied from the >6I#E file into the 6O!T file %efore the data is sorted. If the I#>T !OC*:>!* phrase is specified& the records to %e sorted are entered into the sort file using the !*L*"6* statement. If the EI?I#E phrase is specified& the records to %e sorted are copied in appropriate "6C*#:I#E;:*6C*#:I#E Q*H order into the EI?I#E file.
If the O>T>T !OC*:>!* phrase is specified& the records to %e sorted are copied i n the appropriate "6C*#:I#E;:*6C*#:I#E Q*H order into the sort file. !ecords can %e o%tained from the file using the !*T>!# statement.
•
-ips
1.
"ll of the files referenced must %e closed when the 6O!T statement is eecuted.
-.
The )e fields must all %e defined in the record description(s$ for the sort file.
2.
The sort file cannot %e opened or closed.
.
If the >6I#E option is used& the >6I#E file must have the same record laout as the sort file.
5.
If the EI?I#E option is used& the EI?I#E file must have the same record laout as the sort file.
-e &-O! &tatement &ynta9
Format STOP
RUN
S literal+
Description The 6TO statement is used to stop programs. 6TO !># terminates t he program. 6TO literal displas the literal and waits for a response %efore continuing with the net eecuta%le statement. -ips
1.
>se the 6TO !># statement to terminate eecution of a program.
-.
:o not use the 6TO !># statement in a called su%program& as it will cause the termination of the entire run unit. Instead& to terminate eecution of a su%program& use the *IT !OE!"@ statement.
-e &-%#"G &tatement &ynta9
Format STRING
SS
identifier+ literal+
... :*LI@IT*: BH
S
identifier+/ literal+/
...
INTO identifier+0 [WITH POINTER identifier+1 U [ON OVERFLOW imperati)e+statement+U [NOT ON OVERFLOW imperati)e+statement+/U [END-STRING]
Description The 6T!I#E statement is used to concatenate non+numeric items together. "n num%er of items can %e concatenated. *ntire or partial strings ma %e concatenated. To use the entire string& delimit it % si/e. To use onl a portion of a string& delimit it % whatever character indicates the end of the data ou want to concatenate. The OI#T*! clause can %e used to start in a position other than 1. or eample& if the OI#T*! varia%le has a value of 2& character 1 and - of the receiving item will remain unchanged& and the stringing operation
will proceed from the third character of the receiving field. "s each character is inserted into the receiving field& the value of the OI#T*! varia%le will %e incremented& so that the OI#T*! varia%le ma %e used to 6T!I#E in more values where the previous 6T!I#E operation left off. Onl as much of the value in the receiving field will %e changed as is necessar to hold the newl assem%led string& for eample& in the statement
STRING A B DELIMITED BY SIZE INTO C.
If " is a one character string with a value of " and B is a one character string with a value of B and C is a five character string with an original value of CCCCC& after the 6T!I#E statement is eecuted& C will have a value of "BCCC.
-e &+B-%'C- &tatement &ynta9
Format 1 SUBTRACT
S identifier+ ... literal+
FROM Sidentifier+/ RROUNDEDU ...
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-SUBTRACTU
Format ? SUBTRACT
S identifier+ ... literal+
FROM
S identifier+/ literal+/
GIVING identifier+0 RROUNDEDU
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-SUBTRACTU
Format @ SUBTRACT
S
CORRESPONDING CORR
identifier+
FROM identifier+/ RROUNDEDU
RON SIZE ERROR imperati)e+statement+U RNOT ON SIZE ERROR imperati)e+statement+/U REND-SUBTRACTU Description The 6>BT!"CT statement is used to su%tract numeric items. *ach of the threeformats wor)s slightl differentl and is descri%ed in the correspondingl num%ered area. 1.
The first format is used to su%tract field(s$ or value(s$ from another field. The field(s$ or value(s$ listed %etween 6>BT!"CT and !O@ aresummed and su%tracted from the value ofthe field(s$ following the !O@. The answer is stored in the individual field. or eample in 6>BT!"CT " !O@ B& the value in " is su%tracted from the value in B and the result is stored in B. The value in " is unchanged. urthermore& in 6>BT!"CT C : !O@ * & the sum of the values in C and : is su%tracted to the value in *& storing the answer in * and the sum of C and : is also su%tracted from & storing the value in . The values in C and : are unchanged.
-.
The second format is used to su%tract field(s$ or value(s$ from one another& storing the answer in a different field. The field(s$ or value(s$ listed %etween 6>BT!"CT and EI?I#E are added and stored in the field following thefield(s$ following the EI?I#E. or eample& in 6>BT!"CT " !O@ B EI?I#E C& the value in " is su%tracted to the value in B and the result is stored in C. The values in " and B are unchanged.
2.
The third format is used to su%tract su%ordinate field(s$ of one group from su%ordinate field(s$ in another& storing the answer in those fields. Those su%ordinate field(s$ of the group i tem identifier+1 are su%tracted from and stored in those with eactl the same name in the group item identifier+-. The names of the su%ordinate items must %e spelled eactl the same wa in %oth groups to =ualif to participate in the su%traction.
-ips
1.
The fields to %e su%tracted must have numeric pictures& that is& the can onl have the characters 9& 6& and ? in their IC clauses.
-.
!eceiving fields ma %e either numeric or numeric edited fields.
2.
>se the 6I* *!!O! clause to detect field overflow on the receiving field.
.
In all formats& the mathematicall correct results are computed& %ut if the receiving field is too short in either the integer or decimal portion&the result will %e truncated& the integer on the left and the decimal on the right. Including the !O>#:*: phrase will result the answer field %eing rounded instead of truncating. !ounding is alwas done in the least significant portion of the answer.
-e -E%M#"'-E &tatement &ynta9
Format TERMINATE report+name+
Description The T*!@I#"T* statement is used to end processing for a report. It prints an final control footings& page footings and report footings. The T*!@I#"T* statement is part of the COBOL !eport
1.
!eport+name+1 must %e the name of a report in the !*O!T 6*CTIO# of the :"T" :I?I6IO#.
-.
The file associated with the report should still %e closed following the eecution of the T*!@I#"T* statement.
2.
>nfortunatel& man implementors have elected not to implement COBOL's !eport
-e +"&-%#"G &tatement &ynta9
Format UNSTRING
identifier+
R
DELIMITED BY RALLU
S
identifier+ / literal+
R
OR RALLU
S
identifier+ 0 literal+/
INTO Sidentifier+1 RDELIMITER IN identifier+2 U RCOUNT IN identifier+3 U... [WITH POINTER identifier+* U
U ... U
[TALLYING IN identifier+4 U [ON OVERFLOW imperati)e+statement+U [NOT ON OVERFLOW imperati)e+statement+/U [END-UNSTRING]
Description The >#6T!I#E statement is used to parse individual items from within a single string. "n num%er of items ma %e parsed. *ntire or partial strings ma %e parsed. "s man items as are provided as I#TO operands will %e parsed. The OI#T*! clause can %e used to start in a position other than 1. or eample& if the OI#T*! varia%le has a value of 2& character 1 and - of the string will not %e included& and the unstringing operation will proceed from the third character. "s each character is inserted into a receiving field& the value of the OI#T*! varia%le will %e incremented& so that the OI#T*! varia%le ma %e used to >#6T!I#E more values from where the previous >#6T!I#E operation left off. The CO>#T phrase can %e used to determine the length of an parsed string. Onl as much of the values in the receiving field will %e changed as is necessar to hold the newl parsed string& for eample& in the statement
UNSTRING A DELIMITED BY SPACES INTO B C.
If " is a five character unstring with a value of " " " and B is a two character string with an original value of BB and C is a two character string with an original value of CC& after the >#6T!I#E statement is eecuted& B will have a value of "B and C will have a value of "C. -ips
1.
The operands should %e non+numeric.
-.
The OI#T*! and CO>#T operands& if an& must %e positive integers& that is their pictures should contain onl 9's.
2.
I#ITI"LI* the receiving items %efore the >#6T!I#E& to remove unwanted characters that ma %e left from a prior operation.
.
>se the O?*!LO< clause to detect field overflow on the receiving field(s$.
-e (%#-E &tatement &ynta9
Format 1 WRITE record+name+
R
S
BEFORE AFTER
R
FROM
ADVANCING
S identifier+ U literal+ S identifier+/ R U U integer+ S S mnemonic+name+ LINE LINES
PAGE
RAT
S
END-OF-PAGE EOP
RNOT AT
S
imperati)e+statement+U imperati)e+statement+U
END-OF-PAGE EOP
REND-WRITEU
Format ? WRITE record+name+
R
FROM
S identifier+ U literal+
RINVALID KEY imperati)e+statement+U RNOT INVALID KEY imperati)e+statement+/U REND-WRITEU Description The
1.
The first format performs a se=uential write of the file.
If that file is a printer file& the ":?"#CI#E clause ma %e used to position the lines on the printed page. B*O!* ":?"#CI#E cause the positioning to occur after the line is written on the page. "T*! ":?"#CI#E caused the positioning to occur %efore the line is written on the page. The LI#*6 clause is used to determine how man lines down the page the paper is to %e positioned. The num%er of lines advanced includes the line to %e written. The "E* phrase causes advancing to the top of the net page. If the file is a printer file and no ":?"#CI#E clause is specified& the paper will %e advanced 1 line %efore the line is printed. or printer files where the LI#"E* clause is specified& if the write causes the allowed num%er of lines per page to %e eceeded& the code %etween "T *#:+O+ "E* and #OT "T *#:+O+"E*& if an& will %e eecuted. If the write does not cause the allowed num%er of lines per page to %e eceeded& the code %etween #OT "T *#:+O+"E* and *#:+
The second format performs a random write of the file. If the record )e matches a )e alread in the file and LIC"T*6 has %een specified on that )e& no record is written and the code %etween I#?"LI: Q*H and #OT I#?"LI: Q*H& if an& will %e eecuted. If the write is successful& the code %etween #OT I#?"LI: Q*H and *#:+
-ips
1.
If there is a file status associated with the file& it will %e set after the
-.
"n file mentioned must %e the su%Dect of a 6*L*CT statement and a file description (:$. iles must %e open for input;output or output prior to eecuting the
2.
.
Be sure to fill in the values of the data fields %efore eecuting the
5.
@iing B*O!* and "T*! advancing
,.
Hou can underline tet on a page % defining a separate line of underline characters and writing it after advancing lines.
4.
In order to invo)e the "T *#:+O+"E* feature& the LI#"E* clause must %e included in the file description (:$ for the printer file.
-#!&
-e Case of te Missing &cope -erminators Indenting& while commonl used to show what portion of the code is conditionall dependent on another portion of the code& doesn't guarantee it. " case in point8 IF ITEM-A NOT MOVE ZEROS TO IF ITEM-B NOT MOVE ZEROS TO IF ITEM-C NOT MOVE ZEROS TO IF ITEM-D NOT MOVE ZEROS TO
NUMERIC ITEM-A NUMERIC ITEM-B NUMERIC ITEM-C NUMERIC ITEM-D
7ere the programmer only "others to check for in)alid data in items B, C, and - if the data in item % was in)alid. 8orse, in order for C and - to "e checked, B also had to "e in)alid and - still isn!t checked unless C is also in)alid. I guess people who make mistakes in data entry must tend to make the same kinds of errors o)er and o)er again. 7ere!s the way it should ha)e "een coded: IF ITEM-A NOT MOVE ZEROS TO END-IF. IF ITEM-B NOT MOVE ZEROS TO END-IF. IF ITEM-C NOT MOVE ZEROS TO END-IF. IF ITEM-D NOT MOVE ZEROS TO END-IF.
NUMERIC ITEM-A NUMERIC ITEM-B NUMERIC ITEM-C NUMERIC ITEM-D
-e #F &tatement from Heck " good length for a routine is one screen. 6im pler routines can %e longer. @ore comple routines are %etter shorter. ere's a reall long routine that is also ver comple. Eood luc) ma)ing sense of it8
IF ITEM-A = ITEM-B IF ITEM-C = ITEM-D PERFORM ALL-MATCH ELSE PERFORM FIRST-MATCH END-IF ELSE IF ITEM-C = ITEM-D PERFORM SECOND-MATCH ELSE PERFORM NO-MATCHES END-IF END-IF.
9our 'O $O!s and none of them accomplishes a thing "ecause they all go to e#actly the same place that the e#ecution would ha)e resumed at anyway. 7ere!s the way it should ha)e "een coded: IF ITEM-A = ITEM-B IF ITEM-C = ITEM-D PERFORM ALL-MATCH ELSE PERFORM FIRST-MATCH END-IF ELSE IF ITEM-C = ITEM-D PERFORM SECOND-MATCH ELSE PERFORM NO-MATCHES END-IF END-IF. RTN-EXIT. EXIT.
(asted &pace " programmer tring to find a pro%lem with a program these das is more li)el to %e loo)ing at a screen than a paper listing. It's a good idea to ma)e things stand out % using %lan) lines %etween them& %ut it's possi%le to carr this idea wa to far. PARAGRAPH-NAME. IF ITEM-A = ITEM-B IF ITEM-C = ITEM-D PERFORM ALL-MATCH ELSE PERFORM FIRST-MATCH END-IF ELSE IF ITEM-C = ITEM-D PERFORM SECOND-MATCH ELSE PERFORM NO-MATCHES END-IF END-IF. ADD 1 TO COUNT-F. READ FILE-E AT END DISPLAY 'END OF FILE'
NOT AT END DISPLAY 'NOW PROCESSING RECORD ' COUNT-F END-READ.
RTN-EXIT. EXIT.
$his routine could easily ha)e fit onto one screen and therefore could ha)e "een seen at once, if the programmer hadn!t inserted so many "lank lines into the procedure. 8ith proper indenting, the conditional dependencies of the code are o")ious without making the code difficult to follow "y going across multiple screens. emem"er when trying to e#amine clauses that should "e aligned such as I9 ; <=< ; or items more prominent. 7ere!s the way it could ha)e "een coded on one screen: PARAGRAPH-NAME. IF ITEM-A = ITEM-B IF ITEM-C = ITEM-D PERFORM ALL-MATCH ELSE PERFORM FIRST-MATCH END-IF ELSE IF ITEM-C = ITEM-D PERFORM SECOND-MATCH ELSE PERFORM NO-MATCHES END-IF END-IF. ADD 1 TO ITEM-A. READ FILE-E AT END DISPLAY 'END OF FILE' NOT AT END DISPLAY 'NOW PROCESSING RECORD ' COUNT-F END-READ. RTN-EXIT. EXIT.
&orry #ve =ust Got to Go It's widel ac)nowledged that use of the EO TO is %ad form& %ut this one ta)es the ca)e. IF ITEM-A = ITEM-B IF ITEM-C = ITEM-D PERFORM ALL-MATCH GO TO RTN-EXIT ELSE PERFORM FIRST-MATCH GO TO RTN-EXIT END-IF ELSE IF ITEM-C = ITEM-D PERFORM SECOND-MATCH GO TO RTN-EXIT
ELSE PERFORM NO-MATCHES GO TO RTN-EXIT END-IF END-IF. RTN-EXIT. EXIT.
9our 'O $O!s and none of them accomplishes a thing "ecause they all go to e#actly the same place that the e#ecution would ha)e resumed at anyway. 7ere!s the way it should ha)e "een coded: IF ITEM-A = ITEM-B IF ITEM-C = ITEM-D PERFORM ALL-MATCH ELSE PERFORM FIRST-MATCH END-IF ELSE IF ITEM-C = ITEM-D PERFORM SECOND-MATCH ELSE PERFORM NO-MATCHES END-IF END-IF. RTN-EXIT. EXIT.
-e "ames Have Been Canged to !rotect te !rogrammers =o/ ere's some actual data names used in a program that maintained an eviction data file produced from the wor) of a group of court researchers8 SHOVEL BODIES-EXHUMED BODIES-INTERRED
$he programmer wanted to "e the only person who could understand his own code. 7e thought he couldn!t "e fired "ecause no"ody would "e a"le to figure out what the program was doing with such unhelpful names for its data items. $he names shown are some of the few that could "e listed pu"licly. $he programmer!s usual tendencies leaned more toward the four+letter data names. $wo weeks after a consultant "rought in "y the company owner to check up on this particular programmer!s work pointed out these and hundreds of other e#amples to the company owner, the programmer, who had "een the Information =ystems department head, was gone and the consultant was the Information =ystems department head. I guess his >o" wasn!t so secure after all. 7ere!s some "etter choices for the data names, considering their actual use: LOOP-COUNTER RECORDS-ADDED RECORDS-UPDATED
COBOL &ty.e ere are a few things ou can do to ma)e our program easier to follow for the net programmer. This is especiall hand since more often than not the net programmer is ou.
Indentation
Organi/ing Hour :ata Items
eriods
#aming Conventions
#ndentation 'rea ' #tems "rea " items have ver little room to differentiate them with indentation since there are onl four columns in "rea "& %ut I still li)e to di stinguish %etween them as much as possi%leG so I put :I?I6IO# and 6*CTIO# headers in column 3 and paragraph names& 1's& and :'s in column 1. 'rea B #tems "rea B items& on the other hand& have plent of room to distin=uish them from one another.
Data Description #tems •
•
•
The level 1 item will %e in column 1 as descri%ed a%ove. 6u%se=uent levels with higher num%ers should %e indented % - from the preceding item. If an item contains 1& 5& 1& and 15 level itemsG the should %e entered in columns 1& 1-& 1& and 1, respectivel. The name of the data item should %e indented % - from its level num%er& so that a level 1 item& for eample will have its level num%er in column 1& and its data name in column 1.
!rocedures •
•
•
•
•
The paragraph name will %e in column 1 as descri%ed a%ove. 6entences should %e i ndented % from the paragraph name& appearing in column 1-. If a sentence does not fit onto one line& the end of the sentence should %e continued on the net line& indented % four from the first line of the sentence. Clauses which affect the eecution of the statement should %e placed on a separate line so the stand out. 6ince these are continuations of the statement& the too will %e indented % four. 6tatements which are dependent on conditional code should %e indented % two from the statement the are dependent on. Indent a scope terminator to match its corresponding ver%.
$he following pieces of code illustrate good indenting practices: D'-' D#*#O" 01 MAJOR-ITEM. 05 INTERMEDIATE-ITEM-1. 10
MINOR-ITEM-1A
PIC X(5).
10
MINOR-ITEM-1B
PIC X(15).
05 INTERMEDIATE-ITEM-2. 10
MINOR-ITEM-2A
PIC X(20).
10
MINOR-ITEM-2B
PIC 99V99.
!%OCED+%E D#*#O" GET-NEXT-RECORD. READ INVENTORY-MASTER-FILE AT END DISPLAY "END OF FILE. RECORDS PROCESSED=" INV-RECORD-COUNT SET END-OF-FILE-SWITCH TO TRUE END-READ.
+sing !eriods •
•
•
Onl two periods are re=uired in a paragraph& one after the paragraph name and one at the end of the last statement. Because of this& man programmers have developed a ha%it of not including an other periods. 6ome programmers do this %ecause the don't li)e to tpe and some do it %ecause if the decide to ma)e a %loc) of code conditional later the would have to go %ac) in and delete all the periods in that section. !egardless of the reason& the missing periods are reall %ad news for the programmer tring to maintain the code.
Organi;ing Your Data #tems •
In the *#?I!O#@*#T :I?I6IO#& put the 6*L*CT statements in alpha%etic order % file name.
•
In the IL* 6*CTIO#& put the :'s in alpha%etic order % file name.
•
•
•
•
•
•
In the @>L"TO!6 or 6se the associated condition names in the !OC*:>!* :I?I6IO#. se complementar names li)e 6<+?"LI:+:"T* and 6<+I#?"LI:+:"T*.
•
•
•
or file status items& code 33+levels for ever status ou epect to see and no others. Eroup the statuses into su%groups of all files that are opened together so ou can code an 33 to test all of these statuses at once.
"aming Conventions •
•
•
•
•
•
•
•
•
Choose a meaningful name that descri%es what will %e stored in the item. :on't name unused fields and don't use the ILL*! name for these either. ields with nothing in the name position stand out %etter than fields with an word& even ILL*! in that position. "void a%%reviations& unless the are commonl used. I#? could mean inventor& invoice& invalid& or something else altogether. "ssign a short prefi to data names to indicate their source. or a record name& use the same prefi for ever field in that record. : on't use that prefi for anthing else. ut record formats in cop %oo)s so that ever program that uses the record calls t he fields % the same name. If two fields hold the same data field& such as a field in a record and a control field for a report& name them eactl the same& %ut with different prefies. There are thirt characters availa%le for a name. >sing a four character prefi& still leaves twent+ five characters for the name. This should %e plent& %ut it doesn't mean ou have to use all of it. If ou have two thirt character data names ou can't code a @O?* statement on one line and of course longer names ta)e longer to tpe. Code enough to %e clear without %eing ver%ose. In the !OC*:>!* :I?I6IO#& use a numeric prefi indicative of its level of depth in the processing& eg. use for the main driverG use 1& -& 2& etc. for paragraphs performed % the G use 11& 1-& 12& etc. for paragraphs performed % 1G use 111& 11-& 112& etc. for paragraphs performed % 11. "rrange the !OC*:>!* :I?I6IO# paragraphs according to their numerical order.
(at to '.ign and (en and (y !#Cture C.auses It is fre=uentl necessar to add up t he ICture clauses to determine the overall length of a group. It is easier to add up a straight column of num%ers. I usuall put mine in column 2,. This leaves enough room for a value clause of up to 13 characters without continuing it on the net line. *'L+E C.auses It is fre=uentl necessar to add up t he ICture clauses to determine the overall length of a group. Lining up the ?"L>* clauses )eeps them out of the wa. I usuall put mine in column ,. This leaves enough room for a value clause of up to 13 characters without continuing it on the net line. or values %etween 19 and 53 characters& I code the actual value right Dustified on the net line. Longer items will need some )ind of continuation anwa so I start those right after the ?"L>*.
Lists in te !%OCED+%E D#*#O"
A B C TO D.
F#LE M'"'GEME"- #" COBOL COBOL has support to 2 principle file organi/ations 8 1.
6e=uential organi/ation
-.
!elative organi/ation (!andom organi/ation or :irect "ccess$
2.
Indeed 6e=uential organi/ation
"ll these three organi/ations can %e used on D#&< filesG onl the 6*F>*#TI"L organi/ation can %e used with -'!E files. "ll files must %e declared in the *#?I!O#@*#T division % proper 6*L*CT statements which should contain a phrase descri%ing to the compiler what tpe of organi/ation the file should have. " second important declaration should %e placed in the IL*+6*CTIO# where the programmer desrci%es the record format associated to the file in : %loc)s. inall& the !OC*:>!* division should have proper O*#;CLO6* and !*":;
THE SEQUENTIAL FILE ORGANIZATION In this file organi/ation& the records of the file are stored one after another %oth phsicall and logicall. That is& record with se=uence num%er 1, is located Dust after the 15th record.
" record of a se=uential file can onl %e accessed % reading all the previous records. The records are discriminated from one another using t he record length declared in the associated : statement of the IL*+6*CTIO#. or eample& If the record structure that the programmer has declared is
5- %tes& %loc)s of 5- %te data (records$ are assumed to placed one after another in the file. If the programmer is reading the data in a se=uential file& ever !*": statement %rings 5- %tes into the memor. If the file contains& sa& 5- %te recordsG %ut t he programmer tries to read this file with a program which has declared %te records (i.e the total length of the : structure is %tes$& the program will certainl read some pieces of information into the memor %ut the after the first !*": statement& some meaningless pieces of records will %e %rought into memor and the program will start processing some phsical records which contain logicall meaningless data. It is the programmer's responsi%ilit t o ta)e care of the record si/es in files. Hou must %e careful when declaring record structures for files. "n mista)e ou ma)e in record si/es will cause our program to read;write erroneous information. This is especiall dangerous if the file contents are %eing altered (changed& updated$. 6ince the records are simpl appended to each other when %uilding 6*F>*#TI"L files& ou simpl end up with a 6T!*"@ of %te. If this string does not contain an Carriage !eturn;Line eed control characters in it& the whole file will appear as a single LI#* of character and would %e impsossi%le to process with regular tet editors. "s ou should )now % now& tet editors are good in reading;writing;modifing tet files. These programs will assume that the file consists of LI#*6 and epect the lines to separated from each other % a pair of control characters called Carriage !eturn;Line eed (or C!;L$. COBOL has a special tpe of se=uential file organi/ation& which is called the LI#* 6*F>*#TI"L O!E"#I"TIO# which places a C!;L pair at the end of each record while adding records to a file and epect such a pair while reading. LI#* 6*F>*#TI"L files are much easier to use while developing programs %ecause ou can alwas use a simple tet editor to see the contents of our se=uential file and trace;de%ug our program. lease note that LI#* 6*F>*#TI"L files have two etra characters for each record. or files& which have millions of records& this might use up a significant amount of dis) space. 6*F>*#TI"L files have onl one "CC*66 @O:* and that is se=uential access. Therefore ou need not specif an "CC*66 @O:* in the 6*L*CT st atement. Tpical 6*L*CT statements for 6*F>*#TI"L files are 8 SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS SEQUENTIAL. SELECT MYFILE-2 ASSIGN TO DISK "C:\DATADIR\MYFILE2.TXT" ORGANIZATION IS LINE SEQUENTIAL.
In the IL*+6*CTIO#& ou must provide : %loc)s for each fileG hence for a se=uential file ou could have something li)e 8 FD MYFILE. 01 MYFILE-REC. 02 M-NAME PIC X1!. 02 M-SURNAME PIC X1!. 02 M-#IRT$DATE. 0% M-#D-YEAR PIC &&&&. 0% M-#D-MONT$ PIC &&. 0% M-#D-DAY PIC &&.
Note 8 Hou must #OT provide record fields for the etra two C!;L %tes in record descriptions of LI#* 6*F files. Once ou declare the file to %e a LI#* 6*F file& these two etra %tes are automaticall ta)en in consideration and added for all new records that are added to a file.
!%OCED+%E D#*#O" Considerations
Li)e all files& se=uential and line se=uential files must %e O!E"ed %efore the can %e processes and CLO&Ed when the are not needed anmore. 6e= and Line 6e= files can %e opened for 8 1.
O>T>T
-.
I#>T
2.
*T*#:
.
I+O (Input+Output$
Opening a se= file for O+-!+- means that the program will onl issue 6T C!*"T*: file. Therefore& when ou open a file for O>T>T& COBOL assumes that the file does not e9ist and tr to create a new one. I T* IL* *I6T6& IT6 CO#T*#T6 @*: TO B* " B%'"D "E( IL* I#TO <IC !*CO!:6 T>T. One small mistake and all your valuable records are lost forever. Opening a se= file for #"!+- means that the program will onl issue !*": statements to an *I6TI#E file. Therefore& when ou open a file for I#>T& COBOL assumes that the fi.e e9ists and tr to access it. I T* IL* :O*6 #OT *I6T& "# *!!O! @*66"E* *: I#:IC"TI#E T"T T* @*#TIO#*: IL* CO>L: #OT B* O>#:. Opening a se= file for E>-E"D means that the program will add #*< !*CO!:6 to an *I6TI#E file. Therefore& when ou open a file for *T*#:& COBOL assumes that the file eists and su%se=uent
OUTPUT NE'-FILE. INPUT OLD-FILE. EXTEND OLD-FILE OLD-FILE2. INPUT OLD-FILE OUTPUT NE'-FILE.
Hou can close more than one files with a single CLO6* statement.
&imp.e #"!+-)O+-!+- &tatements for &E, Fi.es
Once ou open a se= file& ou can use !*": or T>T or *T*#: and similarl ou cannot use a T. " tpical !*": statement loo)s li)e 8 READ MY-FILE.
" good programmer must chec) whether the !*": statement could find a record to read successfull. That meansG the programmer must chec) whether t here were an records to !*": when t he statement was eecuted. The situation can %e chec)ed with the following construct 8 READ MY-FILE AT END PERFORM NO-RECORDS-FOUND.
The construct tells the COBOL compiler to eecute the paragraph la%eled #O+!*CO!:6+O>#: when the !*": statement cannot find an records to read. "nother eample could %e 8 READ MY-FILE AT END DISPLAY "No (o)* )*+o)," CLOSE MY-FILE STOP RUN.
Once our program **C>T*6 a successful !*": statement& the information in the data record that was Dust %rought into memor will %e availa%le in the corresponding varia%les mentioned in our record description declaration (the : %loc)$. T>T or *T*#: (depending on the re=uirements of the algorithm of our program$. The COBOL statement that is used to put records into a file is the
the file should %e opened
-.
new values for the field varia%les of the : record description must %e moved to proper varia%les of the record
2.
the !*CO!: #"@* is specified in the
This will %e %est descri%ed % an eample 8 6uppose ou want to add a new record to the file
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS SEQUENTIAL. .... FD MYFILE. 01 MYFILE-REC. 02 M-NAME PIC X1!. 02 M-SURNAME PIC X1!. 02 M-#IRT$DATE. 0% M-#D-YEAR PIC &&&&. 0% M-#D-MONT$ PIC &&. 0% M-#D-DAY PIC &&.
Hou must first open it& then move new values to the fields in @HIL*+!*C and finall
OPEN .... MO/E MO/E MO/E MO/E
EXTEND MYFILE. "UGUR" TO M-NAME. "AYFER" TO M-SURNAME. 1& TO M-#D-YEAR. 1 TO M-#D-MONT$.
MO/E 1 TO M-#D-DAY. 'RITE MYFILE-REC. .....
+!D'-#"G %ECO%D& OF ' &E,+E"-#'L F#LE 6ometimes ou need to ma)e some small changes in the contents of a file. Of course it possi%le to create a %rand new file with the new& modified contents %ut this is not practical. COBOL provides ou with an O*# I+O mode with which ou can modif onl the re=uired record in a file. In other words& there is another fil e opening modeG the I+O modeG and another special !*. The program that ou can write could read li)e SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS SEQUENTIAL. .... FD MYFILE. 01 MYFILE-REC. 02 M-NAME PIC X1!. 02 M-SURNAME PIC X1!. 02 M-#IRT$DATE. 0% M-#D-YEAR PIC &&&&. 0% M-#D-MONT$ PIC &&. 0% M-#D-DAY PIC &&.
PROCEDURE DI/ISION. .... OPEN I-O MYFILE. .... MO/E 20 TO N. .... PERFORM READ-A-REC N-1 TIMES.
TO MAKE SURE T$AT 'E ARE ON T$E CORRECT RECORD IF M-SURNAME NOT "AYFER" DISPLAY "'RONG RECORD" DISPLAY "RECORD NOT UPDATED" CLOSE MYFILE STOP RUN. MO/E "AYFEROGLU" TO M-SURNAME. RE'RITE MYFILE-REC. ..... CLOSE MYFILE. ....
DELE-#"G %ECO%D& OF ' &E,+E"-#'L F#LE #t is "O- possi/.e to de.ete records of a se fi.e. If ou do not want a specific record to %e )ept in a se= file an more& all ou can do is to modif the contents of the record so that it contains some special values that our program will recogni/e as de.eted (remem%er to open the file in I+O mode and !*
'D*'"-'GE& of &E, F#LE& 1.
?er eas to process&
-.
Can %e easil shared with other applications developed using different programming languages&
D#&'D*'"-'GE of &E,+E"-#'L F#LE& Can %e onl processed se=uentiall. If ou need to read record num%er #& ou must first read the previous #+1 records. *speciall no good for programs that ma)e fre=uent searches in the file.
THE RELATIVE FILE ORGANIZATION In this file organi/ation& the records of the file are stored one after another %oth phsicall and logicall. That is& record with se=uence num%er 1, is located Dust after the 15th record.
In contrast to 6*F files& records of a !*L"TI?* file can %e accessed % specifing t he record se=uence num%er in the !*": statement (the K!" and without needing to read all the previous records. It is the programmer's responsi%ilit t o ta)e care of the record si/es in files. Hou must %e careful when declaring record structures for files. "n mista)e ou ma)e in record si/es will cause our program to read;write erroneous information. This is especiall dangerous if the file contents are %eing altered (changed& updated$. lease note that ou must provide the record structure to have a special field allocated to contain t he Q*H& that is the record se=uence num%er. !*L"TI?* files can have !"#:O@ or 6*F>*#TI"L "CC*66 @O:*s. If the 'CCE&& MODE is dec.ared to /e %'"DOM in the corresponding 6*L*CT statement& t he program can read the record with )e value -25 and then the record with )eN2& then record -2 etc etc. In short& one can access an record of a relative file& in an order& provided that the Q*H value is specified %efore the !*": statement is eecuted. If the "CC*66 @O:* is 6*F>*#TI"L& t hat means the records of the file will %e accesses in their phsical se=uential order (Dust li)e 6*F>*#TI"L and LI#* 6*F>*#TI"L files$ and no specific Q*H value %e given for the !*": statementsG %ut instead& the #*T clause will appear in !*": statements meaning Eo get the record with the net consecutive )e value. SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS RELATI/E ACCESS MODE IS RANDOM RECORD KEY IS M-IDNO. SELECT MYFILE-2 ASSIGN TO DISK "C:\DATADIR\MYFILE2.TXT" ORGANIZATION IS RELATI/E ACCESS MODE IS SEQUENTIAL RECORD KEY IS M-IDNO.
In the IL*+6*CTIO#& ou must provide fields for the Q*H& the numerical @+I:#O in this eample& in the : %loc) of the !*L"TI?* fileG FD 01
MYFILE. MYFILE-REC. 02 M-IDNO 02 M-NAME 02 M-SURNAME 02 M-#IRT$DATE. 0% M-#D-YEAR 0% M-#D-MONT$ 0% M-#D-DAY
PIC &&&&. PIC X1!. PIC X1!. PIC &&&&. PIC &&. PIC &&.
Note 8 6ince the )e field will contain integer& record se=uence num%ersG ou should declare the field to %e numerical and ta)e care that it is wide enough to carr all possi%le values for the )e value. or eample& if
our file is epected to have -& records in it& the )e field should declared at least , %tes ( IC 999999 $ so that it can hold the values 1 through -&. " )e field declaration of IC 999 would let use a file of ma 999 records in it.
!%OCED+%E D#*#O" Considerations Li)e all files& relative files must %e O!E"ed %efore the can %e processes and CLO&Ed when the are not needed anmore. !elative files can %e opened for 8 1.
O>T>T
-.
I#>T
2.
I+O (Input+Output$
Opening a relative file for O+-!+- means that the program will onl issue 6T C!*"T*: file. Therefore& when ou open a file for O>T>T& COBOL assumes that the file does not e9ist and tr to create a new one. I T* IL* *I6T6& IT6 CO#T*#T6 @*: TO B* " B%'"D "E( IL* I#TO <IC !*CO!:6 T>T. One small mistake and all your valuable records are lost forever. Once ou open a relative file in Output mode& ou are epected to write records in the I#C!*"6I#E and CO#6*C>TI?* order of )es. That is& %efore writing the record with )e 93 (93th record$ ou should first write the first 94 records(. Opening a relative file for #"!+- means that the program will onl issue !*": statements to an *I6TI#E file. Therefore& when ou open a fil e for I#>T& COBOL assumes that the fi.e e9ists and tr to access it. I T* IL* :O*6 #OT *I6T& "# *!!O! @*66"E* *: I#:IC"TI#E T"T T* @*#TIO#*: IL* CO>L: #OT B* O>#:. If ou have declared the "CC*66 @O:* to %e !"#:O@& %efore each !*": statement& ou are supposed to move a valid Q*H value to the record field varia%le that is declared as the %ECO%D
OPEN INPUT MYFILE. .... MO/E 2% TO M-IDNO. READ MYFILE.
Eood programmers should ta)e precautions in their program to avoid error messages and su%se=uent a%normal terminations is an I#?"LI: value for the record )e specified %efore the !*": statement. The I#?"LI: Q*H clause handles this in COBOL. MO/E 2%00 TO M-IDNO. READ MYFILE IN/ALID KEY
PERFORM OUT-OF-RANGE.
The I#?"LI: Q*H condition raises if the value in @+I:#O is /ero& negative or has a value greater than the total num%er of records in the file. If ou have declared the "CC*66 @O:* as 6*F>*#TI"L& ou should use the #*T clause in the !*": statement. Li)e READ MYFILE NEXT AT END PEFORM NO-MORE-RECORDS.
Hou can close more than one files with a single CLO6* statement.
&imp.e #"!+-)O+-!+- &tatements for %EL'-#*E Fi.es Once ou open a relative file& ou can use !*": or T >T and similarl ou cannot use a T. " tpical !*": statement for !"#:O@ access mode loo)s li)e 8 MO/E 12% TO M-IDNO. READ MY-FILE.
" good programmer must chec) whether the !*": statement could find a record to read successfull. That meansG the programmer must chec) whether t here were an records with the indicated )e value when the statement was eecuted. The situation can %e chec)ed with the following construct 8 READ MY-FILE
IN/ALID KEY PERFORM
NO-RECORDS-FOUND.
The construct tells the COBOL compiler to eecute the paragraph la%eled #O+!*CO!:6+O>#: when the !*": statement cannot find an record with the )e value stored in the )e field varia%le. Once our program **C>T*6 a successful !*": statement& the information in the data record that was Dust %rought into memor will %e availa%le in the corresponding varia%les mentioned in our record description declaration (the : %loc)$. T>T. The COBOL statement that is used to put records into a file is the
the file should %e opened
-.
new values for the field varia%les of the : record description must %e moved to proper varia%les of the record
2.
the value of the Q*H ?"!I"BL* must have a valid value and this valid value should %e one greater than the the )e value of the record written previousl&
.
the !*CO!: #"@* is specified in the
+!D'-#"G %ECO%D& OF ' %EL'-#*E F#LE 6ometimes ou need to ma)e some small changes in the contents of a file. Of course it possi%le to create a %rand new file with the new& modified contents %ut this is not practical. COBOL provides ou with an O*# I+O mode with which ou can modif onl the re=uired record in a file. In other words& there is another fil e opening modeG the I+O modeG and another special !*. The program that ou can write could read li)e SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS RELATI/E
ACCESS MODE IS RANDOM RECORD KEY IS M-IDNO.
.... FD MYFILE. 01 MYFILE-REC. 02 M-IDNO 02 M-NAME 02 M-SURNAME 02 M-#IRT$DATE. 0% M-#D-YEAR 0% M-#D-MONT$ 0% M-#D-DAY
PIC &&&&. PIC &&. PIC &&.
PROCEDURE DI/ISION. .... OPEN I-O MYFILE. .... MO/E 20 TO M-IDNO. READ MYFILE IN/ALID KEY PERFORM KEY-ERROR.
PIC &&&&. PIC X1!. PIC X1!.
TO MAKE SURE T$AT 'E ARE ON T$E CORRECT RECORD IF M-SURNAME NOT "AYFER" DISPLAY "'RONG RECORD" DISPLAY "RECORD NOT UPDATED" CLOSE MYFILE STOP RUN. MO/E "AYFEROGLU" TO M-SURNAME. RE'RITE MYFILE-REC. ..... CLOSE MYFILE. .....
DELE-#"G %ECO%D& OF ' %EL'-#*E F#LE #t is "O- possi/.e to de.ete records of a re.ative fi.e. If ou do not want a specific record to %e )ept in a relative file an more& all ou can do is to modif the contents of the record so that it contains some special values that our program will recogni/e as de.eted (remem%er to open the file in I+O mode and !*
'D*'"-'GE& of %EL'-#*E F#LE& 1.
Fuite eas to process&
-.
If ou can )now the )e value of the record that ou need to find& there is no need for a search and ou can access the record almost instantaneousl&
D#&'D*'"-'GE of %EL'-#*E F#LE& Can %e onl used in conDunction with consecutive numerical )es. If ou )e value range is 1+1& ou must have records for all 1 possi%le )es& that is& our file must have eactl 1 records. If& for eample& ou are planning to use student I: num%ers as numerical )e values and the student id num%ers are li)e 9,52-1-2- (1 digits$& ou file must contain 9&999&999&999 records and t his doesn't ma)e sense. Of course& ou should modif the student I: num%er structure to start from1 and etend till a few thousand. This disadvantage (onl numerical and consecutive values for t he )e value$ is overcome with a completel different file structure& name) the I#:**: 6*F>*#T I"L IL*.
THE INDEXED FILE ORGANIZATION In this file organi/ation& the records of the file are stored one after another in the order the are added to the file.
In contrast to !*L"TI?* files& records of a I#:**: 6*F>*#TI"L file can %e accessed % specifing an "L"#>@*!IC )e in the !*": statement (the K!" . It is the programmer's responsi%ilit t o ta)e care of the record si/es in files. Hou must %e careful when declaring record structures for files. "n mista)e ou ma)e in record si/es will cause an error and a%normal termination. lease note that ou must provide the record structure to have a special field allocated to contain t he Q*H. I#:**: files can have !"#: O@ or 6*F>*#TI"L "CC*66 @O:*s. If the 'CCE&& MODE is dec.ared to /e %'"DOM in the corresponding 6*L*CT statement& t he program can read the record with )e value "BC and then the record with )eN"1-& then record with )e :- etc etc. In short& one can access an record of an indeed file& in an order& provided that the Q*H value is specified %efore the !*": statement is eecuted. If the "CC*66 @O:* is 6*F>*#TI"L& t hat means the records of the file will %e accesses in their phsical se=uential order (Dust li)e 6*F>*#TI"L and LI#* 6*F>*#TI"L files$ and no specific Q*H value %e given for the !*": statementsG %ut instead& the #*T clause will appear in !*": statements meaning Eo get the record with the net consecutive )e value. SELECT MYFILE ASSIGN TO DISK "C:\DATADIR\MYFILE.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS M-IDNO.
SELECT MYFILE-2 ASSIGN TO DISK "C:\DATADIR\MYFILE2.TXT" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS M-IDNO.
In the IL*+6*CTIO#& ou must provide fields for the Q*H& the @+I:#O in this eample& in the : %loc) of the I#:**: fileG FD 01
MYFILE. MYFILE-REC. 02 M-IDNO 02 M-NAME 02 M-SURNAME 02 M-#IRT$DATE. 0% M-#D-YEAR 0% M-#D-MONT$ 0% M-#D-DAY
PIC XXXX. PIC X1!. PIC X1!. PIC &&&&. PIC &&. PIC &&.
Note 8 6ince the )es are not supposed to %e numerical& ou can use the picture for )e fields.
!%OCED+%E D#*#O" Considerations Li)e all files& Indeed files must %e O!E"ed %efore the can %e processes and CLO&Ed when the are not needed anmore. Indeed files can %e opened for 8
1.
O>T>T
-.
I#>T
2.
I+O (Input+Output$
Opening an indeed file for O+-!+- means that the program will onl issue 6T C!*"T*: file. Therefore& when ou open a file for O>T>T& COBOL assumes that the file does not e9ist and tr to create a new one. I T* IL* *I6T6& IT6 CO#T*#T6 @*: TO B* " B%'"D "E( IL* I#TO <IC !*CO!:6 T>T. One small mistake and all your valuable records are lost forever. Once ou open an indeed file in Output mode& ou are epected to write records in the I#C!*"6I#E order of )es. That is& %efore writing the record with )e "93 ou should first write all the records with )e values alphanumericall less than "93. lease note that indeed fi)le )es are case sensitiveG that is the )e value a39 is #OT *F>"L TO "39G in fact it is alphanumericall greater than "39 %ecause the "6CII value of a (94$ is greater than the "6CII value of " (,5$. Opening an indeed file for #"!+- means that the program will onl issue !*": statements to an *I6TI#E file. Therefore& when ou open a fil e for I#>T& COBOL assumes that the fi.e e9ists and tr to access it. I T* IL* :O*6 #OT *I6T& "# *!!O! @*66"E* *: I#:IC"TI#E T"T T* @*#TIO#*: IL* CO>L: #OT B* O>#:. If ou have declared the "CC*66 @O:* to %e !"#:O@& %efore each !*": statement& ou are supposed to move a valid Q*H value to the record field varia%le that is declared as the %ECO%D
OPEN INPUT MYFILE. .... MO/E "X2%" TO M-IDNO. READ MYFILE.
Eood programmers should ta)e precautions in their program to avoid error messages and su%se=uent a%normal terminations is an I#?"LI: value for the record )e specified %efore the !*": statement. The I#?"LI: Q*H clause handles this in COBOL. MO/E "2%00" TO M-IDNO. READ MYFILE IN/ALID KEY PERFORM OUT-OF-RANGE.
The I#?"LI: Q*H condition raises if there is no record in the file with )e value e=ual to the specified )e value. If ou have declared the "CC*66 @O:* as 6*F>*#TI"L& ou should use the #*T clause in the !*": statement. Li)e READ MYFILE NEXT AT END PEFORM NO-MORE-RECORDS.
Hou can close more than one files with a single CLO6* statement.
%E(#"D#"G an indeed file with "CC*66 @O:* !"#:O@ is not meaninful. If the "CC*66 @O:* is 6*F>*#TI"L& ou can CLO6* and than O*# I#>T again to rewind a se=uential accessed indeed file.
&imp.e #"!+-)O+-!+- &tatements for #"DE>ED Fi.es Once ou open an indeed file& ou can use !*": or T>T and similarl ou cannot use a T. " tpical !*": statement for !"#:O@ access mode loo)s li)e 8 MO/E "1X%" TO M-IDNO. READ MY-FILE.
" good programmer must chec) whether the !*": statement could find a record to read successfull. That meansG the programmer must chec) whether t here were an records with the indicated )e value when the statement was eecuted. The situation can %e chec)ed with the following construct 8 READ MY-FILE
IN/ALID KEY PERFORM
NO-RECORDS-FOUND.
The construct tells the COBOL compiler to eecute the paragraph la%eled #O+!*CO!:6+O>#: when the !*": statement cannot find an record with the )e value stored in the )e field varia%le. Once our program **C>T*6 a successful !*": statement& the information in the data record that was Dust %rought into memor will %e availa%le in the corresponding varia%les mentioned in our record description declaration (the : %loc)$. T>T. The COBOL statement that is used to put records into a new file is the
the file should %e opened
-.
new values for the field varia%les of the : record description must %e moved to proper varia%les of the record
2.
the value of the Q*H ?"!I"BL* must have a valid value and this valid value should %e alphanumericall greater than the the )e value of the record written previousl&
.
the !*CO!: #"@* is specified in the
+!D'-#"G %ECO%D& #" '" #"DE>ED F#LE 6ometimes ou need to ma)e some small changes in the contents of a file. Of course it possi%le to create a %rand new file with the new& modified contents %ut this is not practical. COBOL provides ou with an O*# I+O mode with which ou can modif onl the re=uired record in a file. In other words& there is another fil e opening modeG the I+O modeG and another special %E(%#-E statement. 6uppose that ou want to change the surname field of a record in an indeed file (originall "H*!$ into "H*!OEL>. The program that ou can write could read li)e
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS M-IDNO. .... FD MYFILE. 01 MYFILE-REC. 02 M-IDNO PIC XXXX. 02 M-NAME PIC X1!.
02 02
M-SURNAME PIC X1!. M-#IRT$DATE. 0% M-#D-YEAR PIC &&&&. 0% M-#D-MONT$ PIC &&. 0% M-#D-DAY PIC &&.
... PROCEDURE DI/ISION. .... OPEN I-O MYFILE. .... MO/E "X20" TO M-IDNO. READ MYFILE IN/ALID KEY PERFORM KEY-ERROR. TO MAKE SURE T$AT 'E ARE ON T$E CORRECT RECORD IF M-SURNAME NOT "AYFER" DISPLAY "'RONG RECORD" DISPLAY"RECORD NOT UPDATED" CLOSE MYFILE STOP RUN. MO/E "AYFEROGLU" TO M-SURNAME. RE'RITE MYFILE-REC. .... CLOSE MYFILE. .....
#mportant 8 The )e field of a record in an indeed file C"##OT %e updated. If the programmer has to update the )e field& the onl solution is to delete the record with the old )e value and a add the same record to the fileG this time with the new )e value.
'DD#"G %ECO%D& -O '" #"DE>ED F#LE
DELE-#"G %ECO%D& OF '" #"DE>ED F#LE If ou do not want a specific record to %e )ept in an indeed file an more& ou can use the :*L*T* statement to ma)e the record inaccessi%le. The :*L*T* statement is used more or less li)e the
lease note that& when ou delete a record& the phsical record is #OT deleted. Onl the record is rendered inaccessi%le. 6uppose& for instance that an indeed file is - @%tes in total length and ou delete half of the records in this file. "fter all the deletions are completed& ou shall see that the file is still - @%tes in length. The onl wa ou can recover the dis) space used % deleted records is !*O!E"#II#E the indeed file. (6ee section !*O!E"#II#E I#:**: IL*6$.
'D*'"-'GE& of #"DE>ED F#LE&
1.
Fuite eas to process&
-.
2.
"n field of the records can %e used as the )e. The )e field can %e numerical or alphanumerical.
D#&'D*'"-'GE& of #"DE>ED F#LE& 1.
*tra data structures have to %e maintained (the COBOL run+time modules ta)e care of these and it is not the programmers' concern$. These etra data structures maintained on the dis) can use up much dis) space& especiall for long )e values.
-.
The indeed files have to %e reorgani/ed from time time to get rid of deleted records and improve performance that gets graduall decreased with addition of new records.
'D*'"CED FE'-+%E& OF #"DE>ED &E,+E"-#'L F#LE& %eorgani;ing #nde9ed Fi.es 8 This operation is usuall done % a utili program suppled % the maunfacturer of the COBOL compiler that ou use. !efer to the manuals of our compiler for details and instructions.
M+L-#!LE #IF*. "LT*!#"T* Q*H6 C"# "?* :>LIC"T* ?"L>*6. Hou can declare as man alternate )es our program re=uires. @ore alternate )es ou have& more etra data structures will %e re=uired and slower %e our program. Therefore certain attention should %e paid %efore assigning alternate )es. Hou should avoid unnecessar alternate )es.
.... FD /E$ICLES. 01 /E$ICLE-REC. 02 PLATE 02 #RAND 02 O'NER 02 DATE-REGISTERED 02 ENGINE-NO 02 C$ASSIS-NO ...
PIC PIC PIC PIC PIC PIC
XXXXXXXXXX. X20. X%2. &&&&&&&&. X20. X20.
-e &-'%- &tatement 6ometimes& it is necessar to find a set of records for which ou want to specif a criterion. or eample& in our vehicles eample& ou might want to find the records of TOHOT" %rand vehicles. If the criterion is related to one of the )es& ou will not have to go through the whole file& testing the fields against a certan value. Instead& ou can use the 6T"!T statement to find the first vehicle with TOHOT" recorded as the %rand. "n eample is
START /E$ICLES KEY #RAND IS EQUAL TO "TOYOTA" IN/ALID KEY NO-SUC$-#RAND. LOOP1. READ /E$ICLES NEXT AT END PERFORM END-OF-#RAND. ... GO LOOP1. ...
#$%O&'N' ) lease note that when ou use the 6T"!T statement& the file is accessed as a !"#:O@ fileG whereas the su%se=uent !*": #*T statements acces the file in 6*F>*#TI"L mode. This dilemna is solved % the "CC*66 @O:* I6 :H#"@IC phrase in the 6*L*CT statement. T or I+O. The 6T"!T statement is not allowed and in fact not meaningful when the fil e is opened for O>T>T.
&O%-#"G F#LE& #" COBOL 4%M)Co/o.5 *ine se+uential and e+uential files are used ver fre=uentl in data processing applications. The records in these files are usuall need to %e put in ascending or descending order for proper and eas handling. (!emem%er that searching records in an ordered file ( a file sorted in respect to the field which is
the search )eG ou can stop reading records when ou find a record which has t he key field having greater value than the value ou are searching for$. 6orting a set of data which will fit into arras (therefore fitting into the m emor$ is =uite eas. (!emem%er the #nternal ort techni=ues ou have learned in previous courses$. 6orting large files which will not fit into arras in memor& on the other hand& is not eas. This re=uires special algorithms to %e implemented and generall called *T*!#"L 6O!T T*C#IF>*6. @an COBOL compilers provide facilities to solve this *teral 6ort pro%lem. !@+COBOL& for instance& has a ver powerful &O%- statement which will let the user 6O!T and @*!E* se=uential files ver easil. 6O!TI#E does not need much eplanation here. The $&- process however& needs a little %it eplanation. @*!EI#E :"T" is simpl com%ining two or more 6O!T*: se=uential files together into a single file so that the resulting file is also sorted. =(*2; ...
ON ASCENDING;DESCENDING< KEY ,=4=->=(*1; ,=4=ON ASCENDING;DESCENDING< KEY ,=4=->=(*1; ,=4=-
>=(*2; ... USING 789*-4o-?*-o)4*, GI/ING o)4*,-789*
In order to use this 6O!T statement& ou will have to declare 2 filesG 1.
The
-.
The file to %e sorted (input file$
2.
The file which will contain the sorted records (output file$
The
PERS-REC. 02 ID-NO 02 NAME 02 SURNAME 02 GENDER 02 DEPT-CODE 02 NC$ILDREN 02 $OME-ADR1 02 $OME-ADR2 02 $OME-ADR% 02 $OME-TEL 02 EMPLOYMENT-DATE. 0% R-DAY 0% R-MONT$ 0% R-YEAR 02 LEA/E-DATE. 0% R-DAY 0% R-MONT$ 0% R-YEAR 02 LEA/E-REASON
PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC
X3. X1!. X1!. X. X. &&. X2. X2. X2. X12.
PIC &&. PIC &&. PIC &&&&. PIC &&. PIC &&. PIC &&&&. PIC X.
The corresponding 6O!T
SORT-'ORK-FILE.
01
PERS-REC. 02 ID-NO 02 NAME 02 SURNAME 02 FILLER
PIC PIC PIC PIC
X3. X1!. X1!. X10@.
This declaration will ena%le the user to issue a 6O!T statement using the #"@* and 6>!#"@* fieldsG into either ascending or descending order. lease note that a 13 %te filler is used in the 6O!T+!# "@* to %e the secondar sort fieldG so that records with identical names will %e sorted into surnames among themselves. (Pust li)e the entries in a telephone director$. !eferring to our *!6O#*L.:"T eample& tpical file declarations and a 6O!T statement would loo) li)e 8 In the *#?I!O#@*#T :I?I6IO# 8 INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SORT-FILE ASSIGN TO DISK "SORT'ORK". SELECT UNSORTED-PERSON ASSIGN TO DISK "PERSONEL.DAT" ORGANIZATION IS LINE SEQUENTIAL. SELECT SORTED-PERSON ASSIGN TO DISK "SPERSONL.DAT" ORGANIZATION IS LINE SEQUENTIAL.
In the :"T" :I?I6IO# 8 FILE SECTION. SD SORT-FILE. 01 SORT-RECORD. 02 FILLER 02 S-NAME 02 S-SURNAME 02 FILLER
PIC PIC PIC PIC
FD 01
UNSORTED-PERSON. FILLER
PIC X13.
FD 01
SORTED-PERSON. FILLER
PIC X13.
X3. X1!. X1!. X103.
In the !OC*:>!* :I?I6IO# 8 SORT SORT-FILE ON ASCENDING KEY S-NAME S-SURNAME ON DESCENDING S-EMP-DATE USING UNSORTED-PERSON GI/ING SORTED-PERSON.
"otes :
1.
The lengths of records in the wor)& input and output files should match.
-.
Hou do not need to declare the details of input and output file records if ou do not need these details in our program.
2.
Hou should not O*# or CLO6* the wor)& input and output files %efore& after or during the 6O!T operation.
.
Hou can specif more than one sort field and the fields ma have different ordering (ascending;descending$.
" complete !@+COBOL program which sorts the *!6O#*L.:"T file& creating the sorted file 6*!6O#L.:"T might loo) li)e 8
%lease study the O&' statement and its relevant file declarations carefully
IDENTIFICATION DI/ISION. PROGRAM-ID. "SORT DEMO". EN/IRONMENT DI/ISION. CONFIGURATION SECTION. SOURCE-COMPUTER. RMCO#OL-3. O#BECT-COMPUTER. RMCO#OL-3. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SORT-FILE ASSIGN TO DISK "SORT'ORK". SELECT UNSORTED-PERSON ASSIGN TO DISK "PERSONEL.DAT" ORGANIZATION IS LINE SEQUENTIAL. SELECT SORTED-PERSON ASSIGN TO DISK "SPERSONL.DAT" ORGANIZATION IS LINE SEQUENTIAL. DATA DI/ISION. FILE SECTION. SD SORT-FILE. 01 SORT-RECORD. 02 S-ID-NO PIC X3. 02 S-NAME PIC X1!. 02 S-SURNAME PIC X1!. 02 S-GENDER PIC X. 02 S-DEPT-CODE PIC &. 02 FILLER PIC X3&. 02 S-EMP-DATE. 0% S-EMP-DATE-DD PIC &&. 0% S-EMP-DATE-MM PIC &&. 0% S-EMP-DATE-YY PIC &&&&. 02 S-LEA/E-DATE. 0% S-LEA/E-DATE-DD PIC &&. 0% S-LEA/E-DATE-MM PIC &&. 0% S-LEA/E-DATE-YY PIC &&&&. 02 S-LEA/E-REASON PIC X.
FD 01
UNSORTED-PERSON. FILLER
FD SORTED-PERSON. 01 FILLER PROCEDURE DI/ISION.
PIC X13.
PIC X13.
MAIN-PGM. SORT SORT-FILE ON ASCENDING KEY S-NAME S-SURNAME ON DESCENDING S-EMP-DATE USING UNSORTED-PERSON GI/ING SORTED-PERSON. STOP RUN.