MainframeMaster

COBOL Tutorial

Procedure Branching

Progress0 of 0 lessons

Introduction to Procedure Branching

Procedure branching in COBOL allows programs to alter the normal sequential flow of execution. While COBOL supports traditional branching with statements like GO TO, modern COBOL programming emphasizes structured approaches that make programs easier to understand and maintain.

Branching Statements in COBOL

  • GO TO: Transfers control to a specified paragraph or section
  • EXIT: Marks the logical end of a procedure
  • GOBACK: Returns control to the calling program or operating system
  • STOP RUN: Terminates program execution
  • EXIT PROGRAM: Returns control to the calling program (subprograms only)
  • EXIT PERFORM: Exits from a PERFORM loop

Evolution of Branching in COBOL

COBOL's approach to program flow control has evolved significantly over the decades:

EraApproachKey Features
Early COBOL (1960s-70s)Heavy GO TO usageSpaghetti code, difficult to follow
Structured era (1970s-80s)PERFORM, nested IFsModular design, reduced GO TO usage
COBOL-85Scope terminatorsEND-IF, END-PERFORM, clearer nesting
Modern COBOLStructured exceptionsON EXCEPTION, EXIT PERFORM, structured error handling

GO TO Statement

The GO TO statement transfers program control unconditionally to the specified procedure name. While it has legitimate uses, overuse of GO TO can lead to confusing program logic that is difficult to maintain.

GO TO Statement Formats

Format 1: Simple GO TO

cobol
1
GO TO procedure-name

Format 2: GO TO with DEPENDING ON (computed GO TO)

cobol
1
2
GO TO procedure-name-1 [procedure-name-2] ... DEPENDING ON identifier

GO TO Statement Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
* Example 1: Simple GO TO IF AMOUNT > 1000 GO TO LARGE-AMOUNT-PARAGRAPH ELSE GO TO SMALL-AMOUNT-PARAGRAPH. * Example 2: GO TO with DEPENDING ON MOVE 2 TO CHOICE-INDEX. GO TO OPTION-A OPTION-B OPTION-C OPTION-D DEPENDING ON CHOICE-INDEX. * This will transfer control to OPTION-B (the 2nd procedure) * Example 3: Error handling with GO TO READ CUSTOMER-FILE AT END GO TO EOF-ROUTINE. PROCESS-RECORD. IF RECORD-ERROR GO TO ERROR-ROUTINE. * Example 4: Forward reference (to avoid falling through) PROCESS-VALID-RECORD. PERFORM VALIDATION-ROUTINE. IF VALID-RECORD PERFORM UPDATE-RECORD ELSE PERFORM REJECT-RECORD. GO TO READ-NEXT-RECORD. READ-NEXT-RECORD. READ CUSTOMER-FILE AT END GO TO EOF-ROUTINE. GO TO PROCESS-VALID-RECORD.

GO TO Limitations and Considerations

  • The target of a GO TO must be a procedure name (paragraph or section)
  • GO TO can only branch within the same program; it cannot transfer to another program
  • GO TO with DEPENDING ON requires an integer identifier with value corresponding to the position of the target procedure in the list
  • If the identifier value is outside the valid range, no branch occurs (execution continues sequentially)
  • GO TO within a PERFORM causes the PERFORM context to be lost (control will not return to the PERFORM statement)
  • Backward branches (to earlier paragraphs) are particularly problematic for maintainability

Legitimate Uses of GO TO

  • Simple error exits: Branching to an error handler from deeply nested logic
  • EOF handling: Jumping to end-of-file routines when no more data exists
  • Forward references only: Using GO TO to avoid "falling through" to unrelated code
  • State machines: Implementing complex state transitions in legacy applications
  • Legacy code maintenance: When refactoring would introduce more risk than benefit

In modern COBOL, most GO TO usage can be replaced with structured alternatives like PERFORM, EXIT PERFORM, and EVALUATE statements.

EXIT Statement

The EXIT statement provides a common end point for a procedure and serves as a placeholder when needed. Its enhanced forms provide structured exits from specific program constructs.

EXIT Statement Formats

Format 1: Simple EXIT

cobol
1
EXIT.

Format 2: EXIT PROGRAM

cobol
1
EXIT PROGRAM.

Format 3: EXIT PERFORM [CYCLE]

