COBOL Example Sample Reference Code


1. Move Statement

Picture of AValue of APicture of BValue of B after Move
PIC 99V9912.35PIC 999V99012.35
PIC 99V9912.35PIC 9999V99990012.3500
PIC 99V99912.345PIC 9V992.34
PIC9(05)V9(03)54321.543PIC 9(03)V9(03)321.543
PIC 9(04)V9(02)23.24PIC ZZZ99.923.2
PIC 99V9900.34PIC $$$.99$.34
PIC X(04)MUSAXBXBXBM U S

2. SYNC Clause

01 MY-DATA.
   05 DATA-ONE   PIC X(6).
   05 DATA-TWO   PIC 9(6) COMP SYNC.
   05 DATA-THREE PIC S9(4)V99 COMP-3.

3. Redefines

01 WS-DATE PIC 9(06).
01 WS-REDEF-DATE REDEFINES WS-DATE.
   05 WS-YEAR   PIC 9(02).
   05 WS-MONTH  PIC 9(02).
   05 WS-DAY    PIC 9(02).

4. Renames

01 WS-REPSONSE.
   05 WS-CHAR143 	PIC X(03).
   05 WS-CHAR4	  PIC X(04).
   66 ADD-REPSONSE RENAMES WS-CHAR143.

5. Condition Name

01 GENDER PIC X.
   88 MALE     VALUE ‘1’
   88 FEMALE   VALUE ‘2’ ‘3’.

6. Arithmetic Operation - Add

ADD OperationABCD
ADD A TO BAA+B
ADD A B C TO DABCA+B+C+D
ADD A B C GIVING DABCA+B+C
ADD A TO B CAA+BA+C

7. Arithmetic Operation - Subtract

SUBTRACT OperationABCD
SUBTRACT A FROM BAB-A
SUBTRACT A B FROM CABC-(A+B)
SUBTRACT A B FROM C GIVING DABCC-(A+B)

8. Arithmetic Operation - Multiply

MULTIPLY OperationABCD
MULTIPLY A BY BAA*B
MULTIPLY A BY B GIVING CABA*B

9. Arithmetic Operation - Divide

DIVIDE OperationABCD
DIVIDE A INTO BAB/A
DIVIDE A INTO B GIVING CABB/A
DIVIDE A BY B GIVING CABA/B
DIVIDE A INTO B GIVING C REMAINDER DABInteger(B/A)Integer remainder

10. Giving Phrase in Arithmetic Operator

Arithmetic OperationABCD
ADD A B C GIVING DABCA+B+C
SUBTRACT A B FROM C GIVING DABCC-(A+B)
MULTIPLY A BY B GIVING CABA*B
DIVIDE A INTO B GIVING C REMAINDER DABInteger(B/A)Integer remainder

11. CORRESPONDING (CORR) phrase

05 WS-FIELD-1.
   10  WS-FIELD-A PIC S9(3).
   10  WS-FIELD-B PIC +99.9.
   10  WS-FIELD-C PIC X(4).
   10  WS-FIELD-D REDEFINES WS-FIELD-C PIC 9(4).
   10  WS-FIELD-E USAGE COMP-1.
   10  WS-FIELD-F USAGE INDEX.

05 WS-FIELD-2.
   10  WS-FIELD-A PIC 99.
   10  WS-FIELD-B PIC +9V9.
   10  WS-FIELD-C PIC A(4).
   10  WS-FIELD-D PIC 9(4).
   10  WS-FIELD-E PIC 9(9) USAGE COMP.
   10  WS-FIELD-F USAGE INDEX.
If ADD CORR WS-FIELD-2 TO WS-FIELD-1 is specified, WS-FIELD-A and WS-FIELD-A, WS-FIELD-B and WS-FIELD-B, and WS-FIELD-E and WS-FIELD-E are considered to be corresponding and are added together. WS-FIELD-C and WS-FIELD-C are not included because they are not numeric. WS-FIELD-D and WS-FIELD-D are not included because WS-FIELD-D includes a REDEFINES clause in its data description. WS-FIELD-F and WS-FIELD-F are not included because they are index data items.

12. Simple PERFORM Statement

PERFORM PARA-NAME-1.
PERFORM statement transfer the control to PARA-NAME-1, after execution of PARA-NAME-1, control return back to the next statement of PERFORM and continued the execution.
Note: Simple form of PERFORM acts very similar to GO TO statement, except that after execution of paragraph control will return back to the next statement of PERFORM statement

13. PERFORM PARA-NAME-1 THRU PARA-NAME-2 Statement

 PERFORM  PARA-NAME-1  THRU  PARA-NAME-2
PARA-NAME-1, PARA-NAME-2 are paragraph names. Execution of PERFORM statement causes control transfers to the first statement of PARA-NAME-1. control executes all the statements from the first statement of PARA-NAME-1 to last statement of PARA-NAME-2 and the control return to the next executable statement of PERFORM statement. if there are any other paragraphs between PARA-NAME-1 and PARA-NAME-2, all those statements are included for execution.

14. PERFORM Statement with TIMES phrase

PERFORM  PARA-NAME-1  THRU PARA-NAME-2  5 TIMES.
All the statements in the scope of PARA-NAME-1 thru PARA-NAME-2 are get executed 5 times. Control then passes to the next executable statement following PERFORM statement.

15. PERFORM Statement with UNTIL phrase

