CONTROLLING PROGRAM FLOW IN VS COBOL II - IBM Mainframe

In your structured program, you control the flow of the program with loops and decisions that are controlled by conditional statements.

Coding Loops

For looping (repeating the same code), use one of the forms of the PERFORM statement. You can use the PERFORM statement to control the looping with a definite number or with a decision. PERFORM statements can be in-line or out-of-line. Use the PERFORM statement to execute a paragraph and then implicitly return control to the next executable statement. In effect, the PERFORM statement is a way of specifying a closed subroutine that you can enter from many different parts of the program.

Coding a Loop to Be Executed a Definite Number of Times

Use the PERFORM ... TIMES statement to execute a paragraph a specified number of times:

Coding a Loop to Be Executed a Definite Number of Times

When the control reaches the PERFORM statement, the code for the paragraph 010-PROCESS-ONE-MONTH is executed 12 times before control is transferred to the INSPECT statement. Conditional Looping

Use the PERFORM . . . UNTIL statement to execute a paragraph until a condition you specify is satisfied. You can use either of the following forms:

  • PERFORM ... WITH TEST AFTER ... UNTIL ...
  • PERFORM ... [WITH TEST BEFORE]... UNTIL...

In the following example, the implicit TEST BEFORE phrase provides a 'DO-WHILE' structure.

Coding a Loop to Be Executed a Definite Number of Times

When control reaches the PERFORM statement, the condition (MONTH EQUAL DECEMBER) is tested. If it is satisfied, control is transferred to the INSPECT statement. If it is not satisfied, 010-PROCESS-ONE-MONTH is executed, and the condition is tested again. This cycle continues until the condition tests as true. (To make your program easier to read, you may want to code the WITH TEST BEFORE clause.) Use the PERFORM ... WITH TEST AFTER . . . UNTIL if you want to execute the paragraph at least once and then test before any subsequent execution. This is equivalent to the "do-until" structure. Looping through a Table

Use the PERFORM statement to control a loop through a table. You can use either of the following forms:

  • PERFORM ... WITH TEST AFTER ... VARYING . .. UNTIL .. .
  • PERFORM ... [WITH TEST BEFORE] ... VARYING . .. UNTIL...

For example, use PERFORM . . . VARYING to initialize the table. In this form, a variable is increased or decreased and tested until a condition is satisfied. Executing a Group of Paragraphs or Sections

In structured programming, the paragraph you execute is usually a single paragraph. However, you can execute a group of paragraphs, a single section, or a group of sections using the PERFORM . . . THRU statement.

Coding a Loop to Be Executed a Definite Number of Times

When you use PERFORM . . . THRU, use a paragraph-EXIT statement to clearly indicate the end point for the series of paragraphs.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM Mainframe Topics