cobol
1
EXIT PERFORM [CYCLE].

Other variants include EXIT PARAGRAPH, EXIT SECTION, and program construct exits like EXIT FUNCTION.

EXIT Statement Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
* Example 1: Simple EXIT as a procedure placeholder EMPTY-PARAGRAPH. EXIT. * Example 2: EXIT as a common termination point PROCESS-CUSTOMER. IF NOT VALID-CUSTOMER DISPLAY "Invalid customer record" GO TO PROCESS-CUSTOMER-EXIT. PERFORM UPDATE-CUSTOMER-RECORD. PERFORM PRINT-CONFIRMATION. PROCESS-CUSTOMER-EXIT. EXIT. * Example 3: EXIT PERFORM to terminate a loop early PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > TABLE-SIZE IF TABLE-ENTRY(IDX) = SEARCH-VALUE MOVE IDX TO FOUND-INDEX EXIT PERFORM END-IF END-PERFORM. * Example 4: EXIT PERFORM CYCLE to skip the current iteration PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > RECORD-COUNT IF RECORD-STATUS(IDX) = "DELETED" EXIT PERFORM CYCLE END-IF PERFORM PROCESS-ACTIVE-RECORD END-PERFORM. * Example 5: EXIT PROGRAM in a subprogram PROCESS-REQUEST. IF INVALID-REQUEST MOVE "E001" TO RETURN-CODE EXIT PROGRAM END-IF. PERFORM PROCESS-VALID-REQUEST. MOVE "0000" TO RETURN-CODE. EXIT PROGRAM.

Key Points About EXIT

  • The simple EXIT must be the only statement in its sentence and paragraph
  • EXIT alone doesn't cause any action; it's a procedural no-op
  • EXIT PROGRAM in a top-level program has no effect; use GOBACK or STOP RUN instead
  • EXIT PERFORM can only be used within a PERFORM statement
  • EXIT PERFORM terminates the innermost PERFORM when in nested PERFORMs
  • EXIT PERFORM CYCLE skips to the next iteration of the current PERFORM loop
  • The structured EXIT variants (EXIT PERFORM, etc.) are preferred over GO TO for modern COBOL

GOBACK Statement

The GOBACK statement returns control to the calling program or to the operating system if executed in a main program. It's a versatile statement that works appropriately in both main programs and subprograms.

GOBACK Statement Format

cobol
1
GOBACK.

GOBACK is a simple statement with no variations in syntax.

GOBACK Statement Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
* Example 1: GOBACK at normal program termination PROCEDURE DIVISION. PERFORM INITIALIZATION. PERFORM MAIN-PROCESS UNTIL END-OF-FILE. PERFORM CLEANUP. GOBACK. * Example 2: GOBACK for early program termination PROCEDURE DIVISION. PERFORM INITIALIZATION. IF SETUP-ERROR DISPLAY "Critical initialization error" MOVE 8 TO RETURN-CODE GOBACK END-IF. PERFORM MAIN-PROCESS. PERFORM CLEANUP. GOBACK. * Example 3: GOBACK in a subprogram PROCEDURE DIVISION USING INPUT-DATA OUTPUT-DATA. MOVE SPACES TO ERROR-MESSAGE. PERFORM VALIDATE-INPUT. IF INPUT-ERROR MOVE "Invalid input data" TO ERROR-MESSAGE MOVE 4 TO RETURN-CODE GOBACK END-IF. PERFORM PROCESS-DATA. MOVE 0 TO RETURN-CODE. GOBACK.

GOBACK Behavior

ContextGOBACK Behavior
Main programTerminates execution and returns to operating system (like STOP RUN)
Subprogram called with CALLReturns control to calling program (like EXIT PROGRAM)
Nested programReturns control to calling program
User-defined functionReturns to calling function with the current function value

GOBACK vs. Other Termination Statements

FeatureGOBACKSTOP RUNEXIT PROGRAM
Works in main programYesYesNo effect
Works in subprogramYesTerminates entire runYes
Context sensitiveYesNoNo
Recommended for new codeYesRarelyIn specific cases

STOP RUN Statement

The STOP RUN statement terminates the entire COBOL run unit, closing all files and releasing all resources. It's the most definitive way to end program execution.

STOP RUN Statement Format

cobol
1
STOP RUN.

There is also a legacy format:

