The COBOL-85 Tutorial, Chapter 3

Data processing in COBOL.

Copyright © 1998-2017

Kim Kjærsulf.


This chapter (TOC)

Copying between fields.
Editing output.
Calculations.
ADD.
SUBTRACT.
MULTIPLY.
DIVIDE.
COMPUTE.
Intrinsic Functions
Handling text strings.
Exercises

Copying between fields.

[This chapter] [TOC] [Tutorial]


Copying the contents of one variable to another (using MOVE) is the most common form of "data processing" in a COBOL program. This should be seen in the light of the fact that a variable's data type can have many forms and that it is these that actually control MOVE. A second factor: COBOL is very much geared to dealing with tasks involving the validation of input and the formatting of output.


Editing output.

[This chapter] [TOC] [Tutorial]


When it is required that figures are presented in a report these always have to appear in a format which is not suitable for use as an internal format. The editing of data will be reviewed in the chapter on print lines.


Calculations.

[This chapter] [TOC] [Tutorial]


What we normally understand as data processing is of course to calculate or compute (hence "computer"), and COBOL naturally also offers several possibilities: I will give a detailed explanation of the ADD instruction and the COMPUTE instruction, and then leave it to you to examine the other three.


ADD.

[This chapter] [TOC] [Tutorial]


To add numeric values, you use the ADD-instruction.
There is two major formats for the ADD:-
one where the receiving variable is part of the calculation and
another where the original value in the receiving variable is discarded.

The receiving variable is part of the calculation.

[This chapter] [TOC] [Tutorial]


In the COBOL manual you will probably find the syntax described like this:-
	ADD {identifier-1 | literal-1} ... TO {identifier-2 [ROUNDED]} ...
		[ON SIZE ERROR imperative-statement-1]
		[NOT ON SIZE ERROR imperative-statement-2]
		[END-ADD]
All the "{, ( , ) , }, [, ]" state what is legal uses of the ADD.

You can add the values of one or more variables (identifiers) and/or literal values to one or more receiving variables (identifiers).

For each of the receiving variables, you can specify that the value should be rounded. The rounding is done according to the size (number of decimal places) of the receiving variable. If you do not specify ROUNDED the excess decimal digits will be truncated.

If the receiving variable cannot hold the result, i.e. there is not enough digits in front of the decimal place, the calculation result in a size error, which is normally ignored. To prevent an undiscovered error from messing up to program's output, you can trap the size error with an imperative statement.

You will find a list of all imperative statements in the manual, but for now you may think of an imperative statement as one without a condition and without any branching.

You can think of the NOT ON SIZE ERROR part of the instruction, as a kind of "else" part, that can be omitted.

Example :

		ADD figure,  2 TO teller.		
In pascal this statement will look like this:
  teller := teller + figure + 2

The original value in the receiving variable is discarded.

[This chapter] [TOC] [Tutorial]


The syntax for the second ADD is expanded by the word "GIVING".
	ADD {identifier-1 | literal-1} ... TO {identifier-2 | literal-2}
		GIVING {identifier-3 [ROUNDED]}
		[ON SIZE ERROR imperative-statement-1]
		[NOT ON SIZE ERROR imperative-statement-2]
		[END-ADD]

Example :

		ADD figure TO 2 GIVING teller.
In pascal this statement will look like this:
   teller := figure + 2


SUBTRACT.

[This chapter] [TOC] [Tutorial]


I have given a detailed explanation of the ADD instruction and the COMPUTE instruction. The other instructions for calculations are left to you to examine. (They all are very similar to ADD)
I have however included a description of the syntax, and an exampel:-

Syntax (1):

	SUBTRACT {identifier-1 | literal-1} ... FROM {identifier-2 [ROUNDED]} ...
		[ON SIZE ERROR imperative-statement-1]
		[NOT ON SIZE ERROR imperative-statement-2]
		[END-SUBTRACT]

Example :

		SUBTRACT figure,  2 FROM teller.
In pascal this statement will look like this:
		teller := teller - figure - 2

Syntax (2) :

	SUBTRACT {identifier-1 | literal-1} ... FROM {identifier-2 | literal-2}
		GIVING {identifier-3 [ROUNDED]}
		[ON SIZE ERROR imperative-statement-1]
		[NOT ON SIZE ERROR imperative-statement-2]
		[END-ADD]

Example :

		SUBTRACT figure FROM  2 GIVING teller.
In pascal this statement will look like this:
		teller := 2 - figure


MULTIPLY.

[This chapter] [TOC] [Tutorial]


I have given a detailed explanation of the ADD instruction and the COMPUTE instruction. The other instructions for calculations are left to you to examine. (They all are very similar to ADD)
I have however included a description of the syntax, and an exampel:-

Syntax (1):

	MULTIPLY {identifier-1 | literal-1}  BY {identifier-2 [ROUNDED]} ...
		[ON SIZE ERROR imperative-statement-1]
		[NOT ON SIZE ERROR imperative-statement-2]
		[END-MULTIPLY]

Example :

		MULTIPLY teller BY 2, figure.	
