Category: Basic

  • 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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-TABLE.05 WS-A PICA(10)VALUE'TUTORIALS'OCCURS5TIMES.PROCEDUREDIVISION.DISPLAY"ONE-D TABLE : "WS-TABLE.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-TABLE.05 WS-A OCCURS2TIMES.10 WS-B PICA(10)VALUE' TUTORIALS'.10 WS-C OCCURS2TIMES.15 WS-D PICX(6)VALUE' POINT'.PROCEDUREDIVISION.DISPLAY"TWO-D TABLE : "WS-TABLE.STOPRUN.

    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.

    Example

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-TABLE.05 WS-A OCCURS3TIMES.10 WS-B PICA(2).10 WS-C OCCURS2TIMES.15 WS-D PICX(3).PROCEDUREDIVISION.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).STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-TABLE.05 WS-A OCCURS3TIMESINDEXEDBY I.10 WS-B PICA(2).10 WS-C OCCURS2TIMESINDEXEDBY J.15 WS-D PICX(3).PROCEDUREDIVISION.MOVE'12ABCDEF34GHIJKL56MNOPQR'TO WS-TABLE.PERFORM A-PARA VARYING I FROM1BY1UNTIL I >3STOPRUN.
       
       A-PARA.PERFORM C-PARA VARYING J FROM1BY1UNTIL 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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-TABLE.05 WS-A OCCURS3TIMESINDEXEDBY I.10 WS-B PICA(2).10 WS-C OCCURS2TIMESINDEXEDBY J.15 WS-D PICX(3).PROCEDUREDIVISION.MOVE'12ABCDEF34GHIJKL56MNOPQR'TO WS-TABLE.SET I J TO1.DISPLAY WS-C(I,J).SET I J UPBY1.DISPLAY WS-C(I,J).STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-TABLE.05 WS-A PICX(1)OCCURS18TIMESINDEXEDBY I.01 WS-SRCH PICA(1)VALUE'M'.PROCEDUREDIVISION.MOVE'ABCDEFGHIJKLMNOPQR'TO WS-TABLE.SET I TO1.SEARCH WS-A
    
      ATENDDISPLAY'M NOT FOUND IN TABLE'WHEN WS-A(I)= WS-SRCH
      DISPLAY'LETTER M FOUND IN TABLE'END-SEARCH.STOPRUN.</pre>

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-TABLE.05 WS-RECORD OCCURS10TIMESASCENDINGKEYIS WS-NUM INDEXEDBY I.10 WS-NUM PIC9(2).10 WS-NAME PICA(3).PROCEDUREDIVISION.MOVE'12ABC56DEF34GHI78JKL93MNO11PQR'TO WS-TABLE.SEARCHALL WS-RECORD
    
     ATENDDISPLAY'RECORD NOT FOUND'WHEN WS-NUM(I)=93DISPLAY'RECORD FOUND 'DISPLAY WS-NUM(I)DISPLAY WS-NAME(I)END-SEARCH.</pre>

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-CNT1 PIC9(2)VALUE0.01 WS-CNT2 PIC9(2)VALUE0.01 WS-STRING PICX(15)VALUE'ABCDACDADEAAAFF'.PROCEDUREDIVISION.INSPECT WS-STRING TALLYING WS-CNT1 FORCHARACTER.DISPLAY"WS-CNT1 : "WS-CNT1.INSPECT WS-STRING TALLYING WS-CNT2 FORALL'A'.DISPLAY"WS-CNT2 : "WS-CNT2
       
    STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-STRING PICX(15)VALUE'ABCDACDADEAAAFF'.PROCEDUREDIVISION.DISPLAY"OLD STRING : "WS-STRING.INSPECT WS-STRING REPLACINGALL'A'BY'X'.DISPLAY"NEW STRING : "WS-STRING.STOPRUN.

    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 DELIMITEDBYSPACE
       ws-string2 DELIMITEDBYSIZEINTO ws-destination-string
       WITHPOINTER ws-count
       ONOVERFLOWDISPLAY message1
       NOTONOVERFLOWDISPLAY 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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-STRING PICA(30).01 WS-STR1 PICA(15)VALUE'Tutorialspoint'.01 WS-STR2 PICA(7)VALUE'Welcome'.01 WS-STR3 PICA(7)VALUE'To AND'.01 WS-COUNT PIC99VALUE1.PROCEDUREDIVISION.STRING WS-STR2 DELIMITEDBYSIZE
    
      WS-STR3 DELIMITEDBYSPACE
      WS-STR1 DELIMITEDBYSIZEINTO WS-STRING 
      WITHPOINTER WS-COUNT
      ONOVERFLOWDISPLAY'OVERFLOW!'END-STRING.DISPLAY'WS-STRING : 'WS-STRING.DISPLAY'WS-COUNT : 'WS-COUNT.STOPRUN.</pre>

    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 DELIMITEDBYSPACEINTO ws-str1, ws-str2
    WITHPOINTER ws-count
    ONOVERFLOWDISPLAYmessageNOTONOVERFLOWDISPLAYmessageEND-UNSTRING.

    Example

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-STRING PICA(30)VALUE'WELCOME TO TUTORIALSPOINT'.01 WS-STR1 PICA(7).01 WS-STR2 PICA(2).01 WS-STR3 PICA(15).01 WS-COUNT PIC99VALUE1.PROCEDUREDIVISION.UNSTRING WS-STRING DELIMITEDBYSPACEINTO WS-STR1, WS-STR2, WS-STR3
       END-UNSTRING.DISPLAY'WS-STR1 : 'WS-STR1.DISPLAY'WS-STR2 : 'WS-STR2.DISPLAY'WS-STR3 : 'WS-STR3.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.PROCEDUREDIVISION.
       A-PARA.PERFORMDISPLAY'IN A-PARA'END-PERFORM.PERFORM C-PARA THRU E-PARA.
       
       B-PARA.DISPLAY'IN B-PARA'.STOPRUN.
       
       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 UNTILCOUNT=5PERFORM A-PARA WITHTESTBEFOREUNTILCOUNT=5PERFORM A-PARA WITHTESTAFTERUNTILCOUNT=5

    Example

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-CNT PIC9(1)VALUE0.PROCEDUREDIVISION.
       A-PARA.PERFORM B-PARA WITHTESTAFTERUNTIL WS-CNT>3.STOPRUN.
       
       B-PARA.DISPLAY'WS-CNT : 'WS-CNT.ADD1TO 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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.PROCEDUREDIVISION.
       A-PARA.PERFORM B-PARA 3TIMES.STOPRUN.
       
       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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-A PIC9VALUE0.PROCEDUREDIVISION.
       A-PARA.PERFORM B-PARA VARYING WS-A FROM1BY1UNTIL WS-A=5STOPRUN.
       
       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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-A PIC9VALUE2.PROCEDUREDIVISION.
       A-PARA.DISPLAY'IN A-PARA'GOTO B-PARA.
       
       B-PARA.DISPLAY'IN B-PARA '.GOTO C-PARA D-PARA DEPENDINGON WS-A.
       
       C-PARA.DISPLAY'IN C-PARA '.
       
       D-PARA.DISPLAY'IN D-PARA '.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9).01 WS-NUM2 PIC9(9).01 WS-NUM3 PIC9(5).01 WS-NUM4 PIC9(6).PROCEDUREDIVISION.
       A000-FIRST-PARA.MOVE25TO WS-NUM1 WS-NUM3.MOVE15TO WS-NUM2 WS-NUM4.IF WS-NUM1 > WS-NUM2 THENDISPLAY'IN LOOP 1 - IF BLOCK'IF WS-NUM3 = WS-NUM4 THENDISPLAY'IN LOOP 2 - IF BLOCK'ELSEDISPLAY'IN LOOP 2 - ELSE BLOCK'END-IFELSEDISPLAY'IN LOOP 1 - ELSE BLOCK'END-IF.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9).01 WS-NUM2 PIC9(9).PROCEDUREDIVISION.
       A000-FIRST-PARA.MOVE25TO WS-NUM1.MOVE15TO WS-NUM2.IF WS-NUM1 ISGREATERTHANOREQUALTO WS-NUM2 THENDISPLAY'WS-NUM1 IS GREATER THAN WS-NUM2'ELSEDISPLAY'WS-NUM1 IS LESS THAN WS-NUM2'END-IF.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PICS9(9)VALUE-1234.01 WS-NUM2 PICS9(9)VALUE123456.PROCEDUREDIVISION.
       A000-FIRST-PARA.IF WS-NUM1 ISPOSITIVETHENDISPLAY'WS-NUM1 IS POSITIVE'.IF WS-NUM1 ISNEGATIVETHENDISPLAY'WS-NUM1 IS NEGATIVE'.IF WS-NUM1 ISZEROTHENDISPLAY'WS-NUM1 IS ZERO'.IF WS-NUM2 ISPOSITIVETHENDISPLAY'WS-NUM2 IS POSITIVE'.STOPRUN.

    JCL to execute the above COBOL program −

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PICS9(9)VALUE-1234.01 WS-NUM2 PICS9(9)VALUE123456.PROCEDUREDIVISION.
       A000-FIRST-PARA.IF WS-NUM1 ISPOSITIVETHENDISPLAY'WS-NUM1 IS POSITIVE'.IF WS-NUM1 ISNEGATIVETHENDISPLAY'WS-NUM1 IS NEGATIVE'.IF WS-NUM1 ISZEROTHENDISPLAY'WS-NUM1 IS ZERO'.IF WS-NUM2 ISPOSITIVETHENDISPLAY'WS-NUM2 IS POSITIVE'.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PICX(9)VALUE'ABCD '.01 WS-NUM2 PIC9(9)VALUE123456789.PROCEDUREDIVISION.
       A000-FIRST-PARA.IF WS-NUM1 ISALPHABETICTHENDISPLAY'WS-NUM1 IS ALPHABETIC'.IF WS-NUM1 ISNUMERICTHENDISPLAY'WS-NUM1 IS NUMERIC'.IF WS-NUM2 ISNUMERICTHENDISPLAY'WS-NUM2 IS NUMERIC'.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM PIC9(3).88 PASS VALUESARE041THRU100.88 FAIL VALUESARE000THRU40.PROCEDUREDIVISION.
       A000-FIRST-PARA.MOVE65TO WS-NUM.IF PASS 
    
      DISPLAY'Passed with ' WS-NUM ' marks'.IF FAIL 
      DISPLAY'FAILED with ' WS-NUM 'marks'.STOPRUN.</pre>

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(2)VALUE20.01 WS-NUM2 PIC9(9)VALUE25.PROCEDUREDIVISION.
       A000-FIRST-PARA.IFNOT WS-NUM1 ISLESSTHAN WS-NUM2 THENDISPLAY'IF-BLOCK'ELSEDISPLAY'ELSE-BLOCK'END-IF.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(2)VALUE20.01 WS-NUM2 PIC9(2)VALUE25.01 WS-NUM3 PIC9(2)VALUE20.PROCEDUREDIVISION.
       A000-FIRST-PARA.IF WS-NUM1 ISLESSTHAN WS-NUM2 AND WS-NUM1=WS-NUM3 THENDISPLAY'Both condition OK'ELSEDISPLAY'Error'END-IF.STOPRUN.

    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

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-A PIC9VALUE0.PROCEDUREDIVISION.MOVE3TO WS-A.EVALUATETRUEWHEN WS-A >2DISPLAY'WS-A GREATER THAN 2'WHEN WS-A <0DISPLAY'WS-A LESS THAN 0'WHENOTHERDISPLAY'INVALID VALUE OF WS-A'END-EVALUATE.STOPRUN.

    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
    

  • Basic Verbs

    COBOL verbs are used in the procedure division for data processing. A statement always start with a COBOL verb. There are several COBOL verbs with different types of actions.

    Input / Output Verbs

    Input/Output verbs are used to get data from the user and display the output of COBOL programs. The following two verbs are used for this process −

    Accept Verb

    Accept verb is used to get data such as date, time, and day from the operating system or directly from the user. If a program is accepting data from the user, then it needs to be passed through JCL. While getting data from the operating system, FROM option is included as shown in the following example −

    ACCEPT WS-STUDENT-NAME.
    ACCEPT WS-DATE FROM SYSTEM-DATE.
    

    Display Verb

    Display verb is used to display the output of a COBOL program.

    DISPLAY WS-STUDENT-NAME.
    DISPLAY "System date is : " WS-DATE.
    

    COBOL PROGRAM

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-STUDENT-NAME PICX(25).01 WS-DATE PICX(10).PROCEDUREDIVISION.ACCEPT WS-STUDENT-NAME.ACCEPT WS-DATE FROMDATE.DISPLAY"Name :  " WS-STUDENT-NAME.DISPLAY"Date : " WS-DATE.STOPRUN.

    JCL to execute the above COBOL program −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO
    //INPUT DD DSN=PROGRAM.DIRECTORY,DISP=SHR
    //SYSIN DD *
    TutorialsPoint
    /*

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

    Name : TutorialsPoint
    Date : 200623
    

    Initialize Verb

    Initialize verb is used to initialize a group item or an elementary item. Data names with RENAME clause cannot be initialized. Numeric data items are replaced by ZEROES. Alphanumeric or alphabetic data items are replaced by SPACES. If we include REPLACING term, then data items can be initialized to the given replacing value as shown in the following example −

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NAME PICA(30)VALUE'ABCDEF'.01 WS-ID PIC9(5).01 WS-ADDRESS.05 WS-HOUSE-NUMBER PIC9(3).05 WS-COUNTRY PICX(15).05 WS-PINCODE PIC9(6)VALUE123456.PROCEDUREDIVISION.
       A000-FIRST-PARA.INITIALIZE WS-NAME, WS-ADDRESS.INITIALIZE WS-ID REPLACINGNUMERICDATABY12345.DISPLAY"My name is   : "WS-NAME.DISPLAY"My ID is     : "WS-ID.DISPLAY"Address      : "WS-ADDRESS.DISPLAY"House Number : "WS-HOUSE-NUMBER.DISPLAY"Country      : "WS-COUNTRY.DISPLAY"Pincode      : "WS-PINCODE.STOPRUN.

    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 −

    My name is   :                               
    My ID is     : 12345
    Address      : 000               000000
    House Number : 000
    Country      :                
    Pincode      : 000000
    

    Move Verb

    Move verb is used to copy data from source data to destination data. It can be used on both elementary and group data items. For group data items, MOVE CORRESPONDING/CORR is used. In try it option, MOVE CORR is not working; but on a mainframe server, it will work.

    For moving data from a string, MOVE(x:l) is used where x is the starting position and l is the length. Data will be truncated if the destination data item PIC clause is less than the source data item PIC clause. If the destination data item PIC clause is more than the source data item PIC clause, then ZEROS or SPACES will be added in the extra bytes. The following example makes it clear.

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9).01 WS-NUM2 PIC9(9).01 WS-NUM3 PIC9(5).01 WS-NUM4 PIC9(6).01 WS-ADDRESS.05 WS-HOUSE-NUMBER PIC9(3).05 WS-COUNTRY PICX(5).05 WS-PINCODE PIC9(6).01 WS-ADDRESS1.05 WS-HOUSE-NUMBER1 PIC9(3).05 WS-COUNTRY1 PICX(5).05 WS-PINCODE1 PIC9(6).PROCEDUREDIVISION.
       A000-FIRST-PARA.MOVE123456789TO WS-NUM1.MOVE WS-NUM1 TO WS-NUM2 WS-NUM3.MOVE WS-NUM1(3:6)TO WS-NUM4.MOVE123TO WS-HOUSE-NUMBER.MOVE'INDIA'TO WS-COUNTRY.MOVE112233TO WS-PINCODE.MOVE WS-ADDRESS TO WS-ADDRESS1.DISPLAY"WS-NUM1     : " WS-NUM1
       DISPLAY"WS-NUM2     : " WS-NUM2
       DISPLAY"WS-NUM3     : " WS-NUM3
       DISPLAY"WS-NUM4     : " WS-NUM4
       DISPLAY"WS-ADDRESS  : " WS-ADDRESS
       DISPLAY"WS-ADDRESS1 : " WS-ADDRESS1
    
    STOPRUN.

    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     : 123456789
    WS-NUM2     : 123456789
    WS-NUM3     : 56789
    WS-NUM4     : 345678
    WS-ADDRESS  : 123INDIA112233
    WS-ADDRESS1 : 123INDIA112233
    

    Legal Moves

    The following table gives information about the legal moves −

    AlphabeticAlphanumericNumeric
    AlphabeticPossiblePossibleNot Possible
    AlphanumericPossiblePossiblePossible
    NumericNot PossiblePossiblePossible

    Add Verb

    Add verb is used to add two or more numbers and store the result in the destination operand.

    Syntax

    Given below is the syntax to Add two or more numbers −

    ADD A B TO C D
    
    ADD A B C TO D GIVING E
    
    ADD CORR WS-GROUP1 TO WS-GROUP2
    

    In syntax-1, A, B, C are added and the result is stored in C (C=A+B+C). A, B, D are added and the result is stored in D (D = A + B + D).

    In syntax-2, A, B, C, D are added and the result is stored in E (E=A+B+C+D).

    In syntax-3, sub-group items within WS-GROUP1 and WS-GROUP2 are added and the result is stored in WS-GROUP2.

    Example

    Open Compiler

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9)VALUE10.01 WS-NUM2 PIC9(9)VALUE10.01 WS-NUM3 PIC9(9)VALUE10.01 WS-NUM4 PIC9(9)VALUE10.01 WS-NUMA PIC9(9)VALUE10.01 WS-NUMB PIC9(9)VALUE10.01 WS-NUMC PIC9(9)VALUE10.01 WS-NUMD PIC9(9)VALUE10.01 WS-NUME PIC9(9)VALUE10.PROCEDUREDIVISION.ADD WS-NUM1 WS-NUM2 TO WS-NUM3 WS-NUM4.ADD WS-NUMA WS-NUMB WS-NUMC TO WS-NUMD GIVING WS-NUME.DISPLAY"WS-NUM1     : " WS-NUM1
       DISPLAY"WS-NUM2     : " WS-NUM2
       DISPLAY"WS-NUM3     : " WS-NUM3
       DISPLAY"WS-NUM4     : " WS-NUM4
       DISPLAY"WS-NUMA     : " WS-NUMA
       DISPLAY"WS-NUMB     : " WS-NUMB
       DISPLAY"WS-NUMC     : " WS-NUMC
       DISPLAY"WS-NUMD     : " WS-NUMD
       DISPLAY"WS-NUME     : " WS-NUME
    
    STOPRUN.

    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     : 000000010
    WS-NUM2     : 000000010
    WS-NUM3     : 000000030
    WS-NUM4     : 000000030
    WS-NUMA     : 000000010
    WS-NUMB     : 000000010
    WS-NUMC     : 000000010
    WS-NUMD     : 000000010
    WS-NUME     : 000000040
    

    Subtract Verb

    Subtract verb is used for subtraction operations.

    Syntax

    Given below is the syntax for Subtract operations −

    SUBTRACT A B FROM C D
    
    SUBTRACT A B C FROM D GIVING E
    
    SUBTRACT CORR WS-GROUP1 TO WS-GROUP2
    

    In syntax-1, A and B are added and subtracted from C. The result is stored in C (C = C-(A+B)). A and B are added and subtracted from D. The result is stored in D (D = D-(A+B)).

    In syntax-2, A, B, C are added and subtracted from D. The result is stored in E (E = D-(A+B+C))

    In syntax-3, sub-group items within WS-GROUP1 and WS-GROUP2 are subtracted and the result is stored in WS-GROUP2.

    Example

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9)VALUE10.01 WS-NUM2 PIC9(9)VALUE10.01 WS-NUM3 PIC9(9)VALUE100.01 WS-NUM4 PIC9(9)VALUE100.01 WS-NUMA PIC9(9)VALUE10.01 WS-NUMB PIC9(9)VALUE10.01 WS-NUMC PIC9(9)VALUE10.01 WS-NUMD PIC9(9)VALUE100.01 WS-NUME PIC9(9)VALUE10.PROCEDUREDIVISION.SUBTRACT WS-NUM1 WS-NUM2 FROM WS-NUM3 WS-NUM4.SUBTRACT WS-NUMA WS-NUMB WS-NUMC FROM WS-NUMD GIVING WS-NUME.DISPLAY"WS-NUM1     : " WS-NUM1
       DISPLAY"WS-NUM2     : " WS-NUM2
       DISPLAY"WS-NUM3     : " WS-NUM3
       DISPLAY"WS-NUM4     : " WS-NUM4
       DISPLAY"WS-NUMA     : " WS-NUMA
       DISPLAY"WS-NUMB     : " WS-NUMB
       DISPLAY"WS-NUMC     : " WS-NUMC
       DISPLAY"WS-NUMD     : " WS-NUMD
       DISPLAY"WS-NUME     : " WS-NUME
    
    STOPRUN.

    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     : 000000010
    WS-NUM2     : 000000010
    WS-NUM3     : 000000080
    WS-NUM4     : 000000080
    WS-NUMA     : 000000010
    WS-NUMB     : 000000010
    WS-NUMC     : 000000010
    WS-NUMD     : 000000100
    WS-NUME     : 000000070
    

    Multiply Verb

    Multiply verb is used for multiplication operations.

    Syntax

    Given below is the syntax to multiply two or more numbers −

    MULTIPLY A BY B C
    
    MULTIPLY A BY B GIVING E
    

    In syntax-1, A and B are multipled and the result is stored in B (B=A*B). A and C are multipled and the result is stored in C (C = A * C).

    In syntax-2, A and B are multipled and the result is stored in E (E=A*B).

    Example

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9)VALUE10.01 WS-NUM2 PIC9(9)VALUE10.01 WS-NUM3 PIC9(9)VALUE10.01 WS-NUMA PIC9(9)VALUE10.01 WS-NUMB PIC9(9)VALUE10.01 WS-NUMC PIC9(9)VALUE10.PROCEDUREDIVISION.MULTIPLY WS-NUM1 BY WS-NUM2 WS-NUM3.MULTIPLY WS-NUMA BY WS-NUMB GIVING WS-NUMC.DISPLAY"WS-NUM1     : " WS-NUM1
       DISPLAY"WS-NUM2     : " WS-NUM2
       DISPLAY"WS-NUM3     : " WS-NUM3
       DISPLAY"WS-NUMA     : " WS-NUMA
       DISPLAY"WS-NUMB     : " WS-NUMB
       DISPLAY"WS-NUMC     : " WS-NUMC
       
    STOPRUN.

    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     : 000000010
    WS-NUM2     : 000000100
    WS-NUM3     : 000000100
    WS-NUMA     : 000000010
    WS-NUMB     : 000000010
    WS-NUMC     : 000000100
    

    Divide Verb

    Divide verb is used for division operations.

    Syntax

    Given below is the syntax for division operations −

    DIVIDE A INTO B
    
    DIVIDE A BY B GIVING C REMAINDER R
    

    In syntax-1, B is divided by A and the result is stored in B (B=B/A).

    In syntax-2, A is divided by B and the result is stored in C (C=A/B) and the remainder is stored in R.

    Example

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9)VALUE5.01 WS-NUM2 PIC9(9)VALUE250.01 WS-NUMA PIC9(9)VALUE100.01 WS-NUMB PIC9(9)VALUE15.01 WS-NUMC PIC9(9).01 WS-REM PIC9(9).PROCEDUREDIVISION.DIVIDE WS-NUM1 INTO WS-NUM2.DIVIDE WS-NUMA BY WS-NUMB GIVING WS-NUMC REMAINDER WS-REM.DISPLAY"WS-NUM1     : " WS-NUM1
       DISPLAY"WS-NUM2     : " WS-NUM2
       DISPLAY"WS-NUMA     : " WS-NUMA
       DISPLAY"WS-NUMB     : " WS-NUMB
       DISPLAY"WS-NUMC     : " WS-NUMC
       DISPLAY"WS-REM      : " WS-REM
       
    STOPRUN.

    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     : 000000005
    WS-NUM2     : 000000050
    WS-NUMA     : 000000100
    WS-NUMB     : 000000015
    WS-NUMC     : 000000006
    WS-REM      : 000000010
    

    Compute Statement

    Compute statement is used to write arithmetic expressions in COBOL. This is a replacement for Add, Subtract, Multiply, and Divide.

    Example

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9)VALUE10.01 WS-NUM2 PIC9(9)VALUE10.01 WS-NUM3 PIC9(9)VALUE10.01 WS-NUMA PIC9(9)VALUE50.01 WS-NUMB PIC9(9)VALUE10.01 WS-NUMC PIC9(9).PROCEDUREDIVISION.COMPUTE WS-NUMC=(WS-NUM1 * WS-NUM2)-(WS-NUMA / WS-NUMB)+ WS-NUM3.DISPLAY"WS-NUM1     : " WS-NUM1
       DISPLAY"WS-NUM2     : " WS-NUM2
       DISPLAY"WS-NUM3     : " WS-NUM3
       DISPLAY"WS-NUMA     : " WS-NUMA
       DISPLAY"WS-NUMB     : " WS-NUMB
       DISPLAY"WS-NUMC     : " WS-NUMC
    
    STOPRUN.

    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 : 000000010 WS-NUM2 : 000000010 WS-NUM3 : 000000010 WS-NUMA : 000000050 WS-NUMB : 000000010 WS-NUMC : 000000105

  • Data Types

    Data Division is used to define the variables used in a program. To describe data in COBOL, one must understand the following terms −

    • Data Name
    • Level Number
    • Picture Clause
    • Value Clause
    01            TOTAL-STUDENTS            PIC9(5)            VALUE '125'.
    |                    |                    |                    |
    |                    |                    |                    |
    |                    |                    |                    | 
    Level Number     Data Name           Picture Clause       Value Clause
    

    Data Name

    Data names must be defined in the Data Division before using them in the Procedure Division. They must have a user-defined name; reserved words cannot be used. Data names give reference to the memory locations where actual data is stored. They can be elementary or group type.

    Example

    The following example shows valid and invalid data names −

    Valid:
       WS-NAME
       TOTAL-STUDENTS
       A100
       100B
    
    Invalid:MOVE(Reserved Words)COMPUTE(Reserved Words)100(NoAlphabet)100+B           (+isnot allowed)

    Level Number

    Level number is used to specify the level of data in a record. They are used to differentiate between elementary items and group items. Elementary items can be grouped together to create group items.

    Sr.No.Level Number & Description
    101Record description entry
    202 to 49Group and Elementary items
    366Rename Clause items
    477Items which cannot be sub-divided
    588Condition name entry
    • Elementary items cannot be divided further. Level number, Data name, Picture clause, and Value clause (optional) are used to describe an elementary item.
    • Group items consist of one or more elementary items. Level number, Data name, and Value clause (optional) are used to describe a group item. Group level number is always 01.

    Example

    The following example shows Group and Elementary items −

    DATADIVISION.WORKING-STORAGESECTION.01 WS-NAME    PICX(25).                               ---> ELEMENTARY ITEM 
    01 WS-CLASS   PIC9(2)VALUE'10'.                   ---> ELEMENTARY ITEM
    
    01 WS-ADDRESS.                                         --->GROUP ITEM   
       05 WS-HOUSE-NUMBER    PIC9(3).                     ---> ELEMENTARY ITEM
       05 WS-STREET          PICX(15).                    ---> ELEMENTARY ITEM
       05 WS-CITY            PICX(15).                    ---> ELEMENTARY ITEM
       05 WS-COUNTRY         PICX(15)VALUE'INDIA'.     ---> ELEMENTARY ITEM
    

    Picture Clause

    Picture clause is used to define the following items −

    • Data type can be numeric, alphabetic, or alphanumeric. Numeric type consists of only digits 0 to 9. Alphabetic type consists of letters A to Z and spaces. Alphanumeric type consists of digits, letters, and special characters.
    • Sign can be used with numeric data. It can be either + or .
    • Decimal point position can be used with numeric data. Assumed position is the position of decimal point and not included in the data.
    • Length defines the number of bytes used by the data item.

    Symbols used in a Picture clause −

    Sr.No.Symbol & Description
    19Numeric
    2AAlphabetic
    3XAlphanumeric
    4VImplicit Decimal
    5SSign
    6PAssumed Decimal

    Example

    The following example shows the use of PIC clause −

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PICS9(3)V9(2).01 WS-NUM2 PICPPP999.01 WS-NUM3 PICS9(3)V9(2)VALUE-123.45.01 WS-NAME PICA(6)VALUE'ABCDEF'.01 WS-ID PICX(5)VALUE'A121$'.PROCEDUREDIVISION.DISPLAY"WS-NUM1 : "WS-NUM1.DISPLAY"WS-NUM2 : "WS-NUM2.DISPLAY"WS-NUM3 : "WS-NUM3.DISPLAY"WS-NAME : "WS-NAME.DISPLAY"WS-ID : "WS-ID.STOPRUN.

    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 : +000.00
    WS-NUM2 : .000000
    WS-NUM3 : -123.45
    WS-NAME : ABCDEF
    WS-ID : A121$
    

    Value Clause

    Value clause is an optional clause which is used to initialize the data items. The values can be numeric literal, alphanumeric literal, or figurative constant. It can be used with both group and elementary items.

    Example

    The following example shows the use of VALUE clause −

    Open Compiler

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC99V9VALUEIS3.5.01 WS-NAME PICA(6)VALUE'ABCD'.01 WS-ID PIC99VALUEZERO.PROCEDUREDIVISION.DISPLAY"WS-NUM1 : "WS-NUM1.DISPLAY"WS-NAME : "WS-NAME.DISPLAY"WS-ID   : "WS-ID.STOPRUN.

    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 : 03.5
    WS-NAME : ABCD
    WS-ID   : 00

  • Basic Syntax

    Character Set

    ‘Characters’ are lowest in the hierarchy and they cannot be divided further. The COBOL Character Set includes 78 characters which are shown below −

    Sr.No.Character & Description
    1A-ZAlphabets(Upper Case)
    2a-zAlphabets (Lower Case)
    30-9Numeric
    4 Space
    5+Plus Sign
    6Minus Sign or Hyphen
    7*Asterisk
    8/Forward Slash
    9$Currency Sign
    10,Comma
    11;Semicolon
    12.Decimal Point or Period
    13Quotation Marks
    14(Left Parenthesis
    15)Right Parenthesis
    16>Greater than
    17<Less than
    18:Colon
    19Apostrophe
    20=Equal Sign

    Coding Sheet

    The source program of COBOL must be written in a format acceptable to the compilers. COBOL programs are written on COBOL coding sheets. There are 80 character positions on each line of a coding sheet.

    Character positions are grouped into the following five fields −

    PositionsFieldDescription
    1-6Column NumbersReserved for line numbers.
    7IndicatorIt can have Asterisk (*) indicating comments, Hyphen (-) indicating continuation and Slash ( / ) indicating form feed.
    8-11Area AAll COBOL divisions, sections, paragraphs and some special entries must begin in Area A.
    12-72Area BAll COBOL statements must begin in area B.
    73-80Identification AreaIt can be used as needed by the programmer.

    Example

    The following example shows a COBOL coding sheet −

    000100IDENTIFICATIONDIVISION.000100000200PROGRAM-ID. HELLO.000101000250* THIS IS A COMMENT LINE000102000300PROCEDUREDIVISION.000103000350 A000-FIRST-PARA.000104000400DISPLAY Coding Sheet.000105000500STOPRUN.000106

    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 −

    Coding Sheet
    

    Character Strings

    Character strings are formed by combining individual characters. A character string can be a

    • Comment,
    • Literal, or
    • COBOL word.

    All character strings must be ended with separators. A separator is used to separate character strings.

    Frequently used separators − Space, Comma, Period, Apostrophe, Left/Right Parenthesis, and Quotation mark.

    Comment

    A comment is a character string that does not affect the execution of a program. It can be any combination of characters.

    There are two types of comments −

    Comment Line

    A comment line can be written in any column. The compiler does not check a comment line for syntax and treats it for documentation.

    Comment Entry

    Comment entries are those that are included in the optional paragraphs of an Identification Division. They are written in Area B and programmers use it for reference.

    The text highlighted in Bold are the commented entries in the following example −

    000100IDENTIFICATIONDIVISION.000100000150PROGRAM-ID. HELLO.000101000200AUTHOR. TUTORIALSPOINT.000102000250* THIS IS A COMMENT LINE000103000300PROCEDUREDIVISION.000104000350 A000-FIRST-PARA.000105000360/First Para Begins - Documentation Purpose                       000106000400DISPLAY Comment line.000107000500STOPRUN.000108

    JCL to execute 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 −

    Comment Line
    

    Literal

    Literal is a constant that is directly hard-coded in a program. In the following example, “Hello World” is a literal.

    PROCEDUREDIVISION.DISPLAY'Hello World'.

    There are two types of literals as discussed below −

    Alphanumeric Literal

    Alphanumeric Literals are enclosed in quotes or apostrophe. Length can be up to 160 characters. An apostrophe or a quote can be a part of a literal only if it is paired. Starting and ending of the literal should be same, either apostrophe or quote.

    Example

    The following example shows valid and invalid Alphanumeric Literals −

    Valid:
       This is valid
       "This is valid"
       This isnt invalidInvalid:
       This isinvalid
       This isnt valid
    

    Numeric Literal

    A Numeric Literal is a combination of digits from 0 to 9, +, , or decimal point. Length can be up to 18 characters. Sign cannot be the rightmost character. Decimal point should not appear at the end.

    Example

    The following example shows valid and invalid Numeric Literals −

    Valid:100+10.9-1.9Invalid:1,0010.10.9-
    

    COBOL Word

    COBOL Word is a character string that can be a reserved word or a user-defined word. Length can be up to 30 characters.

    User-Defined

    User-defined words are used for naming files, data, records, paragraph names, and sections. Alphabets, digits, and hyphens are allowed while forming userdefined words. You cannot use COBOL reserved words.

    Reserved Words

    Reserved words are predefined words in COBOL. Different types of reserved words that we use frequently are as follows −

    • Keywords like ADD, ACCEPT, MOVE, etc.
    • Special characters words like +, -, *, <, <=, etc
    • Figurative constants are constant values like ZERO, SPACES, etc. All the constant values of figurative constants are mentioned in the following table.

    Figurative Constants

    Sr.No.Figurative Constants & Description
    1HIGH-VALUESOne or more characters which will be at the highest position in descending order.
    2LOW-VALUESOne or more characters have zeros in binary representation.
    3ZERO/ZEROESOne or more zero depending on the size of the variable.
    4SPACESOne or more spaces.
    5QUOTESSingle or double quotes.
    6ALL literalFills the data-item with Literal.

    Print Page

    Character Set

    ‘Characters’ are lowest in the hierarchy and they cannot be divided further. The COBOL Character Set includes 78 characters which are shown below −

    Sr.No.Character & Description
    1A-ZAlphabets(Upper Case)
    2a-zAlphabets (Lower Case)
    30-9Numeric
    4 Space
    5+Plus Sign
    6Minus Sign or Hyphen
    7*Asterisk
    8/Forward Slash
    9$Currency Sign
    10,Comma
    11;Semicolon
    12.Decimal Point or Period
    13Quotation Marks
    14(Left Parenthesis
    15)Right Parenthesis
    16>Greater than
    17<Less than
    18:Colon
    19Apostrophe
    20=Equal Sign

    Coding Sheet

    The source program of COBOL must be written in a format acceptable to the compilers. COBOL programs are written on COBOL coding sheets. There are 80 character positions on each line of a coding sheet.

    Character positions are grouped into the following five fields −

    PositionsFieldDescription
    1-6Column NumbersReserved for line numbers.
    7IndicatorIt can have Asterisk (*) indicating comments, Hyphen (-) indicating continuation and Slash ( / ) indicating form feed.
    8-11Area AAll COBOL divisions, sections, paragraphs and some special entries must begin in Area A.
    12-72Area BAll COBOL statements must begin in area B.
    73-80Identification AreaIt can be used as needed by the programmer.

    Example

    The following example shows a COBOL coding sheet −

    000100IDENTIFICATIONDIVISION.000100000200PROGRAM-ID. HELLO.000101000250* THIS IS A COMMENT LINE000102000300PROCEDUREDIVISION.000103000350 A000-FIRST-PARA.000104000400DISPLAY Coding Sheet.000105000500STOPRUN.000106

    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 −

    Coding Sheet
    

    Character Strings

    Character strings are formed by combining individual characters. A character string can be a

    • Comment,
    • Literal, or
    • COBOL word.

    All character strings must be ended with separators. A separator is used to separate character strings.

    Frequently used separators − Space, Comma, Period, Apostrophe, Left/Right Parenthesis, and Quotation mark.

    Comment

    A comment is a character string that does not affect the execution of a program. It can be any combination of characters.

    There are two types of comments −

    Comment Line

    A comment line can be written in any column. The compiler does not check a comment line for syntax and treats it for documentation.

    Comment Entry

    Comment entries are those that are included in the optional paragraphs of an Identification Division. They are written in Area B and programmers use it for reference.

    The text highlighted in Bold are the commented entries in the following example −

    000100IDENTIFICATIONDIVISION.000100000150PROGRAM-ID. HELLO.000101000200AUTHOR. TUTORIALSPOINT.000102000250* THIS IS A COMMENT LINE000103000300PROCEDUREDIVISION.000104000350 A000-FIRST-PARA.000105000360/First Para Begins - Documentation Purpose                       000106000400DISPLAY Comment line.000107000500STOPRUN.000108

    JCL to execute 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 −

    Comment Line
    

    Literal

    Literal is a constant that is directly hard-coded in a program. In the following example, “Hello World” is a literal.

    PROCEDUREDIVISION.DISPLAY'Hello World'.

    There are two types of literals as discussed below −

    Alphanumeric Literal

    Alphanumeric Literals are enclosed in quotes or apostrophe. Length can be up to 160 characters. An apostrophe or a quote can be a part of a literal only if it is paired. Starting and ending of the literal should be same, either apostrophe or quote.

    Example

    The following example shows valid and invalid Alphanumeric Literals −

    Valid:
       This is valid
       "This is valid"
       This isnt invalidInvalid:
       This isinvalid
       This isnt valid
    

    Numeric Literal

    A Numeric Literal is a combination of digits from 0 to 9, +, , or decimal point. Length can be up to 18 characters. Sign cannot be the rightmost character. Decimal point should not appear at the end.

    Example

    The following example shows valid and invalid Numeric Literals −

    Valid:100+10.9-1.9Invalid:1,0010.10.9-
    

    COBOL Word

    COBOL Word is a character string that can be a reserved word or a user-defined word. Length can be up to 30 characters.

    User-Defined

    User-defined words are used for naming files, data, records, paragraph names, and sections. Alphabets, digits, and hyphens are allowed while forming userdefined words. You cannot use COBOL reserved words.

    Reserved Words

    Reserved words are predefined words in COBOL. Different types of reserved words that we use frequently are as follows −

    • Keywords like ADD, ACCEPT, MOVE, etc.
    • Special characters words like +, -, *, <, <=, etc
    • Figurative constants are constant values like ZERO, SPACES, etc. All the constant values of figurative constants are mentioned in the following table.

    Figurative Constants

    Sr.No.Figurative Constants & Description
    1HIGH-VALUESOne or more characters which will be at the highest position in descending order.
    2LOW-VALUESOne or more characters have zeros in binary representation.
    3ZERO/ZEROESOne or more zero depending on the size of the variable.
    4SPACESOne or more spaces.
    5QUOTESSingle or double quotes.
    6ALL literalFills the data-item with Literal.

  • Program Structure

    A COBOL program structure consists of divisions as shown in the following image −

    Program Structure

    A brief introduction of these divisions is given below −

    • Sections are the logical subdivision of program logic. A section is a collection of paragraphs.
    • Paragraphs are the subdivision of a section or division. It is either a user-defined or a predefined name followed by a period, and consists of zero or more sentences/entries.
    • Sentences are the combination of one or more statements. Sentences appear only in the Procedure division. A sentence must end with a period.
    • Statements are meaningful COBOL statements that perform some processing.
    • Characters are the lowest in the hierarchy and cannot be divisible.

    You can co-relate the above-mentioned terms with the COBOL program in the following example −

    PROCEDUREDIVISION.
    A0000-FIRST-PARA SECTION.
    FIRST-PARAGRAPH.ACCEPT WS-ID            - Statement-1  -----|
    MOVE'10'TO WS-ID      - Statement-2       |-- Sentence-1DISPLAY WS-ID           - Statement-3  -----|
    .

    Divisions

    A COBOL program consists of four divisions.

    Identification Division

    It is the first and only mandatory division of every COBOL program. The programmer and the compiler use this division to identify the program. In this division, PROGRAM-ID is the only mandatory paragraph. PROGRAM-ID specifies the program name that can consist 1 to 30 characters.

    Try the following example using the Live Demo option online.

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.PROCEDUREDIVISION.DISPLAY'Welcome to Tutorialspoint'.STOPRUN.

    Given below is the 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 −

    Welcome to Tutorialspoint
    

    Environment Division

    Environment division is used to specify input and output files to the program. It consists of two sections −

    • Configuration section provides information about the system on which the program is written and executed. It consists of two paragraphs −
      • Source computer − System used to compile the program.
      • Object computer − System used to execute the program.
    • Input-Output section provides information about the files to be used in the program. It consists of two paragraphs −
      • File control − Provides information of external data sets used in the program.
      • I-O control − Provides information of files used in the program.
    ENVIRONMENTDIVISION.CONFIGURATIONSECTION.SOURCE-COMPUTER. XXX-ZOS.OBJECT-COMPUTER. XXX-ZOS.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT FILEN ASSIGNTO DDNAME
       ORGANIZATIONISSEQUENTIAL.

    Data Division

    Data division is used to define the variables used in the program. It consists of four sections −

    • File section is used to define the record structure of the file.
    • Working-Storage section is used to declare temporary variables and file structures which are used in the program.
    • Local-Storage section is similar to Working-Storage section. The only difference is that the variables will be allocated and initialized every time a program starts execution.
    • Linkage section is used to describe the data names that are received from an external program.

    COBOL Program

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.ENVIRONMENTDIVISION.INPUT-OUTPUTSECTION.FILE-CONTROL.SELECT FILEN ASSIGNTOINPUT.ORGANIZATIONISSEQUENTIAL.ACCESSISSEQUENTIAL.DATADIVISION.FILESECTION.FD FILEN
       01 NAME PICA(25).WORKING-STORAGESECTION.01 WS-STUDENT PICA(30).01 WS-ID PIC9(5).LOCAL-STORAGESECTION.01 LS-CLASS PIC9(3).LINKAGESECTION.01 LS-ID PIC9(5).PROCEDUREDIVISION.DISPLAY'Executing COBOL program using JCL'.STOPRUN.

    The JCL to execute the above COBOL program is as follows −

    //SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
    //STEP1 EXEC PGM = HELLO
    //INPUT DD DSN = ABC.EFG.XYZ,DISP = SHR
    

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

    Executing COBOL program using JCL
    

    Procedure Division

    Procedure division is used to include the logic of the program. It consists of executable statements using variables defined in the data division. In this division, paragraph and section names are user-defined.

    There must be at least one statement in the procedure division. The last statement to end the execution in this division is either STOP RUN which is used in the calling programs or EXIT PROGRAM which is used in the called programs.

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NAME PICA(30).01 WS-ID PIC9(5)VALUE12345.PROCEDUREDIVISION.
       A000-FIRST-PARA.DISPLAY'Hello World'.MOVE'TutorialsPoint'TO WS-NAME.DISPLAY"My name is : "WS-NAME.DISPLAY"My ID is : "WS-ID.STOPRUN.

    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 −

    Hello World
    My name is : TutorialsPoint
    My ID is : 12345

  • Environment Setup

    We have set up the COBOL Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it online.

    IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.PROCEDUREDIVISION.DISPLAY'Hello World'.STOPRUN.

    For most of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning.

    Installing COBOL on Windows/Linux

    There are many Free Mainframe Emulators available for Windows which can be used to write and learn simple COBOL programs.

    One such emulator is Hercules, which can be easily installed on Windows by following a few simple steps as given below −

    • Download and install the Hercules emulator, which is available from the Hercules’ home site: www.hercules-390.eu
    • Once you have installed the package on Windows machine, it will create a folder like C:/hercules/mvs/cobol.
    • Run the Command Prompt (CMD) and reach the directory C:/hercules/mvs/cobol on CMD.
    • The complete guide on various commands to write and execute a JCL and COBOL programs can be found at:www.jaymoseley.com/hercules/installmvs/instmvs2.htm

    Hercules is an open-source software implementation of the mainframe System/370 and ESA/390 architectures, in addition to the latest 64-bit z/Architecture. Hercules runs under Linux, Windows, Solaris, FreeBSD, and Mac OS X.

    A user can connect to a mainframe server in a number of ways such as thin client, dummy terminal, Virtual Client System (VCS), or Virtual Desktop System (VDS). Every valid user is given a login id to enter into the Z/OS interface (TSO/E or ISPF).

    Compiling COBOL Programs

    In order to execute a COBOL program in batch mode using JCL, the program needs to be compiled, and a load module is created with all the sub-programs. The JCL uses the load module and not the actual program at the time of execution. The load libraries are concatenated and given to the JCL at the time of execution using JCLLIB or STEPLIB.

    There are many mainframe compiler utilities available to compile a COBOL program. Some corporate companies use Change Management tools like Endevor, which compiles and stores every version of the program. This is useful in tracking the changes made to the program.

    //COMPILE   JOB ,CLASS=6,MSGCLASS=X,NOTIFY=&SYSUID             
    //*//STEP1     EXEC IGYCRCTL,PARM=RMODE,DYNAM,SSRANGE
    //SYSIN     DD DSN=MYDATA.URMI.SOURCES(MYCOBB),DISP=SHR
    //SYSLIB    DD DSN=MYDATA.URMI.COPYBOOK(MYCOPY),DISP=SHR
    //SYSLMOD   DD DSN=MYDATA.URMI.LOAD(MYCOBB),DISP=SHR
    //SYSPRINT  DD SYSOUT=*//*

    IGYCRCTL is an IBM COBOL compiler utility. The compiler options are passed using the PARM parameter. In the above example, RMODE instructs the compiler to use relative addressing mode in the program. The COBOL program is passed using the SYSIN parameter. Copybook is the library used by the program in SYSLIB.

    Executing COBOL Programs

    Given below is a JCL example where the program MYPROG is executed using the input file MYDATA.URMI.INPUT and produces two output files written to the spool.

    //COBBSTEP  JOB CLASS=6,NOTIFY=&SYSUID
    ////STEP10    EXEC PGM=MYPROG,PARM=ACCT5000
    //STEPLIB   DD DSN=MYDATA.URMI.LOADLIB,DISP=SHR
    //INPUT1    DD DSN=MYDATA.URMI.INPUT,DISP=SHR
    //OUT1      DD SYSOUT=*//OUT2      DD SYSOUT=*//SYSIN     DD *//CUST1     1000//CUST2     1001/*

    The load module of MYPROG is located in MYDATA.URMI.LOADLIB. This is important to note that the above JCL can be used for a non-DB2 COBOL module only.

    Executing COBOL-DB2 programs

    For running a COBOL-DB2 program, a specialized IBM utility is used in the JCL and the program; DB2 region and required parameters are passed as input to the utility.

    The steps followed in running a COBOL-DB2 program are as follows −

    • When a COBOL-DB2 program is compiled, a DBRM (Database Request Module) is created along with the load module. The DBRM contains the SQL statements of the COBOL programs with its syntax checked to be correct.
    • The DBRM is bound to the DB2 region (environment) in which the COBOL will run. This can be done using the IKJEFT01 utility in a JCL.
    • After the bind step, the COBOL-DB2 program is run using IKJEFT01 (again) with the load library and the DBRM library as the input to the JCL.
    //STEP001  EXEC PGM=IKJEFT01
    //*//STEPLIB  DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
    //*//input files
    //output files
    //SYSPRINT DD SYSOUT=*//SYSABOUT DD SYSOUT=*//SYSDBOUT DD SYSOUT=*//SYSUDUMP DD SYSOUT=*//DISPLAY  DD SYSOUT=*//SYSOUT   DD SYSOUT=*//SYSTSPRT DD SYSOUT=*//SYSTSIN  DD *
       DSN SYSTEM(SSID)RUNPROGRAM(MYCOBB) PLAN(PLANNAME) PARM(parameters tocobolprogram)-
       LIB('MYDATA.URMI.LOADLIB')END/*

    In the above example, MYCOBB is the COBOL-DB2 program run using IKJEFT01. Please note that the program name, DB2 Sub-System Id (SSID), and DB2 Plan name are passed within the SYSTSIN DD statement. The DBRM library is specified in the STEPLIB.

  • Overview

    Introduction to COBOL

    COBOL is a high-level language. One must understand the way COBOL works. Computers only understand machine code, a binary stream of 0s and 1s. COBOL code must be converted into machine code using a compiler. Run the program source through a compiler. The compiler first checks for any syntax errors and then converts it into machine language. The compiler creates an output file which is known as load module. This output file contains executable code in the form of 0s and 1s.

    Evolution of COBOL

    During 1950s, when the businesses were growing in the western part of the world, there was a need to automate various processes for ease of operation and this gave birth to a high-level programming language meant for business data processing.

    • In 1959, COBOL was developed by CODASYL (Conference on Data Systems Language).
    • The next version, COBOL-61, was released in 1961 with some revisions.
    • In 1968, COBOL was approved by ANSI as a standard language for commercial use (COBOL-68).
    • It was again revised in 1974 and 1985 to develop subsequent versions named COBOL-74 and COBOL-85 respectively.
    • In 2002, Object-Oriented COBOL was released, which could use encapsulated objects as a normal part of COBOL programming.

    Importance of COBOL

    • COBOL was the first widely used high-level programming language. It is an English-like language which is user friendly. All the instructions can be coded in simple English words.
    • COBOL is also used as a self-documenting language.
    • COBOL can handle huge data processing.
    • COBOL is compatible with its previous versions.
    • COBOL has effective error messages and so, resolution of bugs is easier.

    Features of COBOL

    Standard Language

    COBOL is a standard language that can be compiled and executed on machines such as IBM AS/400, personal computers, etc.

    Business Oriented

    COBOL was designed for business-oriented applications related to financial domain, defense domain, etc. It can handle huge volumes of data because of its advanced file handling capabilities.

    Robust Language

    COBOL is a robust language as its numerous debugging and testing tools are available for almost all computer platforms.

    Structured Language

    Logical control structures are available in COBOL which makes it easier to read and modify. COBOL has different divisions, so it is easy to debug.