Fun

Like BASIC and PASCAL, COBOL is a computer language. "COBOL" is pronounced "koe ball" and stands for "COmmon Business Oriented Language".

COBOL solves business problems that involve large files of data, so COBOLís used mainly by businesses having maxicomputers. But today, you can use COBOL even on minicomputers and microcomputers.

In the "help wanted" section of your local newspaper, many ads that say "programmer wanted" are placed by businesses that have maxicomputers and use COBOL. To get a job through the "help wanted" section, a knowledge of COBOL will help you more than PASCAL or FORTRAN.

The first version of COBOL was called COBOL 60, because it was invented in 1960. Then came COBOL 61, COBOL 65, COBOL 68, COBOL 74, and COBOL 85. Today, most computers still use COBOL 74 or a variation of it. This chapter explains how to write COBOL programs that work on most computers.

During the 1960ís and early 1970ís, COBOL programmers used a style called "easy programming". Today, most COBOL programmers use a more sophisticated style, called structured programming. This chapter explains structured programming. Though itís harder to learn than easy programming, it will make your boss kiss you.

Simple programs

Every COBOL program is written as an outline. Hereís a short outline; to turn it into a COBOL program, just fill in the blanks:

IDENTIFICATION DIVISION.

PROGRAM-ID.

The program's name.

AUTHOR.

Your name.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

DATA DIVISION.

PROCEDURE DIVISION.

MAIN-ROUTINE.

What you want the computer to do.

STOP RUN.

 

For example, hereís a COBOL program I wrote:

Program Reason

IDENTIFICATION DIVISION.

PROGRAM-ID.

HARRY. The programís name is HARRY.

AUTHOR.

RUSS WALTER. My name is Russ Walter.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

DECSYSTEM-20. My computerís a DECsystem-20.

OBJECT-COMPUTER.

DECSYSTEM-20.

DATA DIVISION.

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "LIFE STINKS." I want the computer to gripe.

STOP RUN.

When I run that program, the computer will print:

LIFE STINKS

Every COBOL program consists of four parts. The first part of the program is called the IDENTIFICATION DIVISION: it includes the programís name and the programmerís name. The second part of the program is called the ENVIRONMENT DIVISION: it includes the computerís name. The third part of the program is called the DATA DIVISION; for a simple program, the DATA DIVISION is blank. The fourth part of the program is the PROCEDURE DIVISION: it says what you want the computer to do.

The order is important: the IDENTIFICATION DIVISION must come first, then the ENVIRONMENT DIVISION, then the DATA DIVISION, and finally the PROCEDURE DIVISION. So to become an expert COBOL programmer, you must memorize: "IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE".

To memorize that easily, memorize this easy sentence: "I enjoy data processing". In that sentence, the words begin with the letters "I E D P" ó and so do the four COBOL divisions.

In the program, each blank that you fill is called a paragraph. In the first paragraph, write the programís name; in the next paragraph, write your own name; in the next two paragraphs, write the computerís name; and in the last paragraph, write what you want the computer to do. The first paragraph is called the PROGRAM-ID; the next paragraph is called the AUTHOR; the next two paragraphs are called the SOURCE-COMPUTER and the OBJECT-COMPUTER; and the last paragraph is called the MAIN-ROUTINE.

Each paragraph is indented. To indent on PDP and Eclipse computers, type a controlled I.

In COBOL, the only important punctuation mark is the period. When writing a simple program, put a period at the end of each line. COBOL never requires commas or semicolons.

Donít forget the hyphens! Put a hyphen in PROGRAM-ID, SOURCE-COMPUTER, OBJECT-COMPUTER, and MAIN-ROUTINE.

Use correct spacing.

Right: DISPLAY "BURP".

Wrong: DIS PLAY "BURP".

Wrong: DISPLAY"BURP".

Wrong: DISPLAY "BURP" .

In the paragraphs that are called SOURCE-COMPUTER and OBJECT-COMPUTER, you must type the computerís name correctly. Here are the correct names for some famous computers:

IBM-360.

IBM-370.

PDP-11.

DECSYSTEM-10. (It means you have a PDP-10.)

DECSYSTEM-20. (It means you have a PDP-20.)

ECLIPSE C300.

6600. (It means you have a CDC 6600.)

Donít forget the hyphens!

Go ahead: try writing your own COBOL program. In the PROCEDURE DIVISION, remember to say DISPLAY:

BASIC: PRINT "LIFE STINKS"

PASCAL: WRITELN('LIFE STINKS');

COBOL: DISPLAY "LIFE STINKS".

Old-fashioned computers On IBM computers, instead of using quotation marks, you must use apostrophes.

Most computers: DISPLAY "LIFE STINKS".

IBM computers: DISPLAY 'LIFE STINKS'.

On IBM and CDC computers, you must indent the entire program, like this (each  represents a blank space):

7 blanks 8th column

¯

IDENTIFICATION DIVISION.

PROGRAM-ID.

HARRY.

7 blanks 4 blanks

 

11 blanks 12th column

On those computers, each paragraph begins in column 12, and must not go farther to the right than column 72. (The computer ignores everything in columns 73-80.)

Abridgments

If youíre lazy, you can omit the AUTHOR paragraph:

Complete IDENTIFICATION DIVISION Abridged version

IDENTIFICATION DIVISION. IDENTIFICATION DIVISION.

PROGRAM-ID. PROGRAM-ID.

HARRY. HARRY.

AUTHOR.

RUSS WALTER.

If youíre lazy, and youíre using a PDP-10, PDP-20, Eclipse, or IBM computer, you can omit the CONFIGURATION SECTION, SOURCE-COMPUTER, and OBJECT-COMPUTER:

Complete ENVIRONMENT DIVISION Abridged version

ENVIRONMENT DIVISION. ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name.

If youíre very lazy, and youíre using a PDP-10 or PDP-20 computer, you can abridge the program even further, so that the entire program looks like this:

IDENTIFICATION DIVISION.

PROCEDURE DIVISION.

DISPLAY "LIFE STINKS".

STOP RUN.

But if youíre working for a big company, your employer will expect you not to be lazy: if youíre lazy, you get fired!

Fancy displays

Youíve seen that every COBOL program consists of four divisions: IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE. The most important division is the PROCEDURE DIVISION. Letís look at it more closely.

Hereís a cute PROCEDURE DIVISION:

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "BILLIE AND BONNIE".

DISPLAY "BURP".

STOP RUN.

It makes the computer display:

BILLIE AND BONNIE

BURP

Another example:

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "FLU" "SHED".

STOP RUN.

The computer will display FLU and SHED on the same line:

FLUSHED

PERFORM

Letís make the computer display "I LOVE YOU", then display "I HATE YOU" six times, then display "I AM CONFUSED", like this:

I LOVE YOU

I HATE YOU

I HATE YOU

I HATE YOU

I HATE YOU

I HATE YOU

I HATE YOU

I AM CONFUSED

Hereís the PROCEDURE DIVISION:

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "I LOVE YOU".

PERFORM EXPRESS-THE-HATRED 6 TIMES.

DISPLAY "I AM CONFUSED".

STOP RUN.

EXPRESS-THE-HATRED.

DISPLAY "I HATE YOU".

That PROCEDURE DIVISION consists of two paragraphs. The first paragraph is called the MAIN-ROUTINE. The second paragraph is called EXPRESS-THE-HATRED.

The computer obeys the first paragraph: it displays "I LOVE YOU", then performs the EXPRESS-THE-HATRED 6 times, then displays "I AM CONFUSED", and finally stops.

When you invent your own PROCEDURE DIVISION, the first paragraph should be called the MAIN-ROUTINE; for the other paragraphs underneath, invent whatever names you like (such as EXPRESS-THE-HATRED). A paragraphís name should be hyphenated, and should contain no more than 30 characters. (EXPRESS-THE-HATRED contains 18 characters, so itís okay.) The first paragraph is the main routine, the paragraphs underneath are subroutines. The bottom line of the main routine should say STOP RUN. In the middle of the main routine, you should say to PERFORM the subroutines.

In the example above, the main routine says to PERFORM the EXPRESS-THE-HATRED subroutine 6 times. If youíd like to see more hatred, say 100 times instead of 6:

PERFORM EXPRESS-THE-HATRED 100 TIMES.

If youíd rather see just a little hatred, say just ó

PERFORM EXPRESS-THE-HATRED 1 TIMES.

