COBOL Perform Statements: Iteration and Looping

May 5, 2024

Perform statements in COBOL are essential for implementing iteration and looping structures. This lesson covers different types of perform statements and how they can be used to control program flow.

Perform Statements in COBOL

Perform statements in COBOL are used for looping and iteration. They allow you to repeat a set of statements multiple times based on a condition. There are different types of perform statements in COBOL:

Simple Perform

The simple perform statement in COBOL is used to repeat a block of code a specified number of times. It is similar to a ‘for’ loop in other programming languages. Here’s an example:

01 COUNT PIC 9(2) VALUE 0. 
PERFORM 10 TIMES
  ADD 1 TO COUNT
END-PERFORM.

In this example, the block of code inside the perform statement will be executed 10 times, incrementing the COUNT variable by 1 each time.

Perform Until

The perform until statement in COBOL is used to repeat a block of code until a certain condition is met. It is similar to a ‘while’ loop in other programming languages. Here’s an example:

PERFORM UNTIL COUNT > 10 
  ADD 1 TO COUNT
END-PERFORM.

In this example, the block of code inside the perform statement will be executed until the COUNT variable is greater than 10.

Nested Perform

You can also nest perform statements within each other to create more complex looping structures. Here’s an example of nested perform statements:

PERFORM 5 TIMES 
  PERFORM 3 TIMES
    ADD 1 TO COUNT
  END-PERFORM
END-PERFORM.

In this example, the inner perform statement will be executed 3 times for each iteration of the outer perform statement, resulting in a total of 15 executions.

Conclusion

Perform statements in COBOL are powerful tools for implementing iteration and looping structures in your programs. By understanding how to use perform statements effectively, you can control the flow of your program and perform repetitive tasks efficiently.