The COBOL-85 Tutorial, Chapter 4

Structuring COBOL programs.

Copyright © 1998-2017

Kim Kjærsulf.


This chapter (TOC)

Control structures.
IF.
Conditions.
Condition names defined by the programmer (level 88).
Class names.
PERFORM.
PERFORM - simple case.
PERFORM - thru.
PERFORM - repeat.
PERFORM - repeat (inline).
PERFORM - for loop.
GO TO.
EVALUATE.
Paragraph and section.

Control structures.

[This chapter] [TOC] [Tutorial]


This chapter describes how to control selections and iterations in a COBOL program. The instructions for these tasks, is very much connected to the way COBOL programs have been structured for a very long time. There have been no tradition among cobol-programmers to write small programs, who performed a single task based on some parameters. I have seen a lot of very large programs that could easy have been split into several small programs. A small program will allways be more readable, and less prone to errors when changed. Later on in this book, I will show an alternative method for structuring. A method that are more like it is done in pascal or C.


IF

[This chapter] [TOC] [Tutorial]


The basic instruction for expressing a selection is "if". At a first glance it looks just like in pascal ( and C if it wasn´t for the THEN word ), but be careful. If you do not use the END-IF to encapsulate the statements that are controlled by the IF, all statements up to the next period is part of the IF. In COBOL-74 this was the only possibility and hence there are a lot of IF's out there without the END-IF part! A problem who rose because of the lack of END-IF in COBOL-74, have produced some puzzling pieces of code. When the programmer wanted to use nested IF, and the innermost IF had an ELSE part. How can the innermost IF … ELSE be ended, without ending the IFs that are surrounding it? Either by an unconditional branching (GO TO) or by placing the nested IF … ELSE in another paragraph called by the outermost IF. Neither of these solutions make the program more easy to read and understand. I am not writing this so you can appreciate the END-IF in COBOL-85, but so you do not blame the writers of old program - they had no better way to write the code.
		IF condition THEN 
			{statement-1 | NEXT SENTENCE}
		{ELSE 
			{statement-2 | NEXT SENTENCE}}
		[END-IF]


Conditions.

[This chapter] [TOC] [Tutorial]


A condition in COBOL can be made up of these parts:-
The exact syntax of conditions can be found in the COBOL manual, but I will introduce the level 88 and the programmer defined class names here.


Condition names defined by the programmer (level 88).

[This chapter] [TOC] [Tutorial]


The prime use of condition names defined by the programmer, is to define conditions in conjunction with a variable once. And then perform the test at different places in the program. Two things are gained by this:-
There can be given a name to the fact that a variable contains one or more specific values. If the variable - at any given time during execution of the program - contains the value, the name of the condition is TRUE, else it is FALSE.

Example.

000000* An example illustrating the use of a programmer defined conditionname
       identification division.
       program-id.     level88.
       author.         kik.
       environment division.
       configuration section.
       special-names.
            console is crt
            decimal-point is comma.
       data division.
       working-storage section.
       77  transaction-kode    pic 99.
       88  valid-kode          value 4, 8 thru 15.
       88  create              value 10.
       88  destroy             value 15.

       procedure division.
       main section.
      *
      *  Some code leading to "transacion-kode" getting a value
      *

           move 10 to transaction-kode.


      *
      * Testing the conditions 
      *

           if valid-kode then
              if create then
                 perform p-create
              else
                 if destroy then
                    perform p-destroy
                 else
                    perform ordinary-transaction.

      *
       p-create.
      *  some creation code

       p-destroy.
      *  some destruction code


       ordinary-transaction.
      *  some ordinary data processing code

      *-------------------------------------------------------
      *  This is how the selection should have been made without the level88.
 
           if transaction-kode = 4 or 
              ( transaction-kode >= 8 and transaction-kode <= 15 ) then
              if transaction-kode = 10 then  
                 perform p-create
              else
                 if transaction-kode = 15 then
                    perform p-destroy
                 else
                    perform ordinary-transaction.


Class names.