cobol
1
STOP {literal|identifier}.

The literal/identifier format displays a message but is generally considered obsolete.

STOP RUN Statement Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
* Example 1: Simple program termination PROCEDURE DIVISION. PERFORM MAIN-PROCESS. CLOSE ALL FILES. STOP RUN. * Example 2: Emergency termination PROCESS-TRANSACTION. IF CRITICAL-ERROR DISPLAY "Fatal error encountered: " ERROR-MESSAGE MOVE 16 TO RETURN-CODE CLOSE ALL FILES STOP RUN END-IF. PERFORM PROCESS-VALID-TRANSACTION. * Example 3: Conditional termination PROCESS-USER-COMMAND. IF COMMAND-CODE = "EXIT" OR "QUIT" PERFORM CLEANUP-ROUTINE DISPLAY "Program terminated by user request" STOP RUN END-IF. PERFORM PROCESS-COMMAND. GO TO PROCESS-USER-COMMAND. * Example 4: Obsolete format (not recommended) STOP "End of processing".

Key Points About STOP RUN

  • STOP RUN terminates the entire run unit regardless of where it's executed
  • Files should be explicitly closed before STOP RUN for data integrity
  • STOP RUN in a subprogram will terminate the main program and all subprograms
  • The RETURN-CODE special register value is passed to the operating system
  • In a batch environment, job steps after the COBOL program may depend on the return code
  • In modern COBOL, GOBACK is often preferred over STOP RUN for better subprogram handling
  • The STOP literal/identifier format is obsolete and should not be used in new code

STOP RUN vs. GOBACK in Main Program

In a main program, both STOP RUN and GOBACK have similar effects, but there are subtle differences:

FeatureSTOP RUNGOBACK
Intention clarityExplicit terminationContext-dependent
Code reusabilityLess flexible if code is moved to a subprogramWorks in both contexts
Historical usageTraditional in older codeMore common in modern code

Modern Alternatives to GO TO

Modern COBOL programming emphasizes structured techniques that minimize or eliminate the need for GO TO statements. These alternatives lead to code that is easier to understand, maintain, and debug.

Structured Programming Alternatives

Legacy PatternModern AlternativeAdvantage
GO TO for loopsPERFORM UNTILClear loop boundaries and termination condition
Computed GO TOEVALUATETable-like structure with clear case mapping
Early loop exitEXIT PERFORMMaintains loop structure while allowing early exit
Error jumpsOn-condition phrasesLocal error handling where the error occurs
Sequential paragraph fall-throughPERFORM...THRUExplicit execution of a range of paragraphs

Replacing GO TO Loops

Legacy approach with GO TO:

cobol
1
2
3
4
5
6
7
8
9
READ-RECORDS. READ INPUT-FILE AT END GO TO END-PROCESS. PERFORM PROCESS-RECORD. GO TO READ-RECORDS. END-PROCESS. CLOSE INPUT-FILE.

Modern approach with PERFORM UNTIL:

cobol
1
2
3
4
5
6
7
8
9
10
PROCESS-ALL-RECORDS. PERFORM UNTIL END-OF-FILE READ INPUT-FILE AT END SET END-OF-FILE TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM. CLOSE INPUT-FILE.

Replacing Computed GO TO

Legacy approach with GO TO DEPENDING ON:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
MOVE TRANSACTION-TYPE TO TYPE-INDEX. GO TO PROCESS-SALE PROCESS-RETURN PROCESS-ADJUSTMENT PROCESS-INQUIRY DEPENDING ON TYPE-INDEX. PROCESS-SALE. PERFORM SALE-ROUTINE. GO TO TRANSACTION-EXIT. PROCESS-RETURN. PERFORM RETURN-ROUTINE. GO TO TRANSACTION-EXIT. PROCESS-ADJUSTMENT. PERFORM ADJUSTMENT-ROUTINE. GO TO TRANSACTION-EXIT. PROCESS-INQUIRY. PERFORM INQUIRY-ROUTINE. GO TO TRANSACTION-EXIT. TRANSACTION-EXIT. EXIT.

Modern approach with EVALUATE:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
EVALUATE TRANSACTION-TYPE WHEN 1 PERFORM SALE-ROUTINE WHEN 2 PERFORM RETURN-ROUTINE WHEN 3 PERFORM ADJUSTMENT-ROUTINE WHEN 4 PERFORM INQUIRY-ROUTINE WHEN OTHER PERFORM UNKNOWN-TRANSACTION END-EVALUATE.

