GO TO Statement [ HP COBOL II Reference Manual ]

HP COBOL II Reference Manual

GO TO Statement 

The GO TO statement transfers control from one part of the PROCEDURE
DIVISION to another.

The GO TO statement has two formats:

     GO TO [procedure-name-1]

     GO TO {procedure-name-1}   DEPENDING ON identifier-1 

Parameters 

procedure-name-  and its subsequent occurrences are names of procedures
1                within the PROCEDURE DIVISION of your program.

identifier-1     the name of a numeric elementary data item that has no
                 positions to the right of the decimal point.

The first format of the GO TO statement transfers control to the
procedure named by procedure-name-1 or, if no procedure is named, to the
procedure specified in a previously executed ALTER statement.  An ALTER
statement must be issued for this type of GO TO statement before it is
executed if no procedure is named in it.  This also implies that a GO TO
statement without a procedure name specification must make up the only
sentence in a paragraph.  Refer to the ALTER statement description in
this chapter for other restrictions.

If the first format of the GO TO statement does specify a procedure name
and if it appears in a sequence of imperative statements within a
sentence, it must be the last statement in that sentence.

In the second format of the GO TO statement, identifier must be the name
of a numeric elementary item described with no positions to the right of
the decimal point.  It is used to determine which procedure is to be
executed.  If the contents of identifier is an integer in the range one
to n, where n is the number of procedure names appearing in the GO TO
statement, then control passes to the procedure in the position
corresponding to the value of identifier.  Otherwise, no transfer occurs
and control passes to the next statement following the GO TO statement.

Examples 

     WORKING-STORAGE SECTION.
     01 SELECTOR            PIC 9(3).
           .
           .
           .

     PROCEDURE DIVISION.
           .
           .
           .
          ALTER GO-PARA TO PROCEED TO WHICH.
           .
           .
           .
     GO-PARA.  GO TO.
     AFTER-GO-PARA.
           .
           .
           .
          GO TO UNDER, OVER, EXACT DEPENDING ON SELECTOR.
          DISPLAY "SELECTOR OUT OF RANGE - VALUE IS ", SELECTOR.
           .
           .
           .
     UNDER.
           .
           .
           .
     OVER.
           .
           .
           .
     EXACT.
           .
           .
           .
     WHICH.
           .
           .
           .

In the above illustration, the GO TO statement in the GO-PARA paragraph
is equivalent to GO TO WHICH because of the ALTER statement preceding it.

The second GO TO statement branches to UNDER, OVER, or EXACT, depending
upon whether SELECTOR has a value of 1, 2, or 3 respectively.  If
SELECTOR has any other value, the DISPLAY statement is executed.