[This chapter] [TOC] [Tutorial]


A class name looks a lot like conditions defined by the programmer, by is not connected to a single variable.

You can define your own classes, but COBOL-85 gives you a selection of predefined class names of which "NUMERIC" is - I guess - the most common used. Please consult your COBOL manual to study the predefined class names. I will show how to declare your own classes:-

Example.

000000* An example illustrating the use of a programmer defined classname
       identification division.
       program-id.     classname.
       author.         kik.
       environment division.
       configuration section.
       special-names.
            console is crt
            decimal-point is comma
            class hexadecimal is '0' thru '9', 
                                 'a' thru 'f', 'A' thru 'F'.
       data division.
       working-storage section.
       77  data-string      pic x(30).

       procedure division.
       main section.
      *
      *  Some code leading to "data-string" getting a value
      *

           move '15f6bC6' to data-string.


      *
      * Testing the conditions 
      *

           if data-string is hexadecimal then
              perform hex-process
           else
              perform ordinary-process.

      *
       hex-process.

      *
       ordinary-process.


PERFORM

[This chapter] [TOC] [Tutorial]


The PERFORM statement is found in at least four different formats, which in turn can also vary. This statement is used for:- Only a limited selection is shown here.

PERFORM - simple case

[This chapter] [TOC] [Tutorial]


The simple format in the first variation. This corresponds to BEGIN..END in Pascal or to {...} in C++.
	PERFORM
		statement series
	END-PERFORM


PERFORM - thru

[This chapter] [TOC] [Tutorial]


The simple format in the second variation.
This is used to execute all statements in a paragraph or a series of paragraphs. Note that by using THRU all paragraphs from and including paragraph-1 to and including paragraph-2 will be executed.
Collecting paragraphs in this manner in one section leads to better programs, but unfortunately there is no tradition among programmers for doing so. You can think of this PERFORM as a kind of call to a subroutine, but without the possibility of using parameters.
			PERFORM paragraph-1 [THRU paragraph-2]


PERFORM - repeat

[This chapter] [TOC] [Tutorial]


A repeat/while construction with PERFORM. The condition is like a condition in an IF-statement.
	PERFORM paragraph [THRU paragraph-2]
		WITH TEST {BEFORE | AFTER}
		UNTIL condition
Se also : PERFORM - repeat (inline)


PERFORM - repeat (inline)

[This chapter] [TOC] [Tutorial]


Another repeat/while construction with PERFORM.
	PERFORM [WITH TEST {BEFORE | AFTER}]
		UNTIL condition
		statement series
		END-PERFORM
Se also : PERFORM - repeat


PERFORM - for loop

[This chapter] [TOC] [Tutorial]


A for-loop with PERFORM, notice that PERFORM can handle up to 3 levels of nested loops in one statement. If you need more you have to place the extra levels in the paragraph that you perform.
	PERFORM paragraph [THRU paragraph-2]
		VARYING counter FROM start value
		BY interval UNTIL end condition
		[AFTER counter-2 FROM start value-2
		BY interval-2 UNTIL end condition-2]
		[AFTER counter-3 FROM start value-3
		BY interval-3 UNTIL end condition-3]


GO TO

[This chapter] [TOC] [Tutorial]


As a rule it is best to avoid using GO TO, but in COBOL-74 this proved to be difficult. As a result many programmers hang on to this bad habit. The worst usage is to make a GO TO out of a number of paragraphs which is executed by a PERFORM .. THRU … This leads to you losing control over the THRU!

There is, however, a special variation of GO TO, which is very useful in connection with the implementation of an object-oriented design (dealt with at the end of this material):
GO TO paragraph-1 [paragraph-2] ... DEPENDING ON identifier.

If the variable given as the identifier has the value 1, the program will continue processing paragraph-1.
If the variable given as the identifier has the value 2, the program will continue processing paragraph-2, etc.

For the sake of clarity it is recommended to let paragraph-1, -2, etc., be concluded with a standard GO TO to a shared place, e.g. immediately after GO TO ... DEPENDING.


EVALUATE