PERFORM PARA-NAME-1 VARIYING  WS-A FROM 1 BY  1  UNTIL WS-A > 5
All statements in PARA-NAME-1 are executed till the codition associated with UNTIL becomes true. For first iteration WS-A contains the value of 1 ( as it is specified after FROM ), for second iteration WS-A value increase by 1 ( as it is specified after BY keyword) and condition will be tested, if it false statements in PARA-NAME-1 will get executed and control come back to PERFORM, Now WS-A value increased again by 1 and condition will be tested, if it is false, statements in PARA-NAME-1 will get executed again. This loop continues till the condition becomes true.

16. PERFORM Statement with VARYING, AFTER phrases

PERFORM PARA-NAME-1 VARIYING  WS-A FROM 1 BY  1  UNTIL WS-A > 10
                            AFTER WS-B FROM 1 BY 1 UNTIL WS-B > 5
In this example, in addition to WS-A, WS-B value also get changed. For every valid value in WS-A, WS-B value start from 1 till WS-B > 5 becomes true. PERFORM statement execution ends only when WS-A > 10 becomes true.

17. Conditional Statement - Simple IF

IF Condition
    {Statement Block}
[END-IF].
If the condition is true, then it will execute the set of statements written in the IF block. If condition is not satisfied, the control will transfer to the next statements after the IF statement terminated.

END-IF is the scope terminator, which is optional in the program. The period (.) can be defined at the last statement of IF block. If we didn't specify the period, then scope terminator END-IF is mandatory.

18. Conditional Statement - IF ELSE

IF Condition-1
    {Statement-Block-1}
[ELSE]
    {Statement-Block-2}
[END-IF].
In IF-ELSE, the block of statements will be executed if the specified condition is true. If the condition is false, the other set of statements will be executed, and those set will be under the ELSE block.

19. Conditional Statement - Nested IF

IF Condition-1 THEN
    IF Condition-2 THEN
        Statements-block-1
    [ELSE
        Statements-block-2
    END-IF]
[ELSE
    IF Condition-3 THEN
        Statements-block-3
    [ELSE
        Statements-block-4
    END-IF]
END-IF.]
IF statement within the IF statement called as nested IF statement.

20. Simple EVALUATE Statement

EVALUATE WS-DATA-TYPE-IND
    WHEN 'A'
	  DISPLAY 'Alphabetic filed'
    WHEN '1'
	 DISPLAY 'Numeric filed'
    WHEN 'X'
	 DISPLAY 'Alpha numeric filed'
    WHEN OTHER
	 DISPLAY 'Invalid indicator value'
END-EVALUATE
This example evaluates WS-DATA-TYPE-IND, if WS-DATA-TYPE-IND is 'A' displays 'Alphabetic field', if WS-DATA-TYPE-IND is '1' displays 'Numeric filed', if WS-DATA-TYPE-IND is 'X' displays 'Alpha numeric filed'. If WS-DATA-TYPE-IND is not 'A' or '1' or 'X' then it displays 'Invalid indicator value'.

21. Evaluate Statement with ALSO

EVALUATE TRUE ALSO WS-GENDER ALSO WS-AGE

    WHEN WS-SALARY >= 1000 AND < 50000  ALSO  'M'  ALSO  25 THRU 50
          MOVE 0.5 TO WS-RATE-OF-INT

    WHEN WS-SALARY >= 1000 AND < 50000  ALSO  'F'  ALSO  25 THRU 60
          MOVE 0.25 TO WS-RATE-OF-INT

    WHEN OTHER
          MOVE 0    TO WS-RATE-OF-INT

END-EVALUATE.
This example is an example of decision table, calculates tax rate based on income, age and sex. First WHEN clause will satisfy if condition on income (>= 1000 and < 50000) is true, gender is 'M' and age is in range of 25 to 50, if it is true, then MOVE statement after first when will get executed, and control come out of EVALUATE.

If first WHEN condition becomes false, then control goes to second WHEN condition and check the condition, whether that is true or false, if it is true, it will execute MOVE statement after the second WHEN condition. if second WHEN condition is false, then control will go to the MOVE statement coded after WHEN OTHER, here it wont check for true or false.

22. STRING Statement

STRING FIRST-NAME DELIMITED BY " "
        " "      DELIMITED BY SIZE
        LAST-NAME DELIMITED BY " "
     INTO FULL-NAME.
Field NameValue
FIRST-NAMEABDUL
LAST-NAMEKALAM
FULL-NAMEABDUL KALAM

23. UNSTRING Statement

UNSTRING FULL-NAME
        DELIMITED BY " "
    INTO FIRST-NAME LAST-NAME.
Field NameValue
FULL-NAMEABDUL KALAM
FIRST-NAMEABDUL
LAST-NAMEKALAM

24. INSPECT Statement with TALLYING option

INSPECT WS-STRING TALLYING WS-COUNT FOR CHARACTER.
Field NameValue
WS-STRINGJOEBIDEN
WS-COUNT8
INSPECT WS-STRING TALLYING WS-COUNT FOR ALL 'E'.
Field NameValue
WS-STRINGJOE BIDEN
WS-COUNT2

25. INSPECT Statement with REPLACING option

INSPECT WS-STRING REPLACING ALL 'E' BY 'I'.
Field NameValue
WS-STRINGJOE BIDEN
WS-COUNTJOI BIDIN

26. Reference Modification in MOVE Statement

MOVE WS-FULL-NAME(1:3) TO WS-NAME
Field NameValue
WS-FULL-NAMEJOE BIDEN
WS-NAMEJOE
MOVE WS-PHONE          TO WS-COUNTRY-CODE(1:3)
Field NameValue
WS-PHONE+1-9876543210
WS-COUNTRY-CODE+1-