Category: Cobol

  • Database Interface

    As of now, we have learnt the use of files in COBOL. Now, we will discuss how a COBOL program interacts with DB2. It involves the following terms −

    • Embedded SQL
    • DB2 Application Programming
    • Host Variables
    • SQLCA
    • SQL Queries
    • Cursors

    Embedded SQL

    Embedded SQL statements are used in COBOL programs to perform standard SQL operations. Embedded SQL statements are preprocessed by the SQL processor before the application program is compiled. COBOL is known as the Host Language. COBOL-DB2 applications are those applications that include both COBOL and DB2.

    Embedded SQL statements work like normal SQL statements with some minor changes. For example, the output of a query is directed to a predefined set of variables which are referred as Host Variables. An additional INTO clause is placed in the SELECT statement.

    DB2 Application Programming

    Following are rules to be followed while coding a COBOL-DB2 program −

    • All the SQL statements must be delimited between EXEC SQL and ENDEXEC..
    • SQL statements must be coded in Area B.
    • All the tables that are used in a program must be declared in the WorkingStorage Section. This is done by using the INCLUDE statement.
    • All SQL statements other than INCLUDE and DECLARE TABLE must appear in the Procedure Division.

    Host Variables

    Host variables are used for receiving data from a table or inserting data in a table. Host variables must be declared for all values that are to be passed between the program and the DB2. They are declared in the Working-Storage Section.

    Host variables cannot be group items, but they may be grouped together in host structure. They cannot be Renamed or Redefined. Using host variables with SQL statements, prefix them with a colon (:)..

    Syntax

    Following is the syntax to declare host variables and include tables in the Working-Storage section −

    DATA DIVISION.
       WORKING-STORAGE SECTION.
       
       EXEC SQL
       INCLUDE table-name
       END-EXEC.
    
       EXEC SQL BEGIN DECLARE SECTION
       END-EXEC.
       
       01 STUDENT-REC.
    
      05 STUDENT-ID PIC 9(4).
      05 STUDENT-NAME PIC X(25).
      05 STUDENT-ADDRESS X(50).
    EXEC SQL END DECLARE SECTION END-EXEC.

    SQLCA

    SQLCA is a SQL communication area through which DB2 passes the feedback of SQL execution to the program. It tells the program whether an execution was successful or not. There are a number of predefined variables under SQLCA like SQLCODE which contains the error code. The value ‘000’ in SQLCODE states a successful execution.

    Syntax

    Following is the syntax to declare an SQLCA in the Working-Storage section −

    DATA DIVISION.
    WORKING-STORAGE SECTION.
    	EXEC SQL
    	INCLUDE SQLCA
    	END-EXEC.
    

    SQL Queries

    Let’s assume we have one table named as ‘Student’ that contains Student-Id, Student-Name, and Student-Address.

    The STUDENT table contains the following data −

    Student Id		Student Name		Student Address
    1001 			   Mohtashim M.		Hyderabad
    1002			   Nishant Malik		Delhi
    1003 			   Amitabh Bachan		Mumbai
    1004			   Chulbul Pandey		Lucknow
    

    The following example shows the usage of SELECT query in a COBOL program −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       EXEC SQL
    
      INCLUDE SQLCA
    END-EXEC. EXEC SQL
      INCLUDE STUDENT
    END-EXEC. EXEC SQL BEGIN DECLARE SECTION END-EXEC.
      01 WS-STUDENT-REC.
         05 WS-STUDENT-ID PIC 9(4).
         05 WS-STUDENT-NAME PIC X(25).
         05 WS-STUDENT-ADDRESS X(50).
    EXEC SQL END DECLARE SECTION END-EXEC. PROCEDURE DIVISION. EXEC SQL
      SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS
      INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS FROM STUDENT
      WHERE STUDENT-ID=1004
    END-EXEC. IF SQLCODE = 0
      DISPLAY WS-STUDENT-RECORD
    ELSE DISPLAY 'Error' END-IF. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP001  EXEC PGM = IKJEFT01
    //STEPLIB  DD DSN = MYDATA.URMI.DBRMLIB,DISP = SHR
    //SYSPRINT DD SYSOUT=*
    //SYSUDUMP DD SYSOUT=*
    //SYSOUT   DD SYSOUT=*
    //SYSTSIN  DD *
       DSN SYSTEM(SSID)
       RUN PROGRAM(HELLO) PLAN(PLANNAME) -
       END
    /*

    When you compile and execute the above program, it produces the following result −

    1004 Chulbul Pandey		Lucknow
    

    The following example shows the usage of INSERT query in a COBOL program −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       EXEC SQL
       INCLUDE SQLCA
       END-EXEC.
       
       EXEC SQL
       INCLUDE STUDENT
       END-EXEC.
       
       EXEC SQL BEGIN DECLARE SECTION
       END-EXEC.
    
      01 WS-STUDENT-REC.
         05 WS-STUDENT-ID PIC 9(4).
         05 WS-STUDENT-NAME PIC X(25).
         05 WS-STUDENT-ADDRESS X(50).
    EXEC SQL END DECLARE SECTION END-EXEC. PROCEDURE DIVISION. MOVE 1005 TO WS-STUDENT-ID. MOVE 'TutorialsPoint' TO WS-STUDENT-NAME. MOVE 'Hyderabad' TO WS-STUDENT-ADDRESS. EXEC SQL
      INSERT INTO STUDENT(STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS)
      VALUES (:WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS)
    END-EXEC. IF SQLCODE = 0
      DISPLAY 'Record Inserted Successfully'
      DISPLAY WS-STUDENT-REC
    ELSE DISPLAY 'Error' END-IF. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP001  EXEC PGM = IKJEFT01
    //STEPLIB  DD DSN = MYDATA.URMI.DBRMLIB,DISP=SHR
    //SYSPRINT DD SYSOUT = *
    //SYSUDUMP DD SYSOUT = *
    //SYSOUT   DD SYSOUT = *
    //SYSTSIN  DD *
       DSN SYSTEM(SSID)
       RUN PROGRAM(HELLO) PLAN(PLANNAME) -
       END
    /*

    When you compile and execute the above program, it produces the following result −

    Record Inserted Successfully
    1005 TutorialsPoint		Hyderabad
    

    The following example shows the usage of UPDATE query in a COBOL program −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       
       EXEC SQL
       INCLUDE SQLCA
       END-EXEC.
       
       EXEC SQL
       INCLUDE STUDENT
       END-EXEC.
       
       EXEC SQL BEGIN DECLARE SECTION
       END-EXEC.
    
      01 WS-STUDENT-REC.
         05 WS-STUDENT-ID PIC 9(4).
         05 WS-STUDENT-NAME PIC X(25).
         05 WS-STUDENT-ADDRESS X(50).
    EXEC SQL END DECLARE SECTION END-EXEC. PROCEDURE DIVISION. MOVE 'Bangalore' TO WS-STUDENT-ADDRESS. EXEC SQL
      UPDATE STUDENT SET STUDENT-ADDRESS=:WS-STUDENT-ADDRESS
      WHERE STUDENT-ID = 1003
    END-EXEC. IF SQLCODE = 0
      DISPLAY 'Record Updated Successfully'
    ELSE DISPLAY 'Error' END-IF. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP001  EXEC PGM = IKJEFT01
    //STEPLIB  DD DSN = MYDATA.URMI.DBRMLIB,DISP = SHR
    //SYSPRINT DD SYSOUT = *
    //SYSUDUMP DD SYSOUT = *
    //SYSOUT   DD SYSOUT = *
    //SYSTSIN  DD *
       DSN SYSTEM(SSID)
       RUN PROGRAM(HELLO) PLAN(PLANNAME) -
       END
    /*

    When you compile and execute the above program, it produces the following result −

    Record Updated Successfully
    

    The following example shows the usage of DELETE query in a COBOL program −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    
       EXEC SQL
       INCLUDE SQLCA
       END-EXEC.
       
       EXEC SQL
       INCLUDE STUDENT
       END-EXEC.
       
       EXEC SQL BEGIN DECLARE SECTION
       END-EXEC.
    
      01 WS-STUDENT-REC.
         05 WS-STUDENT-ID PIC 9(4).
         05 WS-STUDENT-NAME PIC X(25).
         05 WS-STUDENT-ADDRESS X(50).
    EXEC SQL END DECLARE SECTION END-EXEC. PROCEDURE DIVISION. MOVE 1005 TO WS-STUDENT-ID. EXEC SQL
      DELETE FROM STUDENT
      WHERE STUDENT-ID=:WS-STUDENT-ID
    END-EXEC. IF SQLCODE = 0
      DISPLAY 'Record Deleted Successfully'
    ELSE DISPLAY 'Error' END-IF. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP001  EXEC PGM = IKJEFT01
    //STEPLIB  DD DSN = MYDATA.URMI.DBRMLIB,DISP=SHR
    //SYSPRINT DD SYSOUT = *
    //SYSUDUMP DD SYSOUT = *
    //SYSOUT   DD SYSOUT = *
    //SYSTSIN  DD *
       DSN SYSTEM(SSID)
       RUN PROGRAM(HELLO) PLAN(PLANNAME) -
       END
    /*

    When you compile and execute the above program, it produces the following result −

    Record Deleted Successfully
    

    Cursors

    Cursors are used to handle multiple row selections at a time. They are data structures that hold all the results of a query. They can be defined in the Working-Storage Section or the Procedure Division. Following are the operations associated with Cursor −

    • Declare
    • Open
    • Close
    • Fetch

    Declare Cursor

    Cursor declaration can be done in the Working-Storage Section or the Procedure Division. The first statement is the DECLARE statement which is a nonexecutable statement.

    EXEC SQL
       DECLARE STUDCUR CURSOR FOR
       SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS FROM STUDENT
       WHERE STUDENT-ID >:WS-STUDENT-ID
    END-EXEC.

    Open

    Before using a cursor, Open statement must be performed. The Open statement prepares the SELECT for execution.

    EXEC SQL
       OPEN STUDCUR
    END-EXEC.

    Close

    Close statement releases all the memory occupied by the cursor. It is mandatory to close a cursor before ending a program.

    EXEC SQL
       CLOSE STUDCUR
    END-EXEC.

    Fetch

    Fetch statement identifies the cursor and puts the value in the INTO clause. A Fetch statement is coded in loop as we get one row at a time.

    EXEC SQL
       FETCH STUDCUR
       INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS
    END-EXEC.

    The following example shows the usage of cursor to fetch all the records from the STUDENT table −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       
       EXEC SQL
       INCLUDE SQLCA
       END-EXEC.
       
       EXEC SQL
       INCLUDE STUDENT
       END-EXEC.
       
       EXEC SQL BEGIN DECLARE SECTION
       END-EXEC.
    
      01 WS-STUDENT-REC.
         05 WS-STUDENT-ID PIC 9(4).
         05 WS-STUDENT-NAME PIC X(25).
         05 WS-STUDENT-ADDRESS X(50).
    EXEC SQL END DECLARE SECTION END-EXEC. EXEC SQL
      DECLARE STUDCUR CURSOR FOR
      SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS FROM STUDENT
      WHERE STUDENT-ID >:WS-STUDENT-ID
    END-EXEC. PROCEDURE DIVISION. MOVE 1001 TO WS-STUDENT-ID. PERFORM UNTIL SQLCODE = 100 EXEC SQL
      FETCH STUDCUR
      INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS
    END-EXEC DISPLAY WS-STUDENT-REC END-PERFORM STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP001  EXEC PGM=IKJEFT01
    //STEPLIB  DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
    //SYSPRINT DD SYSOUT=*
    //SYSUDUMP DD SYSOUT=*
    //SYSOUT   DD SYSOUT=*
    //SYSTSIN  DD *
       DSN SYSTEM(SSID)
       RUN PROGRAM(HELLO) PLAN(PLANNAME) -
       END
    /*

    When you compile and execute the above program, it produces the following result −

    1001 Mohtashim M.		Hyderabad
    1002 Nishant Malik		Delhi
    1003 Amitabh Bachan		Mumbai
    1004 Chulbul Pandey		Lucknow
    
  • Internal Sort

    Sorting of data in a file or merging of two or more files is a common necessity in almost all business-oriented applications. Sorting is used for arranging records either in ascending or descending order, so that sequential processing can be performed. There are two techniques which are used for sorting files in COBOL −

    • External sort is used to sort files by using the SORT utility in JCL. We have discussed this in the JCL chapter. As of now, we will focus on internal sort.
    • Internal sort is used to sort files within a COBOL program. SORT verb is used to sort a file.

    Sort Verb

    Three files are used in the sort process in COBOL −

    • Input file is the file which we have to sort either in ascending or descending order.
    • Work file is used to hold records while the sort process is in progress. Input file records are transferred to the work file for the sorting process. This file should be defined in the File-Section under SD entry.
    • Output file is the file which we get after the sorting process. It is the final output of the Sort verb.

    Syntax

    Following is the syntax to sort a file −

    SORT work-file ON ASCENDING KEY rec-key1
       [ON DESCENDING KEY rec-key2]
    USING input-file GIVING output-file.
    

    SORT performs the following operations −

    • Opens work-file in I-O mode, input-file in the INPUT mode and output-file in the OUTPUT mode.
    • Transfers the records present in the input-file to the work-file.
    • Sorts the SORT-FILE in ascending/descending sequence by rec-key.
    • Transfers the sorted records from the work-file to the output-file.
    • Closes the input-file and the output-file and deletes the work-file.

    Example

    In the following example, INPUT is the input file which needs to be sorted in ascending order −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
    
      SELECT INPUT ASSIGN TO IN.
      SELECT OUTPUT ASSIGN TO OUT.
      SELECT WORK ASSIGN TO WRK.
    DATA DIVISION. FILE SECTION. FD INPUT.
      01 INPUT-STUDENT.
         05 STUDENT-ID-I PIC 9(5).
         05 STUDENT-NAME-I PIC A(25).
    FD OUTPUT.
      01 OUTPUT-STUDENT.
         05 STUDENT-ID-O PIC 9(5).
         05 STUDENT-NAME-O PIC A(25).
    SD WORK.
      01 WORK-STUDENT.
         05 STUDENT-ID-W PIC 9(5).
         05 STUDENT-NAME-W PIC A(25).
    PROCEDURE DIVISION. SORT WORK ON ASCENDING KEY STUDENT-ID-O USING INPUT GIVING OUTPUT. DISPLAY 'Sort Successful'. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO
    //IN DD DSN = INPUT-FILE-NAME,DISP = SHR
    //OUT DD DSN = OUTPUT-FILE-NAME,DISP = SHR
    //WRK DD DSN = &&TEMP

    When you compile and execute the above program, it produces the following result −

    Sort Successful
    

    Merge Verb

    Two or more identically sequenced files are combined using Merge statement. Files used in the merge process −

    • Input Files − Input-1, Input-2
    • Work File
    • Output File

    Syntax

    Following is the syntax to merge two or more files −

    MERGE work-file ON ASCENDING KEY rec-key1
       [ON DESCENDING KEY rec-key2]
    
    USING input-1, input-2 GIVING output-file.

    Merge performs the following operations −

    • Opens the work-file in I-O mode, input-files in the INPUT mode and output-file in the OUTPUT mode.
    • Transfers the records present in the input-files to the work-file.
    • Sorts the SORT-FILE in ascending/descending sequence by rec-key.
    • Transfers the sorted records from the work-file to the output-file.
    • Closes the input-file and the output-file and deletes the work-file.

    Example

    In the following example, INPUT1 and INPUT2 are the input files which are to be merged in ascending order −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
    
      SELECT INPUT1 ASSIGN TO IN1.
      SELECT INPUT2 ASSIGN TO IN2.
      SELECT OUTPUT ASSIGN TO OUT.
      SELECT WORK ASSIGN TO WRK.
    DATA DIVISION. FILE SECTION. FD INPUT1.
      01 INPUT1-STUDENT.
         05 STUDENT-ID-I1 PIC 9(5).
         05 STUDENT-NAME-I1 PIC A(25).
    FD INPUT2.
      01 INPUT2-STUDENT.
         05 STUDENT-ID-I2 PIC 9(5).
         05 STUDENT-NAME-I2 PIC A(25).
    FD OUTPUT.
      01 OUTPUT-STUDENT.
         05 STUDENT-ID-O PIC 9(5).
         05 STUDENT-NAME-O PIC A(25).
    SD WORK.
      01 WORK-STUDENT.
         05 STUDENT-ID-W PIC 9(5).
         05 STUDENT-NAME-W PIC A(25).
    PROCEDURE DIVISION. MERGE WORK ON ASCENDING KEY STUDENT-ID-O USING INPUT1, INPUT2 GIVING OUTPUT. DISPLAY 'Merge Successful'. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO
    //IN1 DD DSN=INPUT1-FILE-NAME,DISP=SHR
    //IN2 DD DSN=INPUT2-FILE-NAME,DISP=SHR
    //OUT DD DSN = OUTPUT-FILE-NAME,DISP=SHR
    //WRK DD DSN = &&TEMP

    When you compile and execute the above program, it produces the following result −

    Merge Successful
    
  • Subroutines

    Cobol subroutine is a program that can be compiled independently but cannot be executed independently. There are two types of subroutines: internal subroutines like Perform statements and external subroutines like CALL verb.

    Call Verb

    Call verb is used to transfer the control from one program to another program. The program that contains the CALL verb is the Calling Program and the program being called is known as the Called Program. Calling program execution will halt until the called program finishes the execution. Exit Program statement is used in the Called program to transfer the control back.

    Called Program Constraints

    Following are the called program requirements −

    • Linkage section must be defined in the called program. It consists of data elements passed in the program. The data items should not have Value clause. PIC clause must be compatible with the variables passed through the calling program.
    • Procedure division using has a list of variables passed from the calling program and the order must be same as mentioned in the Call verb.
    • Exit program statement is used in the called program to transfer the control back. It must be the last statement in the called program.

    The parameters can be passed between programs in two ways −

    • By Reference
    • By Content

    Call By Reference

    If the values of variables in the called program are modified, then their new values will reflect in the calling program. If BY clause is not specified, then variables are always passed by reference.

    Syntax

    Following is the syntax of calling subroutine by reference −

    CALL sub-prog-name USING variable-1, variable-2.
    

    Example

    Following example is the MAIN calling program and UTIL is the called program −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. MAIN.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-STUDENT-ID PIC 9(4) VALUE 1000.
       01 WS-STUDENT-NAME PIC A(15) VALUE 'Tim'.
    
    PROCEDURE DIVISION.
       CALL 'UTIL' USING WS-STUDENT-ID, WS-STUDENT-NAME.
       DISPLAY 'Student Id : ' WS-STUDENT-ID
       DISPLAY 'Student Name : ' WS-STUDENT-NAME
    STOP RUN.

    Called Program

    IDENTIFICATION DIVISION.
    PROGRAM-ID. UTIL.
    
    DATA DIVISION.
       LINKAGE SECTION.
       01 LS-STUDENT-ID PIC 9(4).
       01 LS-STUDENT-NAME PIC A(15).
    
    PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
       DISPLAY 'In Called Program'.
       MOVE 1111 TO LS-STUDENT-ID.
    EXIT PROGRAM.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = MAIN

    When you compile and execute the above program, it produces the following result −

    In Called Program
    Student Id : 1111
    Student Name : Tim
    

    Call By Content

    If the values of variables in the called program are modified, then their new values will not reflect in the calling program.

    Syntax

    Following is the syntax of calling subroutine by content −

    CALL sub-prog-name USING 
    BY CONTENT variable-1, BY CONTENT variable-2.
    

    Example

    Following example is the MAIN calling program and UTIL is the called program −

    IDENTIFICATION DIVISION.
    PROGRAM-ID. MAIN.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-STUDENT-ID PIC 9(4) VALUE 1000.
       01 WS-STUDENT-NAME PIC A(15) VALUE 'Tim'.
    
    PROCEDURE DIVISION.
       CALL 'UTIL' USING BY CONTENT WS-STUDENT-ID, BY CONTENT WS-STUDENT-NAME.
       DISPLAY 'Student Id : ' WS-STUDENT-ID
       DISPLAY 'Student Name : ' WS-STUDENT-NAME
    STOP RUN.

    Called Program

    IDENTIFICATION DIVISION.
    PROGRAM-ID. UTIL.
    
    DATA DIVISION.
       LINKAGE SECTION.
       01 LS-STUDENT-ID PIC 9(4).
       01 LS-STUDENT-NAME PIC A(15).
    
    PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
       DISPLAY 'In Called Program'.
       MOVE 1111 TO LS-STUDENT-ID.
    EXIT PROGRAM.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = MAIN

    When you compile and execute the above program, it produces the following result −

    In Called Program
    Student Id : 1000
    Student Name : Tim
    

    Types of Call

    There are two types of calls −

    • Static Call occurs when a program is compiled with the NODYNAM compiler option. A static called program is loaded into storage at compile time.
    • Dynamic Call occurs when a program is compiled with the DYNAM and NODLL compiler option. A dynamic called program is loaded into storage at runtime.
  • File Access Mode

    Till now, file organization schemes have been discussed. For each file organization scheme, different access modes can be used. Following are the types of file access modes −

    • Sequential Access
    • Random Access
    • Dynamic Access

    The syntaxes in this module, mentioned along with their respective terms, only refer to their usage in the program. The complete programs using these syntaxes would be discussed in the next chapter.

    Sequential Access

    When the access mode is sequential, the method of record retrieval changes as per the selected file organization.

    • For sequential files, records are accessed in the same order in which they were inserted.
    • For indexed files, the parameter used to fetch the records are the record key values.
    • For relative files, relative record keys are used to retrieve the records.

    Syntax

    Following is the syntax of sequential access mode −

    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS SEQUENTIAL
       ACCESS MODE IS SEQUENTIAL
    	
    	
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS INDEXED
       ACCESS MODE IS SEQUENTIAL
       RECORD KEY IS rec-key1
       ALTERNATE RECORD KEY IS rec-key2
    
    		
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS RELATIVE
       ACCESS MODE IS SEQUENTIAL
       RELATIVE KEY IS rec-key1
    

    Random Access

    When the access mode is RANDOM, the method of record retrieval changes as per the selected file organization.

    • For indexed files, records are accessed according to the value placed in a key field which can be primary or alternate key. There can be one or more alternate indexes.
    • For relative files , records are retrieved through relative record keys.

    Syntax

    Following is the syntax of random access mode −

    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS INDEXED
       ACCESS MODE IS RANDOM
       RECORD KEY IS rec-key1
       ALTERNATE RECORD KEY IS rec-key2
    
    		
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS RELATIVE
       ACCESS MODE IS RANDOM
       RELATIVE KEY IS rec-key1
    

    Dynamic Access

    Dynamic access supports both sequential and random access in the same program. With dynamic access, one file definition is used to perform both sequential and random processing like accessing some records in sequential order and other records by their keys.

    With relative and indexed files, the dynamic access mode allows you to switch back and forth between sequential access mode and random access mode while reading a file by using the NEXT phrase on the READ statement. NEXT and READ functionalities will be discussed in the next chapter.

    Syntax

    Following is the syntax of dynamic access mode −

    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS SEQUENTIAL
       ACCESS MODE IS DYNAMIC
       RECORD KEY IS rec-key1
       ALTERNATE RECORD KEY IS rec-key2
    
    		
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name
       ORGANIZATION IS RELATIVE
       ACCESS MODE IS DYNAMIC
       RELATIVE KEY IS rec-key1
    
  • File Organization

    File organization indicates how the records are organized in a file. There are different types of organizations for files so as to increase their efficiency of accessing the records. Following are the types of file organization schemes −

    • Sequential file organization
    • Indexed sequential file organization
    • Relative file organization

    The syntaxes in this module, mentioned along with their respective terms, only refer to their usage in the program. The complete programs using these syntaxes would be discussed in the chapter ‘File handling Verbs’.

    Sequential File Organization

    A sequential file consists of records that are stored and accessed in sequential order. Following are the key attributes of sequential file organization −

    • Records can be read in sequential order. For reading the 10th record, all the previous 9 records should be read.
    • Records are written in sequential order. A new record cannot be inserted in between. A new record is always inserted at the end of the file.
    • After placing a record into a sequential file, it is not possible to delete, shorten, or lengthen a record.
    • Order of the records, once inserted, can never be changed.
    • Updation of record is possible. A record can be overwritten, if the new record length is same as the old record length.
    • Sequential output files are good option for printing.

    Syntax

    Following is the syntax of sequential file organization −

    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name-jcl
       ORGANIZATION IS SEQUENTIAL
    

    Indexed Sequential File Organization

    An indexed sequential file consists of records that can be accessed sequentially. Direct access is also possible. It consists of two parts −

    • Data File contains records in sequential scheme.
    • Index File contains the primary key and its address in the data file.

    Following are the key attributes of sequential file organization −

    • Records can be read in sequential order just like in sequential file organization.
    • Records can be accessed randomly if the primary key is known. Index file is used to get the address of a record and then the record is fetched from the data file.
    • Sorted index is maintained in this file system which relates the key value to the position of the record in the file.
    • Alternate index can also be created to fetch the records.

    Syntax

    Following is the syntax of indexed sequential file organization −

    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name-jcl
       ORGANIZATION IS INDEXED
       RECORD KEY IS primary-key
       ALTERNATE RECORD KEY IS rec-key

    Relative File Organization

    A relative file consists of records ordered by their relative address. Following are the key attributes of relative file organization −

    • Records can be read in sequential order just like in sequential and indexed file organization.
    • Records can be accessed using relative key. Relative key represents the record’s location relative to the address of the start of the file.
    • Records can be inserted using relative key. Relative address is calculated using relative key.
    • Relative file provides the fastest access to the records.
    • The main disadvantage of this file system is that if some intermediate records are missing, they will also occupy space.

    Syntax

    Following is the syntax of relative file organization −

    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
       SELECT file-name ASSIGN TO dd-name-jcl
       ORGANIZATION IS RELATIVE
       RELATIVE KEY IS rec-key
  • File Handling

    The concept of files in COBOL is different from that in C/C++. While learning the basics of ‘File’ in COBOL, the concepts of both languages should not be corelated. Simple text files cannot be used in COBOL, instead PS (Physical Sequential) and VSAM files are used. PS files will be discussed in this module.

    To understand file handling in COBOL, one must know the basic terms. These terms only serve to understand the fundamentals of file handling. Further in depth terminology would be discussed in the chapter ‘File Handling Verbs’. Following are the basic terms −

    • Field
    • Record
    • Physical Record
    • Logical Record
    • File

    The following example helps in understanding these terms −

    Program Structure

    Field

    Field is used to indicate the data stored about an element. It represents a single element as shown in the above example such as student id, name, marks, total marks, and percentage. The number of characters in any field is known as field size, for example, student name can have 10 characters. Fields can have the following attributes −

    • Primary keys are those fields that are unique to each record and are used to identify a particular record. For example, in students marks file, each student will be having a unique student id which forms the primary key.
    • Secondary keys are unique or non-unique fields that are used to search for related data. For example, in students marks file, full name of student can be used as secondary key when student id is not known.
    • Descriptors fields are used to describe an entity. For example, in students marks file, marks and percentage fields that add meaning to the record are known descriptors.

    Record

    Record is a collection of fields that is used to describe an entity. One or more fields together form a record. For example, in students marks file, student id, name, marks, total marks, and percentage form one record. The cumulative size of all the fields in a record is known as the record size. The records present in a file may be of fixed length or variable length.

    Physical Record

    Physical record is the information that exists on the external device. It is also known as a block.

    Logical Record

    Logical record is the information used by the program. In COBOL programs, only one record can be handled at any point of time and it is called as logical record.

    File

    File is a collection of related records. For example, the students marks file consists of records of all the students.

  • Table Processing

    Arrays in COBOL are known as tables. An array is a linear data structure and is a collection of individual data items of same type. Data items of a table are internally sorted.

    Table Declaration

    Table is declared in Data Division. Occurs clause is used to define a table. Occurs clause indicates the repetition of data name definition. It can be used only with level numbers starting from 02 to 49. Do not use occurs clause with Redefines. Description of one-dimensional and two-dimensional table is as follows −

    One-Dimensional Table

    In a one-dimensional table, occurs clause is used only once in declaration. WSTABLE is the group item that contains table. WS-B names the table elements that occur 10 times.

    Syntax

    Following is the syntax for defining a one-dimensional table −

    01 WS-TABLE.
       05 WS-A PIC A(10) OCCURS 10 TIMES.
    

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-TABLE.
    
      05 WS-A PIC A(10) VALUE 'TUTORIALS' OCCURS 5 TIMES.     
    PROCEDURE DIVISION. DISPLAY "ONE-D TABLE : "WS-TABLE. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    ONE-D TABLE : TUTORIALS TUTORIALS TUTORIALS TUTORIALS TUTORIALS
    

    Two-Dimensional Table

    A two-dimensional table is created with both data elements being variable length. For reference, go through the syntax and then try to analyze the table. The first array (WS-A) can occur from 1 to 10 times and the inner array (WS-C) can occur from 1 to 5 times. For each entry of WS-A, there will be corresponding 5 entries of WS-C.

    Syntax

    Following is the syntax for defining a two-dimensional table −

    01 WS-TABLE.
       05 WS-A OCCURS 10 TIMES.
    
      10 WS-B PIC A(10).
      10 WS-C OCCURS 5 TIMES.
         15 WS-D PIC X(6).

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-TABLE.
    
      05 WS-A OCCURS 2 TIMES.
         10 WS-B PIC A(10) VALUE ' TUTORIALS'.
         10 WS-C OCCURS 2 TIMES.
            15 WS-D PIC X(6) VALUE ' POINT'.
    PROCEDURE DIVISION. DISPLAY "TWO-D TABLE : "WS-TABLE. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    TWO-D TABLE :  TUTORIALS POINT POINT TUTORIALS POINT POINT
    

    Subscript

    Table individual elements can be accessed by using subscript. Subscript values can range from 1 to the number of times the table occurs. A subscript can be any positive number. It does not require any declaration in data division. It is automatically created with occurs clause.

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-TABLE.
    
      05 WS-A OCCURS 3 TIMES.
         10 WS-B PIC A(2).
         10 WS-C OCCURS 2 TIMES.
            15 WS-D PIC X(3).
    PROCEDURE DIVISION. MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE. DISPLAY 'WS-TABLE : ' WS-TABLE. DISPLAY 'WS-A(1) : ' WS-A(1). DISPLAY 'WS-C(1,1) : ' WS-C(1,1). DISPLAY 'WS-C(1,2) : ' WS-C(1,2). DISPLAY 'WS-A(2) : ' WS-A(2). DISPLAY 'WS-C(2,1) : ' WS-C(2,1). DISPLAY 'WS-C(2,2) : ' WS-C(2,2). DISPLAY 'WS-A(3) : ' WS-A(3). DISPLAY 'WS-C(3,1) : ' WS-C(3,1). DISPLAY 'WS-C(3,2) : ' WS-C(3,2). STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    WS-TABLE  : 12ABCDEF34GHIJKL56MNOPQR
    WS-A(1)   : 12ABCDEF
    WS-C(1,1) : ABC
    WS-C(1,2) : DEF
    WS-A(2)   : 34GHIJKL
    WS-C(2,1) : GHI
    WS-C(2,2) : JKL
    WS-A(3)   : 56MNOPQR
    WS-C(3,1) : MNO
    WS-C(3,2) : PQR
    

    Index

    Table elements can also be accessed using index. An index is a displacement of element from the start of the table. An index is declared with Occurs clause using INDEXED BY clause. The value of index can be changed using SET statement and PERFORM Varying option.

    Syntax

    Following is the syntax for defining Index in a table −

    01 WS-TABLE.
       05 WS-A PIC A(10) OCCURS 10 TIMES INDEXED BY I.
    

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-TABLE.
    
      05 WS-A OCCURS 3 TIMES INDEXED BY I.
         10 WS-B PIC A(2).
         10 WS-C OCCURS 2 TIMES INDEXED BY J.
            15 WS-D PIC X(3).
    PROCEDURE DIVISION. MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE. PERFORM A-PARA VARYING I FROM 1 BY 1 UNTIL I >3 STOP RUN. A-PARA. PERFORM C-PARA VARYING J FROM 1 BY 1 UNTIL J>2. C-PARA. DISPLAY WS-C(I,J).

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    ABC
    DEF
    GHI
    JKL
    MNO
    PQR
    

    Set Statement

    Set statement is used to change the index value. Set verb is used to initialize, increment, or decrement the index value. It is used with Search and Search All to locate elements in table.

    Syntax

    Following is the syntax for using a Set statement −

    SET I J TO positive-number
    SET I TO J
    SET I TO 5
    SET I J UP BY 1
    SET J DOWN BY 5
    

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-TABLE.
    
      05 WS-A OCCURS 3 TIMES INDEXED BY I.
         10 WS-B PIC A(2).
         10 WS-C OCCURS 2 TIMES INDEXED BY J.
            15 WS-D PIC X(3).
    PROCEDURE DIVISION. MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE. SET I J TO 1. DISPLAY WS-C(I,J). SET I J UP BY 1. DISPLAY WS-C(I,J). STOP RUN.

    JCL to execute the above COBOL program.

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    ABC
    JKL
    

    Search

    Search is a linear search method, which is used to find elements inside the table. It can be performed on sorted as well as unsorted table. It is used only for tables declared by Index phrase. It starts with the initial value of index. If the searched element is not found, then the index is automatically incremented by 1 and it continues till the end of table.

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-TABLE.
    
      05 WS-A PIC X(1) OCCURS 18 TIMES INDEXED BY I.
    01 WS-SRCH PIC A(1) VALUE 'M'. PROCEDURE DIVISION. MOVE 'ABCDEFGHIJKLMNOPQR' TO WS-TABLE. SET I TO 1. SEARCH WS-A
      AT END DISPLAY 'M NOT FOUND IN TABLE'
      WHEN WS-A(I) = WS-SRCH
      DISPLAY 'LETTER M FOUND IN TABLE'
    END-SEARCH. STOP RUN.

    JCL to execute the above COBOL program.

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    LETTER M FOUND IN TABLE
    

    Search All

    Search All is a binary search method, which is used to find elements inside the table. Table must be in sorted order for Search All option. The index does not require initialization. In binary search, the table is divided into two halves and it determines in which half the searched element is present. This process repeats till the element is found or the end is reached.

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-TABLE.
    
      05 WS-RECORD OCCURS 10 TIMES ASCENDING KEY IS WS-NUM INDEXED BY I.
      10 WS-NUM PIC 9(2).
      10 WS-NAME PIC A(3).
    PROCEDURE DIVISION. MOVE '12ABC56DEF34GHI78JKL93MNO11PQR' TO WS-TABLE. SEARCH ALL WS-RECORD
     AT END DISPLAY 'RECORD NOT FOUND'
     WHEN WS-NUM(I) = 93
     DISPLAY 'RECORD FOUND '
     DISPLAY WS-NUM(I)
     DISPLAY WS-NAME(I)
    END-SEARCH.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    RECORD FOUND 
    93
    MNO
    
  • String Handling

    String handling statements in COBOL are used to do multiple functional operations on strings. Following are the string handling statements −

    • Inspect
    • String
    • Unstring

    Inspect

    Inspect verb is used to count or replace the characters in a string. String operations can be performed on alphanumeric, numeric, or alphabetic values. Inspect operations are performed from left to right. The options used for the string operations are as follows −

    Tallying

    Tallying option is used to count the string characters.

    Syntax

    Following is the syntax of Tallying option −

    INSPECT input-string
    TALLYING output-count FOR ALL CHARACTERS
    

    The parameters used are −

    • input-string − The string whose characters are to be counted.
    • output-count − Data item to hold the count of characters.

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-CNT1 PIC 9(2) VALUE 0.
       01 WS-CNT2 PIC 9(2) VALUE 0.
       01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'.
       
    PROCEDURE DIVISION.
       INSPECT WS-STRING TALLYING WS-CNT1 FOR CHARACTER.
       DISPLAY "WS-CNT1 : "WS-CNT1.
       INSPECT WS-STRING TALLYING WS-CNT2 FOR ALL 'A'.
       DISPLAY "WS-CNT2 : "WS-CNT2
       
    STOP RUN.

    JCL to execute the above COBOL program.

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    WS-CNT1 : 15
    WS-CNT2 : 06
    

    Replacing

    Replacing option is used to replace the string characters.

    Syntax

    Following is the syntax of Replacing option −

    INSPECT input-string REPLACING ALL char1 BY char2.
    

    The parameter used is −

    • input-string − The string whose characters are to be replaced from char1 to char2.

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'.
    
    PROCEDURE DIVISION.
       DISPLAY "OLD STRING : "WS-STRING.
       INSPECT WS-STRING REPLACING ALL 'A' BY 'X'.
       DISPLAY "NEW STRING : "WS-STRING.
       
    STOP RUN.

    JCL to execute the above COBOL program.

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    OLD STRING : ABCDACDADEAAAFF
    NEW STRING : XBCDXCDXDEXXXFF
    

    String

    String verb is used to concatenate the strings. Using STRING statement, two or more strings of characters can be combined to form a longer string. ‘Delimited By’ clause is compulsory.

    Syntax

    Following is the syntax of String verb −

    STRING ws-string1 DELIMITED BY SPACE
       ws-string2 DELIMITED BY SIZE
       INTO ws-destination-string
       WITH POINTER ws-count
       ON OVERFLOW DISPLAY message1
       NOT ON OVERFLOW DISPLAY message2
    END-STRING.

    Following are the details of the used parameters −

    • ws-string1 and ws-string2 : Input strings to be concatenated
    • ws-string : Output string
    • ws-count : Used to count the length of new concatenated string
    • Delimited specifies the end of string
    • Pointer and Overflow are optional

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-STRING PIC A(30).
       01 WS-STR1 PIC A(15) VALUE 'Tutorialspoint'.
       01 WS-STR2 PIC A(7) VALUE 'Welcome'.
       01 WS-STR3 PIC A(7) VALUE 'To AND'.
       01 WS-COUNT PIC 99 VALUE 1.
    
    PROCEDURE DIVISION.
       STRING WS-STR2 DELIMITED BY SIZE
    
      WS-STR3 DELIMITED BY SPACE
      WS-STR1 DELIMITED BY SIZE
      INTO WS-STRING 
      WITH POINTER WS-COUNT
      ON OVERFLOW DISPLAY 'OVERFLOW!' 
    END-STRING. DISPLAY 'WS-STRING : 'WS-STRING. DISPLAY 'WS-COUNT : 'WS-COUNT. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    WS-STRING : WelcomeToTutorialspoint       
    WS-COUNT : 25
    

    Unstring

    Unstring verb is used to split one string into multiple sub-strings. Delimited By clause is compulsory.

    Syntax

    Following is the syntax of Unstring verb −

    UNSTRING ws-string DELIMITED BY SPACE
    INTO ws-str1, ws-str2
    WITH POINTER ws-count
    ON OVERFLOW DISPLAY message
    NOT ON OVERFLOW DISPLAY message
    END-UNSTRING.

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-STRING PIC A(30) VALUE 'WELCOME TO TUTORIALSPOINT'.
       01 WS-STR1 PIC A(7).
       01 WS-STR2 PIC A(2).
       01 WS-STR3 PIC A(15).
       01 WS-COUNT PIC 99 VALUE 1.
    
    PROCEDURE DIVISION.
       UNSTRING WS-STRING DELIMITED BY SPACE
    
      INTO WS-STR1, WS-STR2, WS-STR3
    END-UNSTRING. DISPLAY 'WS-STR1 : 'WS-STR1. DISPLAY 'WS-STR2 : 'WS-STR2. DISPLAY 'WS-STR3 : 'WS-STR3. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    WS-STR1 : WELCOME
    WS-STR2 : TO
    WS-STR3 : TUTORIALSPOINT 
    
  • Loop Statements

    There are some tasks that need to be done over and over again like reading each record of a file till its end. The loop statements used in COBOL are −

    • Perform Thru
    • Perform Until
    • Perform Times
    • Perform Varying

    Perform Thru

    Perform Thru is used to execute a series of paragraph by giving the first and last paragraph names in the sequence. After executing the last paragraph, the control is returned back.

    In-line Perform

    Statements inside the PERFORM will be executed till END-PERFORM is reached.

    Syntax

    Following is the syntax of In-line perform −

    PERFORM 
       DISPLAY 'HELLO WORLD'
    END-PERFORM.
    

    Out-of-line Perform

    Here, a statement is executed in one paragraph and then the control is transferred to other paragraph or section.

    Syntax

    Following is the syntax of Out-of-line perform −

    PERFORM PARAGRAPH1 THRU PARAGRAPH2
    

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    PROCEDURE DIVISION.
       A-PARA.
       PERFORM DISPLAY 'IN A-PARA'
       END-PERFORM.
       PERFORM C-PARA THRU E-PARA.
       
       B-PARA.
       DISPLAY 'IN B-PARA'.
       STOP RUN.
       
       C-PARA.
       DISPLAY 'IN C-PARA'.
       
       D-PARA.
       DISPLAY 'IN D-PARA'.
       
       E-PARA.
       DISPLAY 'IN E-PARA'.

    JCL to execute the above COBOL program.

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    IN A-PARA
    IN C-PARA
    IN D-PARA
    IN E-PARA
    IN B-PARA
    

    Perform Until

    In ‘perform until’, a paragraph is executed until the given condition becomes true. ‘With test before’ is the default condition and it indicates that the condition is checked before the execution of statements in a paragraph.

    Syntax

    Following is the syntax of perform until −

    PERFORM A-PARA UNTIL COUNT=5
    
    PERFORM A-PARA WITH TEST BEFORE UNTIL COUNT=5
    
    PERFORM A-PARA WITH TEST AFTER UNTIL COUNT=5

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-CNT PIC 9(1) VALUE 0. 
    
    PROCEDURE DIVISION.
       A-PARA.
       PERFORM B-PARA WITH TEST AFTER UNTIL WS-CNT>3.
       STOP RUN.
       
       B-PARA.
       DISPLAY 'WS-CNT : 'WS-CNT.
       ADD 1 TO WS-CNT.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    WS-CNT : 0
    WS-CNT : 1
    WS-CNT : 2
    WS-CNT : 3
    

    Perform Times

    In ‘perform times’, a paragraph will be executed the number of times specified.

    Syntax

    Following is the syntax of perform times −

    PERFORM A-PARA 5 TIMES.
    

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    PROCEDURE DIVISION.
       A-PARA.
       PERFORM B-PARA 3 TIMES.
       STOP RUN.
       
       B-PARA.
       DISPLAY 'IN B-PARA'.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    IN B-PARA
    IN B-PARA
    IN B-PARA
    

    Perform Varying

    In perform varying, a paragraph will be executed till the condition in Until phrase becomes true.

    Syntax

    Following is the syntax of perform varying −

    PERFORM A-PARA VARYING A FROM 1 BY 1 UNTIL A = 5.
    

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-A PIC 9 VALUE 0.
    
    PROCEDURE DIVISION.
       A-PARA.
       PERFORM B-PARA VARYING WS-A FROM 1 BY 1 UNTIL WS-A=5
       STOP RUN.
       
       B-PARA.
       DISPLAY 'IN B-PARA ' WS-A.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    IN B-PARA 1
    IN B-PARA 2
    IN B-PARA 3
    IN B-PARA 4
    

    GO TO Statement

    GO TO statement is used to change the flow of execution in a program. In GO TO statements, transfer goes only in the forward direction. It is used to exit a paragraph. The different types of GO TO statements used are as follows −

    Unconditional GO TO

    GO TO para-name.
    

    Conditional GO TO

    GO TO para-1 para-2 para-3 DEPENDING ON x.
    

    If ‘x’ is equal to 1, then the control will be transferred to the first paragraph; and if ‘x’ is equal to 2, then the control will be transferred to the second paragraph, and so on.

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-A PIC 9 VALUE 2.
       
    PROCEDURE DIVISION.
       A-PARA.
       DISPLAY 'IN A-PARA'
       GO TO B-PARA.
       
       B-PARA.
       DISPLAY 'IN B-PARA '.
       GO TO C-PARA D-PARA DEPENDING ON WS-A.
       
       C-PARA.
       DISPLAY 'IN C-PARA '.
       
       D-PARA.
       DISPLAY 'IN D-PARA '.
       STOP RUN.

    JCL to execute the above COBOL program:

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result:

    IN A-PARA
    IN B-PARA 
    IN D-PARA 
    
  • Conditional Statements

    Conditional statements are used to change the execution flow depending on certain conditions specified by the programmer. Conditional statements will always evaluate to true or false. Conditions are used in IF, Evaluate, and Perform statements. The different types of conditions are as follows −

    • IF Condition Statement
    • Relation Condition
    • Sign Condition
    • Class Condition
    • Condition-Name Condition
    • Negated Condition
    • Combined Condition

    IF Condition Statement

    IF statement checks for conditions. If a condition is true, the IF block is executed; and if the condition is false, the ELSE block is executed.

    END-IF is used to end the IF block. To end the IF block, a period can be used instead of END-IF. But it is always preferable to use END-IF for multiple IF blocks.

    Nested-IF − IF blocks appearing inside another IF block. There is no limit to the depth of nested IF statements.

    Syntax

    Following is the syntax of IF condition statements −

    IF [condition] THEN
       [COBOL statements]
    ELSE
       [COBOL statements]
    END-IF.
    

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1 PIC 9(9).
       01 WS-NUM2 PIC 9(9).
       01 WS-NUM3 PIC 9(5).
       01 WS-NUM4 PIC 9(6).
    
    PROCEDURE DIVISION.
       A000-FIRST-PARA.
       MOVE 25 TO WS-NUM1 WS-NUM3.
       MOVE 15 TO WS-NUM2 WS-NUM4.
       
       IF WS-NUM1 > WS-NUM2 THEN
    
      DISPLAY 'IN LOOP 1 - IF BLOCK'
      
      IF WS-NUM3 = WS-NUM4 THEN
         DISPLAY 'IN LOOP 2 - IF BLOCK'
      ELSE
         DISPLAY 'IN LOOP 2 - ELSE BLOCK'
      END-IF
      
    ELSE
      DISPLAY 'IN LOOP 1 - ELSE BLOCK'
    END-IF. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    IN LOOP 1 - IF BLOCK
    IN LOOP 2 - ELSE BLOCK
    

    Relation Condition

    Relation condition compares two operands, either of which can be an identifier, literal, or arithmetic expression. Algebraic comparison of numeric fields is done regardless of size and usage clause.

    For non-numeric operands

    If two non-numeric operands of equal size are compared, then the characters are compared from left with the corresponding positions till the end is reached. The operand containing greater number of characters is declared greater.

    If two non-numeric operands of unequal size are compared, then the shorter data item is appended with spaces at the end till the size of the operands becomes equal and then compared according to the rules mentioned in the previous point.

    Syntax

    Given below is the syntax of Relation condition statements −

    [Data Name/Arithmetic Operation]
    
       [IS] [NOT] 
    
    [Equal to (=),Greater than (>), Less than (<), 
    Greater than or Equal (>=), Less than or equal (<=) ]
    
    [Data Name/Arithmetic Operation] 
    

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1 PIC 9(9).
       01 WS-NUM2 PIC 9(9).
    
    PROCEDURE DIVISION.
       A000-FIRST-PARA.
       MOVE 25 TO WS-NUM1.
       MOVE 15 TO WS-NUM2.
       
       IF WS-NUM1 IS GREATER THAN OR EQUAL TO WS-NUM2 THEN
    
      DISPLAY 'WS-NUM1 IS GREATER THAN WS-NUM2'
    ELSE
      DISPLAY 'WS-NUM1 IS LESS THAN WS-NUM2'
    END-IF. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program it produces the following result −

    WS-NUM1 IS GREATER THAN WS-NUM2
    

    Sign Condition

    Sign condition is used to check the sign of a numeric operand. It determines whether a given numeric value is greater than, less than, or equal to ZERO.

    Syntax

    Following is the syntax of Sign condition statements −

    [Data Name/Arithmetic Operation] 
    
       [IS] [NOT] 
    
    [Positive, Negative or Zero]
    
    [Data Name/Arithmetic Operation]
    

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1 PIC S9(9) VALUE -1234.
       01 WS-NUM2 PIC S9(9) VALUE 123456.
    
    PROCEDURE DIVISION.
       A000-FIRST-PARA.
       IF WS-NUM1 IS POSITIVE THEN
    
      DISPLAY 'WS-NUM1 IS POSITIVE'.
      
    IF WS-NUM1 IS NEGATIVE THEN
      DISPLAY 'WS-NUM1 IS NEGATIVE'.
      
    IF WS-NUM1 IS ZERO THEN
      DISPLAY 'WS-NUM1 IS ZERO'.
      
    IF WS-NUM2 IS POSITIVE THEN
      DISPLAY 'WS-NUM2 IS POSITIVE'.
    STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program it produces the following result −

    WS-NUM1 IS NEGATIVE
    WS-NUM2 IS POSITIVE
    

    Class Condition

    Class condition is used to check if an operand contains only alphabets or numeric data. Spaces are considered in ALPHABETIC, ALPHABETIC-LOWER, and ALPHABETIC-UPPER.

    Syntax

    Following is the syntax of Class condition statements −

    [Data Name/Arithmetic Operation>]
    
       [IS] [NOT] 
    
    [NUMERIC, ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER]
    
    [Data Name/Arithmetic Operation]
    

    Example

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1 PIC X(9) VALUE 'ABCD '.
       01 WS-NUM2 PIC 9(9) VALUE 123456789.
    
    PROCEDURE DIVISION.
       A000-FIRST-PARA.
       
       IF WS-NUM1 IS ALPHABETIC THEN
    
      DISPLAY 'WS-NUM1 IS ALPHABETIC'.
      
    IF WS-NUM1 IS NUMERIC THEN
      DISPLAY 'WS-NUM1 IS NUMERIC'.
      
    IF WS-NUM2 IS NUMERIC THEN
      DISPLAY 'WS-NUM2 IS NUMERIC'.
    STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    WS-NUM1 IS ALPHABETIC
    WS-NUM2 IS NUMERIC
    

    Condition-name Condition

    A condition-name is a user-defined name. It contains a set of values specified by the user. It behaves like Boolean variables. They are defined with level number 88. It will not have a PIC clause.

    Syntax

    Following is the syntax of user-defined condition statements −

    88 [Condition-Name] VALUE [IS, ARE] [LITERAL] [THRU LITERAL].
    

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM PIC 9(3).
       88 PASS VALUES ARE 041 THRU 100.
       88 FAIL VALUES ARE 000 THRU 40.
    
    PROCEDURE DIVISION.
       A000-FIRST-PARA.
       MOVE 65 TO WS-NUM.
       
       IF PASS 
    
      DISPLAY 'Passed with ' WS-NUM ' marks'.
      
    IF FAIL
      DISPLAY 'FAILED with ' WS-NUM 'marks'.
      
    STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    Passed with 065 marks
    

    Negated Condition

    Negated condition is given by using the NOT keyword. If a condition is true and we have given NOT in front of it, then its final value will be false.

    Syntax

    Following is the syntax of Negated condition statements −

    IF NOT [CONDITION] 
       COBOL Statements
    END-IF.
    

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1 PIC 9(2) VALUE 20.
       01 WS-NUM2 PIC 9(9) VALUE 25.
    
    PROCEDURE DIVISION.
       A000-FIRST-PARA.
       
       IF NOT WS-NUM1 IS LESS THAN WS-NUM2 THEN
    
      DISPLAY 'IF-BLOCK'
    ELSE
      DISPLAY 'ELSE-BLOCK'
    END-IF. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    ELSE-BLOCK
    

    Combined Condition

    A combined condition contains two or more conditions connected using logical operators AND or OR.

    Syntax

    Following is the syntax of combined condition statements −

    IF [CONDITION] AND [CONDITION]
       COBOL Statements
    END-IF.
    

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1 PIC 9(2) VALUE 20.
       01 WS-NUM2 PIC 9(2) VALUE 25.
       01 WS-NUM3 PIC 9(2) VALUE 20.
    
    PROCEDURE DIVISION.
       A000-FIRST-PARA.
       
       IF WS-NUM1 IS LESS THAN WS-NUM2 AND WS-NUM1=WS-NUM3 THEN
    
      DISPLAY 'Both condition OK'
    ELSE
      DISPLAY 'Error'
    END-IF. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    Both condition OK
    

    Evaluate Verb

    Evaluate verb is a replacement of series of IF-ELSE statement. It can be used to evaluate more than one condition. It is similar to SWITCH statement in C programs.

    Example

    Live Demo

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO.
    
    DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-A PIC 9 VALUE 0.
       
    PROCEDURE DIVISION.
       MOVE 3 TO WS-A.
       
       EVALUATE TRUE
    
      WHEN WS-A &gt; 2
         DISPLAY 'WS-A GREATER THAN 2'
      WHEN WS-A &lt; 0
         DISPLAY 'WS-A LESS THAN 0'
      WHEN OTHER
         DISPLAY 'INVALID VALUE OF WS-A'
    END-EVALUATE. STOP RUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO

    When you compile and execute the above program, it produces the following result −

    WS-A GREATER THAN 2