[This chapter] [TOC] [Tutorial]


EVALUATE is the equivalent of Pascal's CASE or C's switch, but is much more wide-ranging, almost in the nature of a decision table.

See the manual for the actual syntax - here we present merely a few examples:

Example 1:

		EVALUATE TRANSACTION-CODE
		   WHEN 1	PERFORM P-A
		   WHEN 2	PERFORM P-B
		   WHEN 3	PERFORM P-C
		   WHEN OTHER PERFORM P-D.
Depending on the value in TRANSACTION-CODE Evaluate will execute one of the four paragraphs P-A .. P-D.

Example 2:

000000* An example illustrating the use of the EVALUATE instruction
       identification division.
       program-id.     evaluat.
       author.         kik.
       environment division.
       configuration section.
       special-names.
            console is crt
            decimal-point is comma.
       data division.
       working-storage section.
       77  transaction-kode    pic 99.
       88  valid-kode          value 4, 8 thru 15.
       88  create              value 10.
       88  destroy             value 15.

       77  balance             pic 9(9)v99      comp.
       procedure division.
       main section.
      *
      *  Some code leading to variables getting a value
      *

           move 10 to transaction-kode.
           move 1234,56 to balance.

      *
      * Testing the conditions 
      *

           if valid-kode then
              evaluate true    also  balance > zero    
              when     create  also  any          perform p-create
              when     destroy also  false        perform p-destroy
              when other                
                                     perform ordinary-transaction.

      *
       p-create.
      *  some creation code

       p-destroy.
      *  some destruction code


       ordinary-transaction.
      *  some ordinary data processing code

      *-------------------------------------------------------
      *  This is how the selection should have been made using IF without the level88.
 
           if transaction-kode = 4 or 
                  ( transaction-kode >= 8 and transaction-kode <= 15 ) then
              if transaction-kode = 10 then  
                 perform p-create
              else
                 if transaction-kode = 15 and not balance > zero then
                    perform p-destroy
                 else
                    perform ordinary-transaction.

[This chapter] [TOC] [Tutorial]



Paragraph and section.

[This chapter] [TOC] [Tutorial]


The normal practice is to conclude a series of connected paragraphs with a paragraph containing nothing other than the instruction EXIT. This signals to other programmers that this paragraph concludes a "procedure". You perform such a series of paragraphs by using the PERFORM … THRU instruction.

Example.

000000* An example illustrating the use of a programmer defined paragraphs
      * and perform-thru
       identification division.
       program-id.     level88.
       author.         kik.
       environment division.
       configuration section.
       special-names.
            console is crt
            decimal-point is comma.
       data division.
       working-storage section.
       77  transaction-kode    pic 99.
       88  valid-kode          value 4, 8 thru 15.
       88  create              value 10.
       88  destroy             value 15.

       procedure division.
       main section.
      *
      *  Some code leading to "transacion-kode" getting a value
      *

           move 10 to transaction-kode.


      *
      * Testing the conditions 
      *

           if valid-kode then
              if create then
                 perform p-create thru p-create-end
              else
                 if destroy then
                    perform p-destroy thru p-destroy-end
                 else
                    perform ordinary-transaction 
                            thru ordinary-transaction-end.

      *
       p-create.
      *  some creation code
       p-create-end.
         exit.

       p-destroy.
      *  some destruction code
       p-destroy-end.
         exit. 

       ordinary-transaction.
      *  some ordinary data processing code
       ord-trns-1.

       ord-trns-2.

       ordinary-transaction-end.
         exit.
Sections can be PERFORMed as a whole, i.e. these are to be viewed as an entity, a factor that is especially relevant in the case of overlay sections. In a modern operating system it is normally not the programmers responsibility to control what part of a program that is to be swapped to a disk-file. The operating system is usually much better at this task.

[This chapter] [TOC] [Tutorial]



Exercise 1.

[This chapter] [TOC] [Tutorial]


Enter, compile and run the examples in this chapter.


Copyright © 1998-2017

Kim Kjærsulf


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