GO TO Statement in COBOL

Introduction

  • To change the flow of execution in a program GO TO statement is used.
  • Transfer the program control only in the forward direction in GO TO statement.
  • To exit a paragraph GO TO statement is used.

Syntax

For unconditional GO TO
GO TO para-name.

For Conditional GO TO
GO TO para-1 para-2 para-3 DEPENDING ON n.

In above syntax, if the value of n is 1, then transfer the control of program to first paragraph and so on. (for n = 2, 3..)

Example : Program to demonstrate GO TO Statement

IDENTIFICATION DIVISION.
PROGRAM-ID. GOTO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 n PIC 9 VALUE 2.
   
PROCEDURE DIVISION.
   PARA-A.
   DISPLAY 'IN PARA-A'
   GO TO PARA-C.
   
   PARA-B.
   DISPLAY 'IN PARA-B '.
   
   PARA-C.
   DISPLAY 'IN PARA-C '.
   GO TO PARA-E PARA-F PARA-G DEPENDING ON n.
   
   PARA-D.
   DISPLAY 'IN PARA-D '.
   
   PARA-E.
   DISPLAY 'IN PARA-E '.
   
   PARA-F.
   DISPLAY 'IN PARA-F '.
   
   PARA-G.
   DISPLAY 'IN PARA-G '.
   
   STOP RUN.


Output:
IN PARA-A
IN PARA-C
IN PARA-F
IN PARA-G