or say just:

PERFORM EXPRESS-THE-HATRED.

Letís make the computer display:

I KNOW THAT

YOU ARE DRIVING

ME CRAZY

YOU ARE DRIVING

ME CRAZY

YOU ARE DRIVING

ME CRAZY

YOU ARE DRIVING

ME CRAZY

YOU ARE DRIVING

ME CRAZY

AND YET I LOVE YOU

 

Hereís the PROCEDURE DIVISION:

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "I KNOW THAT".

PERFORM ACT-AS-IF-GOING-INSANE 5 TIMES.

DISPLAY "AND YET I LOVE YOU".

STOP RUN.

ACT-AS-IF-GOING-INSANE.

DISPLAY "YOU ARE DRIVING".

DISPLAY "ME CRAZY".

Letís make the computer display:

THE ASTRONAUTS GO

UP

UP

UP

UP

UP

UP

UP

AND THEN THEY COME

DOWN

DOWN

DOWN

DOWN

DOWN

DOWN

DOWN

Hereís the PROCEDURE DIVISION:

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "THE ASTRONAUTS GO".

PERFORM SHOW-THE-ASTRONAUTS-RISING 7 TIMES.

DISPLAY "AND THEN THEY COME".

PERFORM SHOW-THE-ASTRONAUTS-FALLING 7 TIMES.

STOP RUN.

SHOW-THE-ASTRONAUTS-RISING.

DISPLAY "UP".

SHOW-THE-ASTRONAUTS-FALLING.

DISPLAY "DOWN".

Letís make the computer display:

YOU ARE VERY SWEET

HA-HA-HA!

HO-HO-HO!

YOU CANNOT BE BEAT

HA-HA-HA!

HO-HO-HO!

YOUR LIPS ARE LIKE WINE

HA-HA-HA!

HO-HO-HO!

BUT YOU SMELL LIKE TURPENTINE

HA-HA-HA!

HO-HO-HO!

YOU STINK!

Hereís the PROCEDURE DIVISION:

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "YOU ARE VERY SWEET".

PERFORM LAUGH-A-LOT.

DISPLAY "YOU CANNOT BE BEAT".

PERFORM LAUGH-A-LOT.

DISPLAY "YOUR LIPS ARE LIKE WINE".

PERFORM LAUGH-A-LOT.

DISPLAY "BUT YOU SMELL LIKE TURPENTINE".

PERFORM LAUGH-A-LOT.

DISPLAY "YOU STINK!".

STOP RUN.

LAUGH-A-LOT.

DISPLAY "HA-HA-HA!".

DISPLAY "HO-HO-HO!".

Variables

Like other languages, COBOL lets you use variables. The name of a variable can be a letter (like X or Y) or a hyphenated phrase (like NUMBER-OF-BULLIES-I-SQUIRTED). A hyphenated phrase can have up to 30 characters.

To use a variable, you must describe it in the data division, as in this example:

IDENTIFICATION DIVISION.

PROGRAM-ID.

JUNK.

AUTHOR.

RUSS WALTER. Instead of "RUSS WALTER", write your own name.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

DECSYSTEM-20. Instead of "DECSYSTEM-20", write your computerís name.

OBJECT-COMPUTER.

DECSYSTEM-20.

DATA DIVISION.

WORKING-STORAGE SECTION. In the DATA DIVISION, say WORKING-STORAGE SECTION.

01 K PICTURE IS XXX.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE "HER" TO K.

DISPLAY "PUS" K "S".

STOP RUN.

In the DATA DIVISIONís WORKING-STORAGE SECTION, the "01 K" says K is a variable. The PICTURE IS XXX says K is a string that has three characters; each X stands for a character. In the PROCEDURE DIVISION, the first sentence makes K become this 3-character string: "HER". The next sentence makes the computer display:

PUSHERS

When you type that program, make sure you put a hyphen between WORKING and STORAGE. If you forget the hyphen, the computer will act crazy, and will say that your program contains many, many errors.

Suppose you change the picture from XXX to XX, so that the DATA DIVISION and PROCEDURE DIVISION look like this:

DATA DIVISION

WORKING-STORAGE SECTION

01 K PICTURE IS XX.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE "HER" TO K.

DISPLAY "PUS" K "S".

STOP RUN.

K will be a string having only two characters. When the computer tries to move "HER" to K, only the first two characters of "HER" will fit, so K will be "HE". The computer will display:

PUSHES

Suppose you change the picture to XXXX. K will have four characters. When the computer tries to move "HER" to K, it needs to move a fourth character also, so it moves a blank space at the end, which makes K be "HER ". The computer will display:

PUSHER S

This program shows how revolutionary politics lead to revolutionary clothing:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 FIRST-PRESIDENT PICTURE IS XXXXXXXXXX.

01 CLEANING-METHOD PICTURE IS XXXX.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE "WASHINGTON" TO FIRST-PRESIDENT.

MOVE FIRST-PRESIDENT TO CLEANING-METHOD.

DISPLAY CLEANING-METHOD " MY BLUE JEANS".

STOP RUN.

Above the DATA DIVISION, write your own IDENTIFICATION DIVISION and ENVIRONMENT DIVISION. The DATA DIVISION says FIRST-PRESIDENT will be a string having ten characters, and CLEANING-METHOD will be a string having four. The first sentence of the MAIN-ROUTINE makes FIRST-PRESIDENT be "WASHINGTON". The next sentence tries to move "WASHINGTON" to CLEANING-METHOD; but because of CLEANING-METHODís picture, the computer moves "WASH" instead. The computer will display:

WASH MY BLUE JEANS

To make sure you understand the word MOVE, examine this example:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 C PICTURE IS XXX.

01 D PICTURE IS XXX.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE "CAT" TO C. C becomes "CAT".

MOVE C TO D. D becomes "CAT".

DISPLAY C. Since C is still "CAT", the computer displays "CAT".

MOVE "HE" TO C. C becomes "HE ".

DISPLAY C "BLED". The computer displays "HE BLED".

STOP RUN.

COBOL allows abbreviations. You can say PIC instead of PICTURE IS, and X(7) instead of XXXXXXX.

Numeric variables

Letís make the computer add 53 and 4, and display the sum, 57. Hereís how:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 K PIC 99.

PROCEDURE DIVISION.

MAIN-ROUTINE.

COMPUTE K = 53 + 4.

DISPLAY K.

STOP RUN.

The PIC 99 says K is a number having two digits. (Each 9 stands for a digit.) The MAIN-ROUTINE sets K equal to 53 + 4, which is 57. The computer will display:

57

In the COMPUTE statement, the equal sign and the plus sign must be surrounded by spaces.

Right: COMPUTE K = 53 + 4.

Wrong: COMPUTE K=53+4.

Wrong: COMPUTE K = 53+4.

If you change the picture to 999, K will be a number having three digits. It will be 057 instead of 57. The computer will display:

057

(Exception: PDP-10 and PDP-20 computers are lazy; they donít bother to display the 0 at the left; they display just 57.)

If you change the picture to 9, K will be a number having only one digit. so K will not be 57. The computer will not display the correct sum.

Like FORTRAN, COBOL uses these operators:

Operator Meaning

+ plus

- minus

* times

/ divided by

** exponent

You must put a blank space before and after each operator:

Right Wrong

53 + 4 53+4

7 ** 2 7**2

- J + 3 -J + 3

Like other computer languages, COBOL lets you use parentheses. Do not put a space after a left parenthesis:

Right

COMPUTE K = I * (- J + 3)

­ ­ ­ ­

no space spaces

You can use these shortcuts:

Sentence Shortcut

COMPUTE A = A + 7. ADD 7 TO A.

COMPUTE B = B - 4. SUBTRACT 4 FROM B.

Operators are allowed only in sentences that say COMPUTE, IF, UNTIL, or WHEN.

Allowed: COMPUTE A = 2 * 3.

Not allowed: DISPLAY 2 * 3.

Not allowed: MOVE 2 * 3 TO A.

Not allowed: ADD 2 * 3 TO A.

Decimals

You can use decimals:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 K PIC 9999V99.

PROCEDURE DIVISION.

MAIN-ROUTINE.

COMPUTE K = 2.208 + 4.109.

DISPLAY K.

STOP RUN.