In pascal this statement will look like this:
		teller := teller * figure * 2

Syntax (2) :

	MULTIPLY {identifier-1 | literal-1}  BY {identifier-2 | literal-2}
		GIVING {identifier-3 [ROUNDED]}
		[ON SIZE ERROR imperative-statement-1]
		[NOT ON SIZE ERROR imperative-statement-2]
		[END-MULTIPLY]

Example :

		MULTIPLY figure BY 2 GIVING teller.
In pascal this statement will look like this:
		teller := figure * 2


DIVIDE.

[This chapter] [TOC] [Tutorial]


I have given a detailed explanation of the ADD instruction and the COMPUTE instruction. The other instructions for calculations are left to you to examine. (They all are very similar to ADD)
I have however included a description of the syntax, and an exampel:-

Syntax (1):

DIVIDE {identifier-1 | literal-1} INTO {identifier-2 [ROUNDED]} ... [ON SIZE ERROR imperative-statement-1] [NOT ON SIZE ERROR imperative-statement-2] [END-DIVIDE]

Example :

		DIVIDE  2 INTO teller.
In pascal this statement will look like this:
		teller := teller / 2

Syntax (2) :

	DIVIDE {identifier-1 | literal-1}  [ INTO | BY ] {identifier-2 | literal-2}
		GIVING {identifier-3 [ROUNDED]}
		[REMAINDER identifier-4]
		[ON SIZE ERROR imperative-statement-1]
		[NOT ON SIZE ERROR imperative-statement-2]
		[END-DIVIDE]

Example :

		DIVIDE figure BY 2 GIVING teller.
In pascal this statement will look like this:
		teller :=  figure / 2


COMPUTE.

[This chapter] [TOC] [Tutorial]


The ordinary four instructions for calculating are very documentary, but not in case of complex statements. The COMPUTE instruction may be more of your liking, if you are a former pascal, C or basic programmer.

The idea behind the COMPUTE instruction is that you compute the new value of a variable by evaluating some arithmetical expression. The explanation of ROUNDED and SIZE ERROR is the same as in the ADD instruction. What is interesting is what arithmetic operations are available?

+ for add.
- for subtract.
* for multiply.
/ for divide.
** for raising to a power.
( and ) for controlling the evaluation.
Operators in the COMPUTE-instruction.
	COMPUTE identifier-1 [ROUNDED] 
                        [identifier-2 [ROUNDED]]
		{EQUAL | =} arithmetical expression	
		[ON SIZE ERROR imperative-statement-1]
		[NOT ON SIZE ERROR imperative-statement-2]
		[END-COMPUTE]

Example :

		COMPUTE teller ROUNDED = (teller + ( figure * 2)) / 4.
or
		COMPUTE teller ROUNDED  EQUAL  (teller + ( figure * 2)) / 4.


Intrinsic Functions

[This chapter] [TOC] [Tutorial]


This section is not part of the free COBOL-85 Tutorial. Please consult your manual.

Built-in Functions.

This chapter

Handling text strings.

[This chapter] [TOC] [Tutorial]


These instructions will not be studied in detail here, please see the manual for further information. However, the key words reproduced here will prove helpful in combination with the sample program given below:-

INSPECT
The instruction allows you, one character at a time:
  • to tally occurrences in a string
  • to replace
  • to count and replace
  • to convert
EXAMINE
The instruction replaces or counts the occurrence of a given character in a variable.
NOT ANSI COBOL-85! Use INSPECT.
STRING
The instruction allows you to join the contents of an alphanumeric variable.
UNSTRING
This instruction is used to separate the contents of an alphanumeric variable, and to place the parts in different fields.
Substring
You can reference a part of a string, by specifying a start and an end index. This is not an instruction, but a special syntax when you reference strings.
TRANSFORM
The instruction replaces a character in an alphanumeric variable, see a converting table.
NOT ANSI COBOL-85! Use INSPECT.