Replacing Error Handling GO TOs

Legacy approach with GO TO for errors:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
PROCESS-RECORD. IF RECORD-TYPE NOT = "CUSTOMER" GO TO INVALID-RECORD-TYPE. IF CUSTOMER-ID = SPACES GO TO MISSING-CUSTOMER-ID. PERFORM UPDATE-CUSTOMER-RECORD. GO TO PROCESS-RECORD-EXIT. INVALID-RECORD-TYPE. DISPLAY "Invalid record type". MOVE "E001" TO ERROR-CODE. GO TO PROCESS-RECORD-EXIT. MISSING-CUSTOMER-ID. DISPLAY "Missing customer ID". MOVE "E002" TO ERROR-CODE. GO TO PROCESS-RECORD-EXIT. PROCESS-RECORD-EXIT. EXIT.

Modern structured approach:

cobol
1
2
3
4
5
6
7
8
9
10
11
PROCESS-RECORD. EVALUATE TRUE WHEN RECORD-TYPE NOT = "CUSTOMER" DISPLAY "Invalid record type" MOVE "E001" TO ERROR-CODE WHEN CUSTOMER-ID = SPACES DISPLAY "Missing customer ID" MOVE "E002" TO ERROR-CODE WHEN OTHER PERFORM UPDATE-CUSTOMER-RECORD END-EVALUATE.

Handling Early Termination

Legacy approach with GO TO:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SEARCH-TABLE. MOVE 1 TO TABLE-INDEX. MOVE "N" TO FOUND-FLAG. SEARCH-LOOP. IF TABLE-INDEX > TABLE-SIZE GO TO SEARCH-EXIT. IF TABLE-ENTRY(TABLE-INDEX) = SEARCH-VALUE MOVE "Y" TO FOUND-FLAG GO TO SEARCH-EXIT. ADD 1 TO TABLE-INDEX. GO TO SEARCH-LOOP. SEARCH-EXIT. EXIT.

Modern approach with EXIT PERFORM:

cobol
1
2
3
4
5
6
7
8
9
10
SEARCH-TABLE. MOVE "N" TO FOUND-FLAG. PERFORM VARYING TABLE-INDEX FROM 1 BY 1 UNTIL TABLE-INDEX > TABLE-SIZE IF TABLE-ENTRY(TABLE-INDEX) = SEARCH-VALUE MOVE "Y" TO FOUND-FLAG EXIT PERFORM END-IF END-PERFORM.

Exercises: Procedure Branching

Exercise 1: Refactoring GO TO Loops

Refactor the following legacy code to eliminate GO TO statements using structured programming techniques.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
* Legacy code with GO TO loops: PROCESS-TRANSACTIONS. READ TRANSACTION-FILE AT END GO TO END-TRANSACTIONS. IF TRANSACTION-CODE = "ADD" GO TO PROCESS-ADD. IF TRANSACTION-CODE = "UPDATE" GO TO PROCESS-UPDATE. IF TRANSACTION-CODE = "DELETE" GO TO PROCESS-DELETE. GO TO PROCESS-UNKNOWN. PROCESS-ADD. PERFORM ADD-ROUTINE. GO TO READ-NEXT. PROCESS-UPDATE. PERFORM UPDATE-ROUTINE. GO TO READ-NEXT. PROCESS-DELETE. PERFORM DELETE-ROUTINE. GO TO READ-NEXT. PROCESS-UNKNOWN. DISPLAY "Unknown transaction code: " TRANSACTION-CODE. GO TO READ-NEXT. READ-NEXT. GO TO PROCESS-TRANSACTIONS. END-TRANSACTIONS. CLOSE TRANSACTION-FILE. * Your task: * Refactor this code using: * 1. PERFORM UNTIL to handle the main processing loop * 2. EVALUATE to handle the transaction code logic * 3. Eliminate all GO TO statements

Exercise 2: Error Handling Refactoring