The PIC 9999V99 says K is a number having four digits, followed by a decimal point, followed by two digits. (The V stands for the decimal point.) The MAIN-ROUTINE tries to set K equal to 2.208 + 4.109, which is 6.317; but because of Kís picture, I will be 0006.31 instead. So the computer should display 0006.31.

PDP-10 and PDP-20 computers donít bother to display the zeros: they display 6.31. PDP-11, IBM, CDC, and Eclipse computers donít bother to display the decimal point: they display 000631.

You can change that program by saying ROUNDED:

COMPUTE K ROUNDED = 2.208 + 4.109.

The computer will find the sum (6.317), and round it so K is 0006.32 instead of 0006.31.

If Jís picture is 99, saying "COMPUTE J = 200 / 3" makes J be 66. Saying "COMPUTE J ROUNDED = 200 / 3" makes J be 67.

Which is better: saying "COMPUTE X = 4.9" or "MOVE 4.9 TO X"? You should usually say "MOVE 4.9 TO X", because the computer handles it more quickly. But MOVE cannot round; so if you want to round, say COMPUTE.

MOVE can do strange things:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 L PIC 999V99.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE 16725.048 TO L.

DISPLAY L.

STOP RUN.

Lís picture makes the computer move the three digits just left of the decimal point and the two digits just right of it. L will be 725.04.

Negatives

You can use negative numbers:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 K PIC S9999.

PROCEDURE DIVISION.

MAIN-ROUTINE.

COMPUTE K = 100 - 367.

DISPLAY K.

STOP RUN.

In Kís picture, the S stands for a sign (which can be plus or minus). K will be -0267. The computer should display -0267.

PDP-10 and PDP-20 computers donít bother to display the zero: they display -267. Some other computers display the minus sign on top of the right digit, like this: 0267. On many computers, the minus combines with the 7 and forms a P, like this: 026P.

If you omit the S from Kís picture, K will be 0267 instead of -0267.

To find the negative of a power, use parentheses:

COMPUTE A = - (3 ** 2).

If you omit the parentheses, the computer will get the wrong answer.

ACCEPT

Hereís how to translate the BASIC word INPUT into PASCAL and COBOL.

BASIC: INPUT K

PASCAL: READ(K);

COBOL: ACCEPT K.

Letís look at a COBOL example:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 K PIC XXX.

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "THIS PROGRAM WANTS YOU TO TYPE SOMETHING".

ACCEPT K.

DISPLAY K.

STOP RUN.

The computer displays:

THIS PROGRAM WANTS YOU TO TYPE SOMETHING

The statement ACCEPT K makes the computer wait for you to type something. If you type ó

FIGHT

the computer will try to move "FIGHT" to K; but since Kís picture is XXX, K will be "FIG". The computer will display:

FIG

If you type ó

ME

the computer will try to move "ME" to K; since Kís picture is XXX, K will be "ME ". The computer will display:

ME

Suppose a program says L PIC 999 and ACCEPT L. If you input ó

4

a PDP-10 or PDP-20 computer will make L be 004, but an IBM or CDC computer will make L be 400.

Suppose a program says M PIC S9999V99 and ACCEPT M. If you want M to be -0034.27, hereís what to input.Ö

On PDP-10 & PDP-20 computers: -0034.27 or -34.27

On IBM and CDC computers: 003427

Editing

The computer can edit the output:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 K PIC XXXBXXX.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE "HITHER" TO K.

DISPLAY K.

STOP RUN.

In Kís picture, B means a blank space. So when the computer moves "HITHER", K becomes "HIT HER". The computer will display:

HIT HER

This program displays the Boston Computer Societyís phone number:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 PHONE-NUMBER PIC 999B9999.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE 3678080 TO PHONE-NUMBER.

DISPLAY PHONE-NUMBER.

STOP RUN.

When the computer moves 3678080, PHONE-NUMBER becomes "367 8080". The computer will display:

367 8080

This program is of historical importance:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 K PIC 9B9B9999.

PROCEDURE DIVISION.

MAIN-ROUTINE.

COMPUTE K = 741775 + 1.

DISPLAY "THE DECLARATION OF INDEPENDENCE WAS SIGNED ON " K.

STOP RUN.

The computer will display:

THE DECLARATION OF INDEPENDENCE WAS SIGNED ON 7 4 1776

Using B to insert a blank is called editing. Youíve learned four kinds of variables:

Kind of variable Symbols in picture

string X

edited string X B

number 9 V S

edited number 9 B Z , $ * . + - DB CR

You can use these pictures for editing numbers:

What K will be What K will be

Kís picture Meaning if you move 50320 to K if you move 0 to K

B99999999 a blank then eight digits " 00050320" " 00000000"

ZZZZZZZZZ blanks then digits " 50320" " "

$,$$$,$$$ put $ before the digits " $50,320" " "

*,***,*** stars instead of blanks "***50,320" "*********"

To edit decimals, put a decimal point in the picture.

Kís picture If you move 50320.6 to K If you move .04 to K If you move 0 to K

Z,ZZZ,ZZZ.ZZ " 50,320.60" " .04" " "

$,$$$,$$$.$$ " $50,320.60" " $.04" " "

*,***,***.** "***50,320.60" "*********.04" "*********.**"

With those pictures, if you move 0 to K the computer doesnít put any digits in K. To guarantee that K contains a digit, put 9 in the picture:

Kís picture If you move 50320.6 to K If you move .04 to K If you move 0 to K

Z,ZZZ,ZZ9.99 " 50,320.60" " 0.04" " 0.00"

$,$$$,$$9.99 " $50,320.60" " $0.04" " $0.00"

*,***,**9.99 "***50,320.60" "********0.04" "********0.00"

To edit negative numbers, use +, -, DB, or CR.

Kís picture Meaning If you move -2.6 to K If you move 2.6 to K

ZZZ.ZZ+ put - or + afterwards " 2.60-" " 2.60+"

ZZZ.ZZ- put - or blank afterwards " 2.60-" " 2.60 "

ZZZ.ZZDB if negative, put DB (for debit) " 2.60DB" " 2.60 "

ZZZ.ZZCR if negative, put CR (for credit) " 2.60CR" " 2.60 "

For fancier pictures, replace the Z by $, *, or 9.

Hereís how to put the sign before the digits:

Kís picture Meaning If you move -2.6 to K If you move 2.6 to K

+++.++ put - or + before digits " -2.60" " +2.60"

---.-- - or blank before digits " -2.60" " 2.60"

Here are the differences between numbers and edited numbers:

Number Edited number

how to put decimal point in the picture V .

how to put a sign in the picture S +, -, DB, or CR

how to fill up most of the picture 9 9, Z, $, *, +, or -

what the value should be intermediate result the final answer to be displayed

in long calculation

If the valueís called K, can you say ACCEPT K? yes no

If the valueís called K, can you use K in further yes no

computations (such as COMPUTE L = K + 1)?

What happens if you try to DISPLAY the value? "0", ".", and "-" displays correctly

might look wrong

DECIMAL-POINT IS COMMA

Some Europeans write commas instead of decimal points, and write decimal points instead of commas.

United States and England: 5,243,794.95

France and Italy: 5.243.794,95

Germany: 5 243 794,95

To write a COBOL program for a Frenchman, an Italian, or a German, make three changes.Ö

Change #1 Insert this line:

DECIMAL-POINT IS COMMA.

Put that line in a SPECIAL-NAMES paragraph, at the end of the ENVIRONMENT DIVISIONís CONFIGURATION SECTION:

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

SPECIAL-NAMES.

DECIMAL-POINT IS COMMA.

Change #2 Type all numbers in French-Italian-German notation. So instead of typing ó

MOVE 5243794.95 TO K

type:

MOVE 5243794,95 TO K

Change #3 Use French-Italian-German notation in pictures for edited numbers.

For a Frenchman or an Italian: K PIC Z.ZZZ.ZZZ,ZZ

For a German: K PIC ZBZZZBZZZ,ZZ

Those are the only changes Europeans make. They still put a period at the end of every sentence, and still use English COBOL words such as MOVE and DISPLAY.

American COBOL: DISPLAY "HELLO, STUPID".

French COBOL: DISPLAY "BONJOUR, BETE".

German COBOL: DISPLAY "GUTEN TAG, DUMMKOPF".

Logic

COBOL lets you do logic.

IF

Like other computer languages, COBOL uses the word IF:

BASIC PASCAL COBOL

INPUT i READ(I); ACCEPT I.

