PERFORM VARYING and PERFORM UNTIL – Loops with COBOL
In COBOL, loops play a crucial role in program flow control, allowing for the repetition of a set of instructions until a specific condition is met. The PERFORM VARYING
command is used to establish a loop controlled by an iteration variable, which is incremented or decremented with each iteration. This provides flexibility to execute a block of code a defined number of times, depending on the value of the control variable. On the other hand, the PERFORM UNTIL
command allows for repeated execution of a code block until a specific condition is met, making it useful when a set of instructions needs to be repeated until a desired state is reached. These two approaches, PERFORM VARYING
and PERFORM UNTIL
, offer powerful tools for the effective implementation of loops in COBOL, facilitating the creation of structured and efficient programs.
PERFORM VARYING
In COBOL, the PERFORM VARYING
loop is used to carry out a series of repetitive actions, where a control variable is incremented or decremented with each iteration. Here is a simple example of a PERFORM VARYING
loop:
IDENTIFICATION DIVISION.
PROGRAM-ID. LoopExample.
AUTHOR. YourName.
DATE-WRITTEN. DD/MM/YYYY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Counter PIC 9(3) VALUE 1.
PROCEDURE DIVISION.
DISPLAY 'Start of loop'.
PERFORM VARYING Counter FROM 1 BY 1 UNTIL Counter > 5
DISPLAY 'Iteration ' Counter
END-PERFORM.
DISPLAY 'End of loop'.
STOP RUN.
WORKING – STORAGE SECTION
Counter: Control variable of the loop, initialized to 1.
PROCEDURE DIVISION
PERFORM VARYING Counter FROM 1 BY 1 UNTIL Counter > 5
: Starts thePERFORM VARYING
loop, which will execute the code block until theUNTIL
condition is met.FROM 1 BY 1
: Indicates that the control variable (Counter
) starts at 1 and increments by 1 with each iteration.UNTIL Counter > 5
: This is the loop’s termination condition.
DISPLAY 'Iteration ' Counter
: Displays the iteration number in the console for each cycle.END-PERFORM
: Marks the end of the loop.DISPLAY 'End of loop'
: Shows a message indicating that the loop has finished.STOP RUN
: Indicates the end of the program.
In this example, the PERFORM VARYING
loop will execute five times, displaying the message “Iteration” followed by the iteration number. You can adjust the UNTIL
condition as needed for the specific logic of your program.
PERFORM UNTIL
As a COBOL programmer, you could design a simple program that uses the PERFORM UNTIL
command to perform a specific task. Suppose you want to create a program that prints the numbers from 1 to 10. Here’s an example of how the program could be structured in COBOL using PERFORM UNTIL
:
IDENTIFICATION DIVISION.
PROGRAM-ID. PrintNumbers.
AUTHOR. YourName.
DATE-WRITTEN. DD/MM/YYYY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Number PIC 9(2) VALUE 1.
PROCEDURE DIVISION.
DISPLAY 'Printing numbers from 1 to 10'.
PERFORM UNTIL Number > 10
DISPLAY Number
ADD 1 TO Number
END-PERFORM.
DISPLAY 'Finished printing numbers'.
STOP RUN.
Explanation:
- IDENTIFICATION DIVISION: Contains metadata about the program.
- DATA DIVISION: Defines the data used in the program, including the
Number
variable, initialized to 1. - PROCEDURE DIVISION: Contains the executable code.
- The
DISPLAY
statement shows the starting message. - The
PERFORM UNTIL
loop continues executing untilNumber
is greater than 10. - Inside the loop, the current value of
Number
is displayed, and then 1 is added toNumber
. - After the loop, a message indicating completion is displayed, and the program ends with
STOP RUN
.
- The
Differences between PERFORM VARYING and PERFORM UNTIL.
The PERFORM VARYING
command and the PERFORM UNTIL
command in COBOL are loop control structures, but they differ in how they control the repetition of a block of code.
PERFORM VARYING
Usage: It is used to perform loops based on a control variable that changes its value in each iteration.
PERFORM [section-name]
VARYING [variable-name] FROM [initial-value]
BY [increment-or-decrement]
UNTIL [termination-condition].
Description: In the example, the loop executes while the Counter
variable is less than or equal to 10, incrementing by 1 in each iteration.
PERFORM UNTIL
Usage: It is used to perform loops based on a condition that must evaluate to true to continue executing the block of code.
PERFORM [section-name]
UNTIL [termination-condition].
Description: In the example, the loop executes until the condition Counter > 10
becomes true, incrementing by 1 in each iteration.
In summary, PERFORM VARYING
is used when you want to loop based on the changing value of a variable, while PERFORM UNTIL
is used when you want to loop based on a specific condition that must evaluate to true to continue. The choice between them depends on the specific logic you are implementing in your COBOL program.