Refactor the following error-handling code to use structured techniques instead of GO TO.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
* Legacy error handling with GO TO: VALIDATE-CUSTOMER. IF CUSTOMER-ID = SPACES GO TO ERROR-MISSING-ID. IF CUSTOMER-NAME = SPACES GO TO ERROR-MISSING-NAME. IF CUSTOMER-TYPE NOT = "R" AND NOT = "W" GO TO ERROR-INVALID-TYPE. IF CREDIT-LIMIT < 0 GO TO ERROR-NEGATIVE-CREDIT. MOVE "Y" TO VALID-CUSTOMER. GO TO VALIDATION-EXIT. ERROR-MISSING-ID. MOVE "Customer ID is required" TO ERROR-MESSAGE. MOVE "E001" TO ERROR-CODE. MOVE "N" TO VALID-CUSTOMER. GO TO VALIDATION-EXIT. ERROR-MISSING-NAME. MOVE "Customer name is required" TO ERROR-MESSAGE. MOVE "E002" TO ERROR-CODE. MOVE "N" TO VALID-CUSTOMER. GO TO VALIDATION-EXIT. ERROR-INVALID-TYPE. MOVE "Invalid customer type" TO ERROR-MESSAGE. MOVE "E003" TO ERROR-CODE. MOVE "N" TO VALID-CUSTOMER. GO TO VALIDATION-EXIT. ERROR-NEGATIVE-CREDIT. MOVE "Credit limit cannot be negative" TO ERROR-MESSAGE. MOVE "E004" TO ERROR-CODE. MOVE "N" TO VALID-CUSTOMER. GO TO VALIDATION-EXIT. VALIDATION-EXIT. EXIT. * Your task: * Refactor this code using: * 1. EVALUATE TRUE for conditional validation * 2. Consider using functions or nested conditions * 3. Eliminate all GO TO statements * 4. Make the code more maintainable

Exercise 3: Rewriting Computed GO TO

Rewrite the following computed GO TO using structured programming techniques.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
* Legacy code with computed GO TO: PROCESS-MENU-OPTION. DISPLAY "Main Menu:". DISPLAY "1. Add Record". DISPLAY "2. Update Record". DISPLAY "3. Delete Record". DISPLAY "4. Display Record". DISPLAY "5. Exit". DISPLAY "Enter choice (1-5): ". ACCEPT MENU-CHOICE. IF MENU-CHOICE < 1 OR MENU-CHOICE > 5 DISPLAY "Invalid choice, try again" GO TO PROCESS-MENU-OPTION. GO TO MENU-ADD MENU-UPDATE MENU-DELETE MENU-DISPLAY MENU-EXIT DEPENDING ON MENU-CHOICE. MENU-ADD. PERFORM ADD-RECORD. GO TO PROCESS-MENU-OPTION. MENU-UPDATE. PERFORM UPDATE-RECORD. GO TO PROCESS-MENU-OPTION. MENU-DELETE. PERFORM DELETE-RECORD. GO TO PROCESS-MENU-OPTION. MENU-DISPLAY. PERFORM DISPLAY-RECORD. GO TO PROCESS-MENU-OPTION. MENU-EXIT. DISPLAY "Exiting program...". CLOSE ALL FILES. STOP RUN. * Your task: * Refactor this code using: * 1. EVALUATE for menu selection * 2. PERFORM UNTIL for the menu loop * 3. A clean program exit strategy * 4. No GO TO statements

Frequently Asked Questions

Test Your Knowledge

1. What is the main criticism of the GO TO statement in structured programming?

  • It is too slow for modern computers
  • It can create spaghetti code that is difficult to follow and maintain
  • It requires too much memory
  • It cannot be used with modern COBOL compilers

2. What does the EXIT statement do in COBOL?

  • Terminates the program immediately
  • Returns control to the operating system
  • Marks the logical end of a procedure
  • Exits the current iteration of a loop

3. What is the difference between STOP RUN and GOBACK?

  • They are identical in function
  • STOP RUN terminates the entire run unit while GOBACK returns to the calling program
  • GOBACK terminates the program while STOP RUN pauses execution
  • STOP RUN is for batch programs while GOBACK is for online programs

4. Which is the recommended modern alternative to GO TO for loop control?

  • EXIT statement
  • STOP RUN statement
  • PERFORM statement with UNTIL or TIMES
  • GOBACK statement

5. Which scope terminator is specifically designed for exiting a PERFORM loop before its normal termination?

  • END-PERFORM
  • EXIT PERFORM
  • EXIT PROGRAM
  • END-EXECUTE