IF i > 5 THEN IF I>5 THEN IF I > 5

j = 80 BEGIN; MOVE 80 TO J

k = 90 J:=80; MOVE 90 TO K.

END IF K:=90;

END;

COBOL also lets you say ELSE:

BASIC PASCAL COBOL

INPUT i READ(I); ACCEPT I.

IF I > 5 THEN IF I>5 THEN IF I > 5

j = 80 BEGIN; MOVE 80 TO J

k = 90 J:=80; MOVE 90 TO K

ELSE K:=90; ELSE

j = 30 END MOVE 30 TO J

k = 50 ELSE MOVE 50 TO K.

END IF BEGIN;

J:=30;

K:=50;

END;

In COBOL, when you write the IF statement, do not put a period at the end of every line; instead, put the period just at the end of the entire IF idea.

Notice that I indented the word MOVE. The indentation is optional, but is a good habit. To indent on PDP and Eclipse computers, type a controlled I; to indent on IBM and CDC computers, press the space bar several times.

COBOL uses these IF lines:

IF line Meaning

IF I = 5 If I is equal to 5

IF I NOT = 5 If I is not equal to 5

IF I > 5 If I is greater than 5

IF I NOT > 5 If I is not greater than 5

IF I < 5 If I is less than 5

IF I NOT < 5 IF I is not less than 5

You can use the words AND and OR and abbreviate:

IF line: IF J > 1 AND J < 100

Abbreviation: IF J > 1 AND < 100

IF line: IF K < -3 OR K = 6 OR K = 9 OR K = 12 OR K > 50

Abbreviation: IF K < -3 OR = 6 OR 9 OR 12 OR > 50

You can say this:

IF AGE < 13

DISPLAY "CHILD"

ELSE IF AGE < 20

DISPLAY "TEENAGER"

ELSE IF AGE < 40

DISPLAY "YOUNG ADULT"

ELSE IF AGE < 60

DISPLAY "MIDDLE-AGED"

ELSE

DISPLAY "SENIOR CITIZEN".

It means, "If AGE is less than 13, display the word CHILD; if not less than 13, do the following: if AGE is less than 20, display the word TEENAGER; if not less than 20, do the following: if AGE is less than 40,Ö" and so on. The computer will display just one phrase, to describe the personís AGE.

UNTIL

You can say UNTIL:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 I PIC 999.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE 5 TO I.

PERFORM FIDDLE-WITH-I UNTIL I > 100.

STOP RUN.

FIDDLE-WITH-I.

DISPLAY I.

COMPUTE I = I * 2.

The computer will perform FIDDLE-WITH-I repeatedly, until I > 100. The computer will display 5, 10, 20, 40, and 80. It will not display 160.

Here are the details. When the computer encounters PERFORM FIDDLE-WITH-I UNTIL I > 100, it checks whether I > 100. If I > 100, the computer proceeds to the next statement (the STOP RUN); but if I is not greater than 100, the computer performs FIDDLE-WITH-I and then re-executes the statement PERFORM FIDDLE-WITH-I UNTIL I > 100.

To translate a BASIC "FOR...NEXT loop" into COBOL, say "PERFORM":

BASIC COBOL

FOR i = 5 TO 17 PROCEDURE DIVISION.

PRINT i MAIN-ROUTINE.

NEXT PERFORM DISPLAY-IT

VARYING I FROM 5 BY 1 UNTIL I > 17.

STOP RUN.

DISPLAY-IT.

DISPLAY I.

 

 

FOR i = 5 TO 17 STEP 3 PROCEDURE DIVISION.

PRINT i MAIN-ROUTINE.

NEXT PERFORM DISPLAY-IT

VARYING I FROM 5 BY 3 UNTIL I > 17.

STOP RUN.

DISPLAY-IT.

DISPLAY I.

 

 

FOR i = 5 TO 17 PROCEDURE DIVISION.

FOR j = 1 TO 3 MAIN-ROUTINE.

PRINT i, j PERFORM DISPLAY-IT

NEXT VARYING I FROM 5 BY 1 UNTIL I > 17

NEXT AFTER J FROM 1 BY 1 UNTIL J > 3.

STOP RUN.

DISPLAY-IT.

DISPLAY I J.

GO TO

Like other computer languages, COBOL lets you say GO TO. In COBOL, put a space between GO and TO. (In PASCAL, you do not put a space between GO and TO.)

For example, instead of saying STOP RUN, you can say GO TO MAIN-ROUTINE:

PROCEDURE DIVISION.

MAIN-ROUTINE.

DISPLAY "WHEATIES".

DISPLAY "ARE WONDERFUL".

GO TO MAIN-ROUTINE.

The computer will display "WHEATIES" and "ARE WONDERFUL", repeatedly:

WHEATIES

ARE WONDERFUL

WHEATIES

ARE WONDERFUL

WHEATIES

ARE WONDERFUL

etc.

The main routine can consist of two paragraphs, called MAIN-ROUTINE-BEGINNING and MAIN-ROUTINE-LOOP:

PROCEDURE DIVISION.

MAIN-ROUTINE-BEGINNING.

DISPLAY "PLEASE".

MAIN-ROUTINE-LOOP.

DISPLAY "KISS".

DISPLAY "ME".

GO TO MAIN-ROUTINE-LOOP.

The computer will display PLEASE, then repeatedly display KISS and ME:

PLEASE

KISS

ME

KISS

ME

KISS

ME

etc.

The main routine can consist of three paragraphs, called MAIN-ROUTINE-BEGINNING, MAIN-ROUTINE-LOOP, and MAIN-ROUTINE-ENDING:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 HUMAN-RESPONSE PIC XXX.

PROCEDURE DIVISION.

MAIN-ROUTINE-BEGINNING.

DISPLAY "I WILL RECITE A SHORT POEM".

MAIN-ROUTINE-LOOP.

DISPLAY " ".

DISPLAY "YOUR NOSE".

DISPLAY "BLOWS".

DISPLAY " ".

DISPLAY "WOULD YOU LIKE TO HEAR THE POEM AGAIN?".

ACCEPT HUMAN-RESPONSE.

IF HUMAN-RESPONSE = "YES"

GO TO MAIN-ROUTINE-LOOP.

MAIN-ROUTINE-ENDING.

DISPLAY "YOU HAVE BEEN A GREAT AUDIENCE".

STOP RUN.

When you run that program, the computer says:

I WILL RECITE A SHORT POEM

Then it recites the poem:

YOUR NOSE

BLOWS

Then it asks:

WOULD YOU LIKE TO HEAR THE POEM AGAIN?

If you answer YES, the computer repeats the poem, then asks whether youíd like to hear it a third time. If you answer YES again, the computer recites the poem a third time, then asks whether youíd like to hear it a fourth time. The computer recites the poem repeatedly, until you finally stop answering YES. Then the computer says ó

YOU HAVE BEEN A GREAT AUDIENCE

and stops.

In that program, if you donít answer YES, the computer doesnít repeat the poem. So if you donít answer YES, the computer acts as if you said NO. The following version is an improvement; if you donít answer YES, and you donít say NO, the computer asks the question again:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 HUMAN-RESPONSE PIC XXX.

PROCEDURE DIVISION.

MAIN-ROUTINE-BEGINNING.

DISPLAY "I WILL RECITE A SHORT POEM".

MAIN-ROUTINE-LOOP.

DISPLAY " ".

DISPLAY "YOUR NOSE".

DISPLAY "BLOWS".

DISPLAY " ".

PERFORM GET-HUMAN-RESPONSE.

IF HUMAN-RESPONSE = "YES"

GO TO MAIN-ROUTINE-LOOP.

MAIN-ROUTINE-ENDING.

DISPLAY "YOU HAVE BEEN A GREAT AUDIENCE".

STOP RUN.

GET-HUMAN-RESPONSE.

DISPLAY "WOULD YOU LIKE TO HEAR THE POEM AGAIN?".

ACCEPT HUMAN-RESPONSE.

IF HUMAN-RESPONSE NOT = "YES" AND NOT = "NO"

DISPLAY "PLEASE SAY YES OR NO!"

GO TO GET-HUMAN-RESPONSE.

GO TO resembles PERFORM. Hereís the difference between GO TO and PERFORM.Ö

To go to a different routine, say PERFORM.

To go to a different paragraph in the same routine, say GO TO.

