Transfer of control

In the PROCEDURE DIVISION, unless there is an explicit control transfer or there is no next executable statement, program flow transfers control from statement to statement in the order in which the statements are written. This normal program flow is an implicit transfer of control.

In addition to the implicit transfers of control between consecutive statements, implicit transfer of control also occurs when the normal flow is altered without the execution of a procedure branching statement. The following examples show implicit transfers of control, overriding statement-to-statement transfer of control:

  • After execution of the last statement of a procedure that is executed under control of another COBOL statement, control implicitly transfers. (COBOL statements that control procedure execution are, for example, MERGE, PERFORM, SORT, and USE.) Further, if a paragraph is being executed under the control of a PERFORM statement that causes iterative execution, and that paragraph is the first paragraph in the range of that PERFORM statement, an implicit transfer of control occurs between the control mechanism associated with that PERFORM statement and the first statement in that paragraph for each iterative execution of the paragraph.
  • During SORT or MERGE statement execution, control is implicitly transferred to an input or output procedure.
  • During XML PARSE statement execution, control is implicitly transferred to a processing procedure.
  • During execution of any COBOL statement that causes execution of a declarative procedure, control is implicitly transferred to that procedure.
  • At the end of execution of any declarative procedure, control is implicitly transferred back to the control mechanism associated with the statement that caused its execution.

COBOL also provides explicit control transfers through the execution of any procedure branching, program call, or conditional statement. (Lists of procedure branching and conditional statements are contained in Statement categories.)

Definition: The term next executable statement refers to the next COBOL statement to which control is transferred, according to the rules given above. There is no next executable statement under the following circumstances:

  • When the program contains no PROCEDURE DIVISION
  • Following the last statement in a declarative section when the paragraph in which it appears is not being executed under the control of some other COBOL statement
  • Following the last statement in a program or method when the paragraph in which it appears is not being executed under the control of some other COBOL statement in that program
  • Following the last statement in a declarative section when the statement is in the range of an active PERFORM statement executed in a different section and this last statement of the declarative section is not also the last statement of the procedure that is the exit of the active PERFORM statement
  • Following a STOP RUN statement or EXIT PROGRAM statement that transfers control outside the COBOL program
  • Following a GOBACK statement that transfers control outside the COBOL program
  • Following an EXIT METHOD statement that transfers control outside the COBOL method
  • The end program or end method marker

When there is no next executable statement and control is not transferred outside the COBOL program, the program flow of control is undefined unless the program execution is in the nondeclarative procedures portion of a program under control of a CALL statement, in which case an implicit EXIT PROGRAM statement is executed.

Similarly, if control reaches the end of the PROCEDURE DIVISION of a method and there is no next executable statement, an implicit EXIT METHOD statement is executed.