Understanding STOP RUN and GOBACK in COBOL

 

 Understanding STOP RUN and GOBACK in COBOL: A Comprehensive Guide for Programmers

Introduction

COBOL (Common Business-Oriented Language) is a widely-used programming language for business applications. Within COBOL programs, two important statements govern the program’s termination and return to the calling program: STOP RUN and GOBACK. In this article, we’ll explore the differences between these statements, their appropriate usage in called and calling programs, and provide examplesunderstanding stop run and goback in cobol to illustrate their functionality.

STOP RUN Statement

The STOP RUN statement is used to terminate the current COBOL program gracefully. When this statement is executed, the program’s execution comes to an end, and the control returns to the operating system or the calling program. The STOP RUN statement also releases any resources held by the program, performs end-of-job processing, and closes any open files.

Syntax:

COBOL
STOP RUN

Considerations for STOP RUN:

  1. Usage: The STOP RUN statement is primarily used at the end of the main program to conclude its execution. It is not recommended to use STOP RUN within called subprograms as it would terminate the entire program abruptly, leading to unexpected results.
  2. File Handling: The STOP RUN statement ensures that any open files are properly closed before program termination, preventing data corruption or loss.
  3. Error Handling: Since the STOP RUN statement ends the program abruptly, it may not allow for graceful error handling or cleanup of resources in case of exceptions. It is advisable to use GOBACK in most cases, which allows for better error handling and recovery.

Example 1: Using STOP RUN in the Main Program

COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(3) VALUE 100. 
 
PROCEDURE DIVISION.
DISPLAY "Main Program has started."
MOVE 200 TO NUM
DISPLAY "Main Program is ending now."
STOP RUN.

In this example, the MainProgram starts, displays a message, sets NUM to 200, and then ends using the STOP RUN statement.

GOBACK Statement

The GOBACK statement is used to return control from a called program to the calling program. When a program executes a CALL statement to invoke a subprogram, it temporarily suspends its execution and passes control to the called program. Once the called program completes its execution or encounters a GOBACK statement, control is transferred back to the next statement after the original CALL statement in the calling program.

Syntax:

COBOL
GOBACK

Considerations for GOBACK:

  1. Usage: GOBACK is primarily used within called subprograms to indicate the successful completion of the subprogram’s tasks and return control to the calling program.
  2. Resource Management: Unlike STOP RUN, GOBACK allows the called program to release resources and perform cleanup operations before returning control to the calling program. This makes it suitable for structured error handling and resource management.
  3. Nested CALLs: In scenarios where multiple programs are called in a nested manner, GOBACK ensures that control returns to the appropriate calling program in the correct order.

Example 2: Using GOBACK in a Called Subprogram

COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. SubProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SUB-NUM PIC 9(3) VALUE 50. 
 
PROCEDURE DIVISION.
DISPLAY "SubProgram has started."
ADD 100 TO SUB-NUM
DISPLAY "SubProgram is ending now."
GOBACK.

In this example, the SubProgram starts, displays a message, adds 100 to SUB-NUM, and then ends using the GOBACK statement.

Example 3: Calling SubProgram from Main Program

COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(3) VALUE 100. 
 
PROCEDURE DIVISION.
DISPLAY "Main Program has started."
PERFORM 2000-TASK
DISPLAY "Main Program is ending now."
STOP RUN.
2000-TASK.
DISPLAY "Performing Task 2000."
CALL "SubProgram"
DISPLAY "Task 2000 completed."
EXIT.

In this example, the MainProgram starts, performs a task (2000-TASK) which calls the SubProgram. After the SubProgram completes using the GOBACK statement, the control returns to the next statement after the CALL “SubProgram” statement in the MainProgram.

Combining GOBACK with Conditional Statements:

COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(3) VALUE 100. 
 
PROCEDURE DIVISION.
DISPLAY "Main Program has started."
PERFORM 2000-TASK
DISPLAY "Main Program is ending now."
STOP RUN.
2000-TASK.
DISPLAY "Performing Task 2000."
IF NUM > 50
DISPLAY "Task 2000 completed successfully."
ELSE
DISPLAY "Task 2000 failed."
GOBACK
END-IF.

In this example, the MainProgram starts, performs a task (2000-TASK), which calls the SubProgram. Based on a condition (NUM > 50), the SubProgram either completes successfully or fails using the GOBACK statement.

Conclusion

In COBOL, the STOP RUN and GOBACK statements serve distinct purposes in program execution. STOP RUN is used to terminate the entire program, releasing resources and closing files. It is typically employed in the main program. On the other hand, GOBACK is utilized in called subprograms to return control to the calling program, allowing for structured error handling and resource management.

Understanding when and where to use STOP RUN and GOBACK is essential for designing efficient and robust COBOL programs. With proper usage and consideration of these statements, COBOL developers can ensure smooth program execution and maintainability in business applications.