For example, suppose you want to go from MAIN-ROUTINE-BEGINNING to FUNNY-SUBROUTINE; since youíre going to a different routine, say PERFORM.

Suppose you want to go from MAIN-ROUTINE-BEGINNING to MAIN-ROUTINE-ENDING; since youíre going to a different paragraph in the same routine, say GO TO.

THRU

Like the main routine, a subroutine can consist of several paragraphs. For example, subroutine FUNNY-FACE can consist of three paragraphs, called FUNNY-FACE-BEGINNING, FUNNY-FACE-LOOP, and FUNNY-FACE-ENDING. To make the computer do the entire subroutine, say:

PERFORM FUNNY-FACE-BEGINNING THRU FUNNY-FACE-ENDING.

Data files

To manipulate a data file whose name is POEM, fill in the blanks:

IDENTIFICATION DIVISION.

PROGRAM-ID.

The program's name.

AUTHOR

Your name.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT POEM-FILE ASSIGN TO the file's location.

DATA DIVISION.

FILE SECTION.

FD POEM-FILE how the file is labeled.

01 POEM-LINE PIC a picture of a line of the file.

WORKING-STORAGE SECTION.

A description of each variable that's not in the file.

PROCEDURE DIVISION.

MAIN-ROUTINE.

OPEN output or input POEM-FILE.

What you want to do to the file.

CLOSE POEM-FILE.

STOP RUN.

The four divisions

Like every COBOL program, that outline consists of four divisions: the IDENTIFICATION DIVISION, the ENVIRONMENT DIVISION, the DATA DIVISION, and the PROCEDURE DIVISION. Letís look at each division.

IDENTIFICATION DIVISION The IDENTIFICATION DIVISION consists of two paragraphs: the PROGRAM-ID and the AUTHOR. For the PROGRAM-ID, fill in the programís name, which must be different from the name of the file. Since the name of the file is POEM, the name of the program must not be POEM. If youíre lazy, you can omit the AUTHOR.

ENVIRONMENT DIVISION The ENVIRONMENT DIVISION consists of two sections: the CONFIGURATION SECTION and the INPUT-OUTPUT SECTION.

The CONFIGURATION SECTION consists of two paragraphs: the SOURCE-COMPUTER and the OBJECT-COMPUTER. On CDC and PDP-11 computers, the CONFIGURATION SECTION is required; but on IBM, Eclipse, PDP-10, and PDP-20 computers, the entire CONFIGURATION SECTION is optional, so you can abridge the ENVIRONMENT DIVISION:

ENVIRONMENT DIVISION.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT POEM-FILE ASSIGN TO the file's location.

The INPUT-OUTPUT SECTION consists of just one paragraph, which is the FILE-CONTROL. The FILE-CONTROL paragraph consists of a sentence that says SELECT, then the fileís name (POEM-FILE), then ASSIGN TO, and finally a blank (which you must fill in, and which tells the fileís location).

What do you put in that blank? The answer depends on the fileís location. Is the file on a disk? On punched cards? Or on paper produced by the printer? Hereís what to put in the blank, for various computers:

Printer Card reader Disk

Eclipse PRINTER "$CDR" "POEM"

CDC OUTPUT INPUT POEM

PDP-11 "LP:" "CR:" "DK":

PDP-10, PDP-20 LPT CDR DSK RECORDING MODE ASCII

IBM using OS UT-S-POEM UT-S-POEM UT-S-POEM

IBM using DOS SYS006-UR-1403-S SYS005-UR-2540R-S SYS020-UT-3330-S-POEM

DATA DIVISION The DATA DIVISION consists of two sections: the FILE SECTION and the WORKING-STORAGE SECTION.

At the beginning of the FILE SECTION, say FD (which means "File Description"). To the right of the FD, say POEM-FILE, and then fill in the blank, which tells how the file is labeled:

Computer What to put in the blank

CDC LABEL RECORDS ARE OMITTED

PDP-11: disk LABEL RECORDS ARE STANDARD VALUE OF ID "POEM"

PDP-11: printer or card reader LABEL RECORDS ARE OMITTED

PDP-10, PDP-20 VALUE ID "POEM"

IBM OS LABEL RECORDS ARE STANDARD

IBM DOS: disk LABEL RECORDS ARE STANDARD

IBM DOS: printer or card reader LABEL RECORDS ARE OMITTED

On PDP-10 and PDP-20 computers, put enough blank spaces (þ) after POEM so that the string has 9 characters. On Eclipse computers, do not fill in the blank; just say:

FD POEM-FILE.

Underneath the line that says FD, you must say 01. The 01 line includes a picture of a line of the file. For example, if a line of the file is an 80-character string, the 01 line should say:

01 POEM-LINE PIC X(80).

The WORKING-STORAGE SECTION describes each variable thatís not in the file.

PROCEDURE DIVISION The PROCEDURE DIVISIONís MAIN-ROUTINE should begin with the word OPEN, and end with the words CLOSE and STOP RUN.

In the OPEN statement, you can say either ó

OPEN OUTPUT POEM-FILE.

or:

OPEN INPUT POEM-FILE.

If you say OPEN OUTPUT POEM-FILE, the computer will output to the POEM-FILE; so it will copy information from the RAM to the POEM-FILE. If you say OPEN INPUT POEM-FILE, the computer will input from the POEM-FILE; so it will copy information from the POEM-FILE to the RAM.

In the PROCEDURE DIVISION, when you fill in the blank about "what you want to do to the file", you must say either WRITE POEM-LINE or READ POEM-FILE. If the file is OPEN OUTPUT (which means youíre copying from the RAM to the file), say WRITE POEM-LINE; if the file is OPEN INPUT (which means youíre copying from the file to the RAM), say READ POEM-FILE.

 

Writing

Hereís a poetic masterpiece:

CANDY IS DANDY

BUT LIKKER IS QUIKKER

It was composed by the famous poet Ogden Nash.

This program makes the computer write that masterpiece onto a disk, and make the masterpiece become a file named POEM:

Program Meaning

IDENTIFICATION DIVISION.

PROGRAM-ID.

CANDY. This program is named CANDY.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT POEM-FILE ASSIGN TO the file's location. This program uses a file called POEM.

DATA DIVISION.

FILE SECTION.

FD POEM-FILE how the file is labeled.

01 POEM-LINE PIC X(21). Make each line have 21 characters, like this:

CANDY IS DANDY

PROCEDURE DIVISION. BUT LIKKER IS QUIKKER

MAIN-ROUTINE.

OPEN OUTPUT POEM-FILE. Prepare to output to POEM-FILE.

MOVE "CANDY IS DANDY" TO POEM-LINE. Make POEM-LINE be "CANDY IS DANDY".

WRITE POEM-LINE. Copy that POEM-LINE to the file.

MOVE "BUT LIKKER IS QUIKKER" TO POEM-LINE. Make POEM-LINE become this new string: "BUT LIKKER IS QUIKKER".

WRITE POEM-LINE. Copy that new POEM-LINE to the file.

CLOSE POEM-FILE. Finish using POEM-FILE.

STOP RUN. Stop running this program.

That program doesnít require a WORKING-STORAGE SECTION, so I omitted it. Since I was lazy, I also omitted the AUTHOR paragraph.

When you run that program, the computer will create a file on disk. The file will be called POEM. It will contain this message:

CANDY IS DANDY

BUT LIKKER IS QUIKKER

Reading

This program reads the file named POEM and displays it on your screen:

IDENTIFICATION DIVISION.

PROGRAM-ID.

READER.

The ENVIRONMENT DIVISION and DATA DIVISION are the same as the previous programís.

PROCEDURE DIVISION.

MAIN-ROUTINE-BEGINNING.

OPEN INPUT POEM-FILE. Find the POEM file on the disk, and prepare to input from it.

MAIN-ROUTINE-LOOP.

READ POEM-FILE AT END GO TO MAIN-ROUTINE-ENDING. Read a line from POEM-FILE; if there are no more lines, go to the next paragraph.

DISPLAY POEM-LINE. Display that line, so you see it on your screen.

GO TO MAIN-ROUTINE-LOOP. Go back to read another line.

MAIN-ROUTINE-ENDING.

DISPLAY "THAT WAS THE WHOLE POEM". Display "THAT WAS THE WHOLE POEM" on your screen.

CLOSE POEM-FILE. Finish using POEM-FILE.

STOP RUN. Stop running this program.

