Cobol and Mainframe

How to Use the PERFORM UNTIL Statement in COBOL

COBOL programs are typically consist of three structural components: Sequence, Selection, and Iteration.

A sequence structure is characterized by the execution of one or more functions in a specific order. For instance, if you have multiple paragraphs that need to be executed in a specific order, you can use the PERFORM statement to execute each section of logic which is represented by different paragraphs. Then, the selection structure offers a choice between two or more functions, depending on a given condition. To implement a selection structure in COBOL, you can use constructs such as the IF-ELSE statement or the EVALUATE statement. Lastly, the iteration structure repeatedly executes a function or block of code as long as a certain condition is satisfied. As an example, the COBOL PERFORM UNTIL statement can be used to repeatedly execute a piece of logic until a specific condition is fulfilled.

Definition:

The PERFORM statement in COBOL is used to move the control to a specific paragraph, section, or procedure, either explicitly or implicitly. There are two ways in which the PERFORM statement in COBOL can be divided. The first category of the PERFORM statement in COBOL is the “inline” perform statement, while the second category is the “outline” perform statement.

The main distinction between an inline and outline perform statements is that an inline perform statement executes the code or commands between “PERFORM” and “AND-PERFORM” immediately, while an outline perform statement references a separate section of code to execute. An example of an inline perform statement is “PERFORM UNTIL statement” where the statements inside the perform block are executed until the statement inside the “until” block is true. In contrast, the outline perform statement is mainly used to execute a set of statements or logic that is included in a separate paragraph or section. An example of an outline perform statement is a standard “PERFORM” statement where the “PERFORM” keyword is followed by the name of a paragraph or section.

PERFORM Statement Variants:

Different variants of COBOL PERFORM statement are as follows:

  • PERFORM… paragraph name
  • PERFORM… times phrase
  • PERFORM… until phrase
  • PERFORM… varying phrase

In this article, we discuss only about the PERFORM… until phrase in detail.

Programming Example 1:

IDENTIFICATION DIVISION.
PROGRAM-ID. UNTILPERFORM.
ENVIRONMENT DIVISION.


INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT EMPLOYEE-DOC
               ASSIGN TO '/home/papan/Desktop/emp.dat'
               
               ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.

WORKING-STORAGE SECTION

01 WS-DOC-SW PIC X (01) VALUE 'N'.
   
    88 NOT-EOF VALUE 'N'.


PROCEDURE DIVISION.
    PERFORM A000-READ UNTIL NOT-EOF
   
    PERFORM UNTIL NOT-EOF
        EMPLOYEE-DOC READ
       
       
END-PERFORM.

STOP RUN.

A000-READ.

Output:

papan@papan-VirtualBox:~/Desktop/until$ cobc -xjF 1.cbl
hello world

Explanation:
An example of an outline perform statement is a “PERFORM A000-READ UNTIL” statement where the code inside the paragraph A000-READ is executed repeatedly until a flag which indicates the end of the file (EOF) is reached.

The logic in the paragraph keeps reading the data from the file until there are no more records left in the file. That means the flag when the end of the file (EOF) is set. The second statement in this particular COBOL program of inline is performed when you specify the PERFORM and END PERFORM logics. What we did in this case is we specified the “Read Logic” file in between the PERFORM and END-PERFORM. So, the logic in the paragraph continues to be executed until the end of file flag is not set.

Programming Example 2:

IDENTIFICATION DIVISION.
PROGRAM-ID. PERFORM3.
AUTHOR. PAPAN.
ENVIRONMENT DIVISION.
DATA DIVISION.

WORKING-STORAGE SECTION.
77 WS-I PIC 9(1) VALUE 1.

PROCEDURE DIVISION.
    PERFORM 1000-PARA UNTIL WS-I > 5
    STOP RUN.

1000-PARA.
    COMPUTE WS-I = WS-I + 1
    DISPLAY 'HERE COBOL SHOWING PERFORM UNTIL' WS-I.

Output:

papan@papan-VirtualBox:~/Desktop/until$ cobc -xjF 2.cbl

HERE COBOL SHOWING PERFORM UNTIL2
HERE COBOL SHOWING PERFORM UNTIL3
HERE COBOL SHOWING PERFORM UNTIL4
HERE COBOL SHOWING PERFORM UNTIL5
HERE COBOL SHOWING PERFORM UNTIL6

Explanation:
Here, we use the command called “UNTIL”. We declare the variable and pass some values whenever we want to use any condition. Then, we use the UNTIL command.

Here, we use UNTIL WS-I > 5. Based on this condition, we call the “HERE COBOL SHOWING PERFORM UNTIL” paragraph. That’s how this program is executed.

Programming Example 3:

IDENTIFICATION DIVISION.
PROGRAM-ID. UNTILPERFORM.
ENVIRONMENT DIVISION.
DATA DIVISION.

WORKING-STORAGE SECTION.

    01 WS-A PIC 9(3) VALUE ZERO.
    01 WS-B PIC 9(3) VALUE ZERO.
    01 K PIC 9 VALUE ZERO.

PROCEDURE DIVISION.

    MOVE 1 TO K.
    PERFORM ACCEPT-PARA UNTIL K > 2.
    STOP RUN.

ACCEPT-PARA.
    ACCEPT WS-A.
    ACCEPT WS-B.
    PERFORM DISPLAY-PARA.


DISPLAY-PARA.
    DISPLAY WS-A.
    DISPLAY WS-B.
    COMPUTE K = K + 1
STOP RUN.

Output:

papan@papan-VirtualBox: ~/Desktop/until$ cobc -xjF 3.cbl
10
12
010
012

Explanation:
In the PERFORM UNTIL condition, the paragraph is executed when the condition is false and it comes out of the paragraph when the condition becomes true.

Here, we use three variables which are WS-A, WS-B, and K. In the procedure division, we pass 1 to the K variable. Then, we call the ACCEPT-PARA. Here, we need to add one condition which is UNTIL k > 2. It means that if the value of K is greater than 2, this paragraph is called. The output shows as WS-A, WS-B. Then, this value of K increases by 1 and the value of K = 2. It means that this condition is checked for the value of k = 2. If the condition is false, it will be ACCEPT-PARA again.

Conclusion

Here, we are able to understand all the aspect and syntax of the PERFORM UNTIL command in COBOL to understand this command. We performed some programming examples. With the help of these programming examples, we are able to monitor this command and understand how it works in the real field.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com