GO TO Statement
The GO TO
statement transfers controls to a named paragraph or section in the Procedure Division.
Format 1
GO TO procedure-name
Format 2
GO TO { proc-1 } ... DEPENDING ON numeric-data-1
Syntax:
proc-n
is the name of a procedure or section in the program.numeric-data-n
is a numeric data item.
General Rules:
- The Format 1
GOTO
statement transfers control to the named paragraph or section. If a section is named, control is transferred to the first paragraph in the section. - In a Format 2
GOTO {list of paragraphs} DEPENDING
statement, a list of paragraphs is followed by a numeric data item. The ordinal position of the paragraph in the list must correspond with the value ofnumeric-data-1
in order for it to be selected as the target of theGOTO
statement. In the Code Sample below, theGOTO
statement is followed by two procedure names, but since theGOTO-DEP-VAR
is set to 1, theGOTO
statement will transfer control to the first one. - If the value of
numeric-data-1
is less than 1 or greater than the number of listedGOTO
targets, the operation is not executed, and control passes to the next statement.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. GOTO-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 GOTO-FLAG PIC 9 VALUE 1.
77 GOTO-DEP-VAR PIC 9 VALUE 1.
77 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
* GO TO PROCEDURE-NAME
IF GOTO-FLAG = 1
GOTO GOTO-1-EXIT
END-IF.
* GO TO { PROCEDURE-NAME } ... DEPENDING ON IDENTIFIER
GOTO DEP-1-EXIT, DEP-2-EXIT DEPENDING ON GOTO-DEP-VAR.
77 GOTO-DEP-VAR PIC 9 VALUE 1.
77 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
* GO TO PROCEDURE-NAME
IF GOTO-FLAG = 1
GOTO GOTO-1-EXIT
END-IF.
* GO TO { PROCEDURE-NAME } ... DEPENDING ON IDENTIFIER
GOTO DEP-1-EXIT, DEP-2-EXIT DEPENDING ON GOTO-DEP-VAR.
GOTO-1-EXIT.
DISPLAY "GOTO-1 EXIT!" LINE 10 COL 10.
ACCEPT DUMMY LINE 10 COL 30.
STOP RUN.
DEP-1-EXIT.
DISPLAY "DEP-1 EXIT!" LINE 10 COL 10.
ACCEPT DUMMY LINE 10 COL 30.
STOP RUN.
DEP-2-EXIT.
DISPLAY "DEP-2 EXIT!" LINE 10 COL 10.
ACCEPT DUMMY LINE 10 COL 30.
STOP RUN.