In the MAIN-ROUTINE-LOOP, the first line means: try to READ a line from POEM-FILE; but if a line cannot be read (because the file has ended), go to MAIN-ROUTINE-ENDING instead.

The READ statement differs from the WRITE statement in two ways:

A WRITE statement mentions a LINE, but a READ statement mentions a FILE.

A READ statement must contain the words AT END.

Counting

This program reads a file called POEM, counts how many lines are in it, and displays the count:

IDENTIFICATION DIVISION.

PROGRAM-ID.

COUNTS.

The ENVIRONMENT DIVISION is same as previous programís.

DATA DIVISION.

FILE SECTION.

FD POEM-FILE how the file is labeled.

01 POEM-LINE PIC X(21). Assume each POEM-LINE has 21 characters.

WORKING-STORAGE SECTION.

01 COUNT-OF-HOW-MANY-LINES PIC 99. Assume the count is a two-digit number,

so assume POEM has less than 100 lines.

PROCEDURE DIVISION.

MAIN-ROUTINE-BEGINNING.

OPEN INPUT POEM-FILE. Find the POEM file on the disk.

MOVE 0 TO COUNT-OF-HOW-MANY-LINES. Start the count at 0.

MAIN-ROUTINE-LOOP.

READ POEM-FILE AT END GO TO MAIN-ROUTINE-ENDING. Read a line from POEM-FILE.

ADD 1 TO COUNT-OF-HOW-MANY-LINES. Add 1 to the count.

GO TO MAIN-ROUTINE-LOOP. Go read another line.

MAIN-ROUTINE-ENDING. When all lines have been read,

DISPLAY COUNT-OF-HOW-MANY-LINES. display the count,

CLOSE POEM-FILE. finish using POEM-FILE,

STOP RUN. and stop running this program.

Copying

This program reads a file called POEM, and creates a copy of it; the copy is a file called POEM2:

IDENTIFICATION DIVISION.

PROGRAM-ID.

COPIER.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT POEM-FILE ASSIGN TO the location of POEM.

SELECT POEM2-FILE ASSIGN TO the location of POEM-2.

DATA DIVISION.

FILE SECTION.

FD POEM-FILE the labeling for POEM.

01 POEM-LINE PIC X(21).

FD POEM2-FILE the labeling for POEM2.

01 POEM2-LINE PIC X(21).

PROCEDURE DIVISION.

MAIN-ROUTINE-BEGINNING.

OPEN INPUT POEM-FILE. Prepare to input from POEM-FILE,

OPEN OUTPUT POEM2-FILE. and output to POEM2-FILE.

MAIN-ROUTINE-LOOP. Do the following repeatedly:

READ POEM-FILE AT END GO TO MAIN-ROUTINE-ENDING. read a line from POEM-FILE,

MOVE POEM-LINE TO POEM2-LINE. copy that line to POEM2-LINE,

WRITE POEM2-LINE. and write POEM2-LINE to POEM2-FILE.

GO TO MAIN-ROUTINE-LOOP.

MAIN-ROUTINE-ENDING. At the end,

DISPLAY "THE FILE HAS BEEN COPIED". display "THE FILE HAS BEEN COPIED",

CLOSE POEM-FILE POEM2-FILE. finish using the files,

STOP RUN. and stop running this program.

Pictures

Suppose youíre dealing with a file named JOE, and each line of JOE-FILE is a three-digit number. Should the lineís picture be edited (JOE-LINE PIC ZZZ) or unedited (JOE-LINE PIC 999)?

When you read a file, the lineís picture must be unedited and match the picture in the program that wrote the file.

When you write a file, ask yourself, "What will read it?" If the answer is "a COBOL program", the picture must be unedited. If the answer is "only a human", edit the picture.

Remember: if one program writes a file, and another program reads it, both programs must use the same picture. For example, if a program writes JACK-FILE and says JACK-LINE PIC S9999V99, the program that reads JACK-FILE must also say JACK-LINE PIC S9999V99.

Peculiarities

To write and read unedited numbers, the computer takes a short-cut: it omits decimal points, and locates the negative sign on top of the last digit. For example, instead of writing -0034.27 in JACK-FILE, the computer writes just 003427. When another program reads 003427 from the file, the S9999V99 picture tells the computer the 003427 means -0034.27.

After you WRITE a line, you cannot use the line again in the program. For example, after you say WRITE POEM-LINE, you should not say MOVE POEM-LINE TO K; it wonít work.

Multiple widths

Letís make the computer compute the square of 12 and the square of 13 and write this file:

HERE ARE THE SQUARES:

144

169

THEY WERE REAL GROOVY

The top and bottom lines are long strings whose pictures are X(21). The other two lines are short numbers whose pictures are 999.

Hereís the program:

IDENTIFICATION DIVISION.

PROGRAM-ID.

SQUARE.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT REPORT-FILE ASSIGN TO location of REPORT. This program uses a file called REPORT.

DATA DIVISION.

FILE SECTION.

FD REPORT-FILE the labeling for REPORT.

01 REPORT-LINE PIC X(21). REPORT-LINE is a 21-character string.

01 REPORT-LINE2 PIC 999. REPORT-LINE2 is a 3-digit number.

PROCEDURE DIVISION.

MAIN-ROUTINE.

OPEN OUTPUT REPORT-FILE. Create a file called REPORT.

MOVE "HERE ARE THE SQUARES:" TO REPORT-LINE. REPORT-LINE is "HERE ARE THE SQUARES:".

WRITE REPORT-LINE. Write "HERE ARE THE SQUARES:".

COMPUTE REPORT-LINE2 = 12 * 12. REPORT-LINE2 is 144.

WRITE REPORT-LINE2. Write 144.

COMPUTE REPORT-LINE2 = 13 * 13. REPORT-LINE2 is 169.

WRITE REPORT-LINE2. Write 169.

MOVE "THEY WERE REAL GROOVY" TO REPORT-LINE. REPORT-LINE is "THEY WERE REAL GROOVY".

WRITE REPORT-LINE. Write "THEY WERE REAL GROOVY".

CLOSE REPORT-FILE. Finish using REPORT.

STOP RUN. Stop running this program.

Advanced structures

COBOL lets you create and manipulate advanced structures.

Group items

In the data division, you can say:

01 K.

02 L PIC 999.

02 M PIC 9.

02 N PIC 99.

That means K is a combination of L, M, and N. If the procedure division says ó

MOVE 427 TO L.

MOVE 8 TO M.

MOVE 31 TO N.

then K will be "427831".

Since K is a combination of other variables, K is called a group variable or group item. L, M, and N are elementary items. Notice that K is the string "427831", not the number 427831. A group item is always a string. Since K is a string, not a number, you cannot say ADD 1 TO K, although you can say ADD 1 TO L or ADD 1 TO M or ADD 1 TO N.

Hereís a group item, for a weight-reducing studio:

01 PERSONAL-INFO-ABOUT-CLIENT.

02 CLIENT-NAME.

03 FIRST-NAME PIC X(15).

03 MIDDLE-INITIAL PIC X.

03 LAST-NAME PIC X(20).

02 CLIENT-SEX PIC X. A person's sex is "M" or "F".

02 CLIENT-AGE PIC 99.

02 WEIGHT-PROGRESS.

03 WEIGHT-WHEN-ENTERED-PROGRAM PIC 999.

03 WEIGHT-THIS-WEEK PIC 999.

03 NUMBER-OF-WEEKS-SO-FAR PIC 999.

PERSONAL-INFO-ABOUT-CLIENT is composed of CLIENT-NAME (which is composed of FIRST-NAME, MIDDLE-INITIAL, and LAST-NAME), CLIENT-SEX, and WEIGHT-PROGRESS (which is composed of WEIGHT-WHEN-ENTERED-PROGRAM, WEIGHT-THIS-WEEK, and NUMBER-OF-WEEKS-SO-FAR). So PERSONAL-INFO-ABOUT-CLIENT is composed of numbers and strings.

Altogether, PERSONAL-INFO-ABOUT-CLIENT contains 48 characters (15 + 1 + 20 + 1 + 2 + 3 + 3 + 3). The computer considers PERSONAL-INFO-ABOUT-CLIENT to be a string whose picture is X(48).

If you say L PIC X(48), you can move all the PERSONAL-INFO-ABOUT-CLIENT to L by saying:

MOVE PERSONAL-INFO-ABOUT-CLIENT TO L.

To move the CLIENT-NAME to M, without moving the CLIENT-SEX, CLIENT-AGE, and WEIGHT-PROGRESS, say:

MOVE CLIENT-NAME TO M.

To write lots of information to a file, make the fileís LINE be a group item.

How to extract from a file Suppose youíve already created a file whose name is EMPLOY; itís on disk or cards. Suppose the file contains information about employees. Suppose each line of the file contains 80 characters, as follows. Characters 1-40 are the employeeís name. Characters 61-70 are the employeeís home phone number, including the area code. The other characters (41-60 and 71-80) are miscellaneous information (such as the employeeís age, sex, address, salary, kind of job, and number of years with the company).

Letís create a new file, called REPORT, on disk or on the printerís paper. Letís make REPORT contain just the employeesí names and phone numbers, and omit the "miscellaneous information". Hereís how:

IDENTIFICATION DIVISION.

PROGRAM-ID.

PHONES.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT EMPLOY-FILE ASSIGN TO location of EMPLOY.

SELECT REPORT-FILE ASSIGN TO location of REPORT.

DATA DIVISION. Throughout the DATA DIVISION, the special word "FILLER"

FILE SECTION. stands for data the program wonít use.

FD EMPLOY-FILE the labeling for EMPLOY.

01 EMPLOY-LINE.

02 EMPLOYEE-NAME PIC X(40). Characters 1-40 are EMPLOYEE-NAME.

02 FILLER PIC X(20). Characters 41-60 are irrelevant.

02 HOME-PHONE.

03 AREA-CODE PIC 999. Characters 61-63 are AREA-CODE.

03 PHONE-EXCHANGE PIC 999. Characters 64-66 are PHONE-EXCHANGE.

03 REST-OF-PHONE-NUMBER PIC 9999. Characters 67-70 are REST-OF-PHONE-NUMBER.

02 FILLER PIC X(10). Characters 71-80 are irrelevant.

FD REPORT-FILE the labeling for REPORT.

01 REPORT-LINE. The PICs say HOME-PHONE looks like this ó

02 EMPLOYEE-NAME-REPORTED PIC X(40). 6176662666

02 HOME-PHONE-REPORTED. but make HOME-PHONE-REPORTED look like this:

03 LEFT-PARENTHESIS PIC X. (617) 666-2666

03 AREA-CODE-REPORTED PIC 999.

03 RIGHT-PARENTHESIS PIC X.

03 PHONE-EXCHANGE-REPORTED PIC B999.

03 THE-DASH PIC X.

03 REST-OF-PHONE-NUMBER-REPORTED PIC 9999.

PROCEDURE DIVISION.

MAIN-ROUTINE-BEGINNING.

OPEN INPUT EMPLOY-FILE.

OPEN OUTPUT REPORT-FILE.

MAIN-ROUTINE-LOOP. The MAIN-ROUTINE-LOOP

READ EMPLOY-FILE AT END GO TO MAIN-ROUTINE-ENDING. reads a line from EMPLOY-FILE,

MOVE EMPLOYEE-NAME TO EMPLOYEE-NAME-REPORTED. copies data into each part of REPORT-LINE,

MOVE "(" TO LEFT-PARENTHESIS.

MOVE AREA-CODE TO AREA-CODE-REPORTED.

MOVE ")" TO RIGHT-PARENTHESIS.

MOVE PHONE-EXCHANGE TO PHONE-EXCHANGE-REPORTED.

MOVE "-" TO THE-DASH.

MOVE REST-OF-PHONE-NUMBER TO REST-OF-PHONE-NUMBER-REPORTED.

WRITE REPORT-LINE. and then writes REPORT-LINE.

GO TO MAIN-ROUTINE-LOOP.

MAIN-ROUTINE-ENDING.

CLOSE EMPLOY-FILE REPORT-FILE.

STOP RUN.

SORT

Suppose CUSTOM is a disk file that contains information about your customers. Suppose each line of the file contains 80 characters, as follows.Ö

Characters 1-20: the customerís last name

Characters 21-80: other information about the customer

Alphabetical order Hereís how to put the file in alphabetical order, according to the customerís name:

IDENTIFICATION DIVISION.

PROGRAM-ID.

ALPHA.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT CUSTOM-FILE ASSIGN TO the location of CUSTOM.

SELECT SORT-FILE ASSIGN TO the location of SORT.

DATA DIVISION.

FILE SECTION.

FD CUSTOM-FILE the labeling for CUSTOM.

01 CUSTOM-LINE PIC X(80).

SD SORT-FILE.

01 SORT-LINE.

02 LAST-NAME PIC X(20).

02 FILLER PIC X(60).

PROCEDURE DIVISION.

MAIN-ROUTINE.

SORT SORT-FILE

ASCENDING KEY LAST-NAME

USING CUSTOM-FILE

GIVING CUSTOM-FILE.

STOP RUN.

Putting a file in order, by alphabetizing or any other method, is called sorting. To sort the CUSTOM-FILE, the computer has to create a temporary disk file called a SORT-FILE.

In the sentence that says SELECT SORT-FILE, hereís what to put for "the location of SORT":

Computer The location of SORT

Eclipse "SORT"

CDC SORT

PDP-10, PDP-20 DSK DSK DSK RECORDING MODE ASCII

IBM using OS UT-S-POEM

IBM using DOS SYS001-UT-3330-S-SORTWK1

In the DATA DIVISIONís FILE SECTION, the SD means a Sort-file Description. In the PROCEDURE DIVISION, the SORT sentence makes the computer automatically open the CUSTOM-FILE, sort it, and close it.

In the SORT sentence, if you replace ASCENDING by DESCENDING, the computer will sort the file in reverse order, so the Zís come first and the Aís come last.

You can make the program fancier, by inserting extra statements before and after the SORT statement. But since the SORT statement automatically tells the computer to open CUSTOM-FILE, the CUSTOM-FILE must not be open already. If you already said OPEN CUSTOM-FILE, you must say CLOSE CUSTOM-FILE before you give the SORT statement.

If you replace GIVING CUSTOM-FILE by GIVING REPORT-FILE, the computer wonít change CUSTOM-FILE, but will create a REPORT-FILE containing the information sorted. For REPORT-FILE, you must type an FD and SELECT it. The computer will automatically open it, so it must not be open already.

Who bought the most? Within each line of CUSTOM-FILE, suppose characters 51-57 tell how much the customer bought from you during the past year. Letís find out which customers bought the most.

Letís make the computer print the customer that bought the most, then the customer that bought the next most, etc. If two customers bought exactly the same amount, letís make the computer print their names in alphabetical order.

This program does it:

IDENTIFICATION DIVISION.

PROGRAM-ID.

BIGBUY.

The ENVIRONMENT DIVISION is same as the previous program's.

DATA DIVISION.

FILE SECTION.

FD CUSTOM-FILE the labeling for CUSTOM.

01 CUSTOM-LINE PIC X(80).

SD SORT-FILE.

01 SORT-LINE.

02 LAST-NAME PIC X(20). characters 1-20

02 FILLER PIC X(30). characters 21-50

02 AMOUNT-BOUGHT-DURING-YEAR PIC 99999V99.

02 FILLER PIC X(23).

PROCEDURE DIVISION.

MAIN-ROUTINE.

SORT SORT-FILE

DESCENDING KEY AMOUNT-BOUGHT-DURING-YEAR

ASCENDING KEY LAST-NAME

USING CUSTOM-FILE

GIVING CUSTOM-FILE.

STOP RUN.

The SORT sentence says: sort the file so that AMOUNT-BOUGHT-DURING-YEAR is in DESCENDING order; in case of a tie, put LAST-NAME in ASCENDING order.

MERGE

Suppose OLDCUS and NEWCUS are files: OLDCUS describes your old customers, and NEWCUS describes your newer customers. In those files, each line contains 80 characters; characters 1-20 contain the customerís last name. Each fileís already in alphabetical order, by customerís last name.

Letís combine the two files. In other words, letís create a "combination" file (on disk or printer paper), called ALLCUS, that contains all the customers; and letís make ALLCUS be in alphabetical order also. Hereís how:

IDENTIFICATION DIVISION.

PROGRAM-ID.

MERGER.

ENVIRONMENT DIVISION.

CONFIGURATION SECTION.

SOURCE-COMPUTER.

The computer's name.

OBJECT-COMPUTER.

The computer's name again.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT OLDCUS-FILE ASSIGN TO the location of OLDCUS.

SELECT NEWCUS-FILE ASSIGN TO the location of NEWCUS.

SELECT ALLCUS-FILE ASSIGN TO the location of ALLCUS.

SELECT SORT-FILE ASSIGN TO the location of SORT.

DATA DIVISION.

FILE SECTION.

FD OLDCUS-FILE the labeling for OLDCUS.

01 OLDCUS-LINE PIC X(80).

FD NEWCUS-FILE the labeling for NEWCUS.

01 NEWCUS-LINE PIC X(80).

FD ALLCUS-FILE the labeling for ALLCUS.

01 ALLCUS-LINE PIC X(80).

SD SORT-FILE.

01 SORT-LINE.

02 LAST-NAME PIC X(20).

02 FILLER PIC X(60).

PROCEDURE DIVISION.

MAIN-ROUTINE.

MERGE SORT-FILE

ASCENDING KEY LAST-NAME

USING OLDCUS-FILE NEWCUS-FILE

GIVING ALLCUS-FILE.

STOP RUN.

That program creates ALLCUS, which is a combination of OLDCUS and NEWCUS. To do that, the computer must create a SORT-FILE.

The word MERGE automatically opens and closes all the files involved. so do not say OPEN or CLOSE.

Warning: the word MERGE is in COBOL 74 but not in COBOL 68. So if your computer is old-fashioned and understands just COBOL 68, it doesnít understand the word MERGE.

If your computer understands the word MERGE, you can merge as many files as you like. For example, if you have files called CUS1, CUS2, CUS3, and CUS4, you can say:

MERGE SORT-FILE

ASCENDING KEY LAST-NAME

USING CUS1-FILE CUS2-FILE CUS3-FILE CUS4-FILE

GIVING ALLCUS-FILE.

Before you MERGE, make sure that the files youíre USING are already in alphabetical order.

Subscripts

Like other computer languages, COBOL lets you use subscripts.

For example, suppose your 4 favorite friends are SUE, JOE, TOM, and ANN. Letís make FAVORITE-FRIEND (1) be "SUE", FAVORITE-FRIEND (2) be "JOE", FAVORITE-FRIEND (3) be "TOM", and FAVORITE-FRIEND (4) be "ANN". Hereís how:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 FAVORITE-FRIEND-TABLE.

02 FAVORITE-FRIEND OCCURS 4 TIMES PIC XXX. You have 4 FAVORITE-FRIENDs; each has PIC XXX.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE "SUE" TO FAVORITE-FRIEND (1).

MOVE "JOE" TO FAVORITE-FRIEND (2).

MOVE "TOM" TO FAVORITE-FRIEND (3).

MOVE "ANN" TO FAVORITE-FRIEND (4).

DISPLAY FAVORITE-FRIEND (1).

DISPLAY FAVORITE-FRIEND (2).

DISPLAY FAVORITE-FRIEND (3).

DISPLAY FAVORITE-FRIEND (4).

STOP RUN.

The computer will display:

SUE

JOE

TOM

ANN

When typing the program, remember to put a blank space before the subscript:

FAVORITE-FRIEND (1)

­

blank space

In COBOL, you say "OCCURS" instead of "DIMENSION":

BASIC: DIM F(4)

FORTRAN: DIMENSION F(4)

PASCAL: F: ARRAY [1..4]

COBOL: F OCCURS 4 TIMES

The subscript can be a variable. For example, instead of saying ó

DISPLAY FAVORITE-FRIEND (1).

DISPLAY FAVORITE-FRIEND (2).

DISPLAY FAVORITE-FRIEND (3).

DISPLAY FAVORITE-FRIEND (4).

you can say:

DISPLAY FAVORITE-FRIEND (I).

To do that, you must tell the computer that the I goes from 1 to 4. Hereís how:

DATA DIVISION.

WORKING-STORAGE SECTION.

01 FAVORITE-FRIEND-TABLE.

02 FAVORITE-FRIEND OCCURS 4 TIMES PIC XXX.

01 I PIC 9. I is a one-digit number.

PROCEDURE DIVISION.

MAIN-ROUTINE.

MOVE "SUE" TO FAVORITE-FRIEND (1).

MOVE "JOE" TO FAVORITE-FRIEND (2).

MOVE "TOM" TO FAVORITE-FRIEND (3).

MOVE "ANN" TO FAVORITE-FRIEND (4).

PERFORM SHOW-FRIENDSHIP

VARYING I FROM 1 BY 1 UNTIL I > 4. I will be 1, 2, 3, 4.

STOP RUN.

SHOW-FRIENDSHIP.

DISPLAY FAVORITE-FRIEND (I). I is the subscript.

To make the program run faster, say "COMP" at the end of the subscriptís picture:

Computer What to say

PDP, Eclipse 01 I PIC 9 COMP.

CDC 01 I PIC 9 COMP-1.

IBM 01 I PIC 9 COMP SYNC.

COMP stands for the word COMPUTATIONAL; SYNC stands for the word SYNCHRONIZED.

A subscript cannot contain an operation:

Okay: FAVORITE-FRIEND (3)

Wrong: FAVORITE-FRIEND (2 + 1) The + is not allowed.

Hereís how to make Y-TABLE be a table that has 4 rows and 6 columns:

01 Y-TABLE.

02 Y-ROW OCCURS 4 TIMES.

03 Y OCCURS 6 TIMES PIC XXX.

The entire table is called:

Y-TABLE

The first row of Y-TABLE is called:

Y-ROW (1)

The second row of Y-TABLE is called:

Y-ROW (2)

The entry in the 2nd row and 5th column of Y-TABLE is called:

Y (2, 5)

­ ­

spaces

 

Test scores Suppose you teach 25 students, youíve given each student 4 tests, and you want to put the scores in a table.

You want the table to contain 25 rows (a row for each student). In each row, you want the studentís first name, middle initial, last name, and 4 scores.

Hereís how:

01 STUDENT-INFORMATION-TABLE.

02 STUDENT-INFORMATION-ROW OCCURS 25 TIMES.

03 FIRST-NAME PIC X(15).

03 MIDDLE-INITIAL PIC X.

03 LAST-NAME PIC X(20).

03 TEST-SCORE OCCURS 4 TIMES PIC 999.

The entire table is called:

STUDENT-INFORMATION-TABLE

The table contains 25 rows. The first row is called:

STUDENT-INFORMATION-ROW (1)

The twelfth row is called:

STUDENT-INFORMATION-ROW (12)

The information in the twelfth row is called:

FIRST-NAME (12)

MIDDLE-INITIAL (12)

LAST-NAME (12)

TEST-SCORE (12, 1)

TEST-SCORE (12, 2)

TEST-SCORE (12, 3)

TEST-SCORE (12, 4)

Extra comments

Put extra comments in your program, to help your colleagues understand how the program works.

IDENTIFICATION DIVISION

The IDENTIFICATION DIVISION can include these paragraphs:

PROGRAM-ID.

AUTHOR.

INSTALLATION.

DATE-WRITTEN.

DATE-COMPILED.

SECURITY.

In each paragraph after the PROGRAM-ID, put whatever garbage you please. The computer ignores everything the IDENTIFICATION DIVISION says.

The IDENTIFICATION DIVISION helps the computer centerís librarian classify your program. The librarian wants the INSTALLATION paragraph to contain the computer centerís name and address, the DATE-WRITTEN paragraph to tell when you finished debugging the program, the DATE-COMPILED paragraph to tell when the computer translated the program from COBOL into machine language, and the SECURITY paragraph to tell who may look at the program and who must not.

If you put the wrong date in the date-compiled paragraph, donít worry: when you ask the computer to produce a COBOL listing of your program, the listing will automatically show the correct date instead.

Asterisks

The computer ignores any line that begins with an asterisk. So if you put this line in your program ó

*THIS IS A LOUSY PROGRAM

ó the computer will ignore the comment.

Create comments that explain how your program works. Put the comments near the bottom of the IDENTIFICATION DIVISION, near the top of the PROCEDURE DIVISION, and wherever your program looks confusing.

On PDP and Eclipse computers, put the asterisk at the far left; donít put any blank spaces before the asterisk. On IBM and CDC computers, put six blank spaces before the asterisk, so that the asterisk is in column 7.