You will find other ways to handle strings in the Intrinsic Functions. (Lower-Case, Upper-Case, Length...)
000000* Examples on string-related commands in COBOL-85
      *
       identification division.
       program-id.    strings.
       author.        kik.
      *
       environment division.
       configuration section.
       special-names.
           console is crt
           decimal-point is comma.
      *
       data division.
       working-storage section.
       77  Enter-1      pic X(30).
       77  Enter-2      pic X(30).
       77  disp-line    pic 99.
       77  strptr       pic 999.
      * Level 78 is Micro-focus specific
      *78  Disp-Length  value length of Disp-Field. <* MF, OO-cobol
       78  Disp-Length  value 64 + 1.

       01  Disp-Field.
           03  Action    pic x(16).
           03  Filler    pic x.
           03  tekst-1   pic x(10).
           03  filler    pic xxx.
           03  tekst-2   pic x(10).
           03  filler    pic xxx.
           03  Result-9  pic 999.
           03  filler    pic xxx.
           03  Result-X.
               05 Res-1     pic x(5) just right.
               05 Res-2     pic x(5) just right.
               05 Res-3     pic x(5) just right.

       01  Headers.
           03  HAction    pic x(16) value "Action".
           03  Filler     pic x     value space.
           03  HTekst-1   pic x(10) value "Enter one".
           03  filler     pic xxx   value space.
           03  Htekst-2   pic x(10) value "Enter two".
           03  filler     pic xxx   value space.
           03  HResult-9  pic x(6)  value "Cnt.".
           03  HResult-X  pic x(10) value "Result".

      *
       procedure division.
      *
       main section.
           display space.

           display "Enter first string:" line 5 position 10.
           accept Enter-1 line 5 position 32.
           display "Enter second string:" line 6 position 10.
           accept Enter-2 line 6 position 32.

           if Enter-1 equal space 
              move "CAaBXAbc" to Enter-1.
           if Enter-2 equal space 
              move "kkkk ii;mm" to Enter-2.

           display Headers line 8 position 1.
           move 11 to disp-line.

      * EXAMINE (OSVS)

           perform Prepare-it.
           move "Examine (OSVS)" to Action.
           move zero to result-9.

           examine result-X replacing all "A" by "B".

           perform show-it.

      * INSPECT (ANS85)

           perform Prepare-it.
           move "Inspect (ans85)" to Action.

           inspect result-X tallying result-9 for all "A".
           inspect result-X replacing all "A" by "B".

           perform show-it.

      * INSPECT (ANS85)

           perform Prepare-it.
           move "Inspect (ans85)" to Action.

           inspect result-X tallying result-9 for all "A".
           inspect result-X replacing characters by "."
                                all "A" by "B"
                                leading "C" by "D".

           perform show-it.

      * INSPECT (ANS85)

           perform Prepare-it.
           move "Inspect (ans85)" to Action.

           inspect result-X tallying result-9 for all "A".
           inspect result-X replacing
                                all "A" by "B"
                                leading "C" by "D"
                                characters by ".".


           perform show-it.

      * INSPECT (ANS85)

           perform Prepare-it.
           move "Inspect (ans85)" to Action.

           inspect result-X replacing
                                all "Abc" by "Xyz"
                                leading "X" by "Y"
                                characters by ".".
           inspect result-X tallying result-9 for all "Y".

           perform show-it.

      * INSPECT (ANS85)

           perform Prepare-it.
           move "Inspect (ans85)" to Action.

           inspect result-X converting
                                "Abc" to "Xyz".

           perform show-it.

      * STRING (ANS85)

           perform Prepare-it.
           move "String (ans85)" to Action.

           string Enter-1 delimited by space,
                  Enter-2 delimited by size
                  into result-X.

           perform show-it.

      * STRING (ANS85)

           perform Prepare-it.
           move "String (ans85)" to Action.

           move 2 to strptr.
      * specifying a substring, from position 3 until end of field.
           string Enter-1 (3:) delimited by space, 
                  Enter-2 delimited by size
                  into result-x
                   with pointer strptr.

           perform show-it.

      * UNSTRING (ANS85)

           perform Prepare-it.
           move "Unstring (ans85)" to Action.

           move 2 to strptr.
      * specifying a substring, from position 3 until end of field.
           unstring Enter-2 (3:) delimited by space or ";" or ","
                   into res-1, res-2, res-3
                   with pointer strptr
                   tallying in result-9.
           perform show-it.

           stop run.

       Prepare-it.
           move space to Disp-Field.
           move Enter-1 to tekst-1, result-x.
           move Enter-2 to tekst-2.
           move zero to result-9.
       Prepare-it-out.
           exit.
      *
       Show-it.
           display Disp-Field line disp-line position 1.
           display "<" line disp-line position Disp-Length.
           add 1 to disp-line.
       Show-it-out.
           exit.

The above program will produce this output to the screen.

---------------------------------------------------------------------------------------------
         Enter first string:
         Enter second string:

Action           Enter one    Enter two    Cnt.  Result


Examine (OSVS)   CAaBXAbc     kkkk ii;mm   000   CBaBXBbc       <
Inspect (ans85)  CAaBXAbc     kkkk ii;mm   002   CBaBXBbc       <
Inspect (ans85)  CAaBXAbc     kkkk ii;mm   002   ...............<
Inspect (ans85)  CAaBXAbc     kkkk ii;mm   002   DB...B.........<
Inspect (ans85)  CAaBXAbc     kkkk ii;mm   000   .....Xyz.......<
Inspect (ans85)  CAaBXAbc     kkkk ii;mm   000   CXaBXXyz       <
String (ans85)   CAaBXAbc     kkkk ii;mm   000   CAaBXAbckkkk ii<
String (ans85)   CAaBXAbc     kkkk ii;mm   000   CaBXAbckkkk ii;<
Unstring (ans85) CAaBXAbc     kkkk ii;mm   003       k   ii   mm<



[This chapter] [TOC] [Tutorial]


Exercise 1.

[This chapter] [TOC] [Tutorial]


The purpose of this exercise is to train the following points:

Design a program with which you can:

Model answer for this exercise.


This chapter

Copyright © 1998-2017

Kim Kjærsulf


Last Updated october 3, 2003
For more information contact: Kim Kjærsulf