COBOL Programming Syntax
This section contains numerous example programs for better understanding of COBOL.
SAMPLE PROGRAM -1
This is a demo program to learn and understand major difference between elementary/individual item and group item.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
IDENTIFICATION DIVISION.
PROGRAM–ID. HELLO–WORLD.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING–STORAGE SECTION.
01 LEVEL–1 PIC 9(03) VALUE 256. à INDIVIDUAL DATA ITEM
01 LEVEL–GROUP. à GROUP DATA ITEM
05 LEVEL–21 PIC 9(03) VALUE 256. à ELEMENTARY DATA ITEM
05 LEVEL–22 PIC 9(03) VALUE 128. à ELEMENTARY DATA ITEM
PROCEDURE DIVISION.
DISPLAY ‘ INDIVIDUAL DATA ITEM : ‘ LEVEL–1.
DISPLAY ‘ GROUP DATA ITEM : ‘ LEVEL–GROUP.
DISPLAY ‘ ELEMENTARY DATA ITEM – 1 : ‘ LEVEL–21.
DISPLAY ‘ ELEMENTARY DATA ITEM – 2 : ‘ LEVEL–22.
STOP RUN.
|
* * * * THE OUTPUT WILL BE * * * *
1
2
3
4
5
6
|
INDIVIDUAL DATA ITEM : 256
GROUP DATA ITEM : 256128
ELEMENTARY DATA ITEM – 1 : 256
ELEMENTARY DATA ITEM – 2 : 128
|
SAMPLE PROGRAM -2
This is a demo program to learn and understand various level numbers in details.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
WORKING–STORAGE SECTION.
01 WS–DISPLAY PIC X(25).
01 STUDENTS.
05 STUDENT–ID PIC 9(5).
05 FILLER PIC X(01).
05 STUDENT–NAME.
10 FNAME PIC A(08).
10 LNAME PIC A(05).
05 STUDENT–DOB PIC X(10).
05 STUDENT–DEPT PIC X(3).
88 COMPUTER VALUE, ‘CMP’.
88 ELECTRONICS VALUE ‘EEE’.
88 MECHANICAL VALUE, ‘MEC’.
88 CIVIL VALUE ‘CIV’,
66 STUDENT–DETAILS RENEWS STUDENT–ID THRU STUDENT–NAME.
77 WS–STUDENT–FLAG PIC X(1)
PROCEDURE DIVISION.
MAIN–PARA.
MOVE “VARIOUS LEVELS EXAMPLE:” TO WS–DISPLAY.
MOVE 12345 TO STUDENT–ID.
MOVE ‘MICHEL’ TO FNAME.
MOVE ‘EVE’ TO LNAME.
MOVE ’21/08/2020′ TO STUDENT–DOB.
DISPLAY WS–DISPLAY.
DISPLAY “STUDENT FROM 01 LEVEL”
DISPLAY STUDENTS.
DISPLAY “STUDENTS DETAILS FROM 66 LEVELS”
DISPLAY STUDENT–DETAILS.
SET COMPUTER TO TRUE.
DISPLAY “STUDENT-DEPARTMENT: “ STUDENT–DEPT.
MOVE ‘A’ TO WS–STUDENT–FLAG
DISPLAY “FROM 77 LEVELS: “ WS–STUDENT–FLAG
STOP RUN.
|
OUTPUT:
1
2
3
4
5
6
7
8
9
|
VARIOUS LEVELS EXAMPLES :
STUDENT FROM 01 LEVELS
12345 MICHEL EVE 21/08/2020
STUDENT DETAILS FROM 66 LEVEL
12345 MICHEL EVE
STUDENT DEPARTMENT: CMP
FROM 77 LEVEL: A
|
Sample program – 3
This is a demo program to learn and understand the clear use of pic/picture clause.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
IDENTIFICATION DIVISION.
PROGRAM–ID. HELLO.
DATA DIVISION.
WORKING–STORAGE SECTION.
01 WSRS–NUM01 PIC S9(3)V9(2).
01 WSRS–NUM02 PIC PPP999.
01 WSRS–NUM03 PIC S9(3)V9(2) VALUE –123.45.
01 WSRS–NAME PIC A(6) VALUE ‘ABCDEF’.
01 WSRS–ID PIC X(5) VALUE ‘A121$’.
PROCEDURE DIVISION.
DISPLAY “WSRS-NUM01: “WSRS–NUM01.
DISPLAY “WSRS-NUM02: “WSRS–NUM02.
DISPLAY “WSRS-NUM03: “WSRS–NUM03.
DISPLAY “WSRS-NAME: “WSRS–NAME.
DISPLAY “WSRS-ID: “WSRS–ID.
STOP RUN.
|
THE OUTPUT WILL BE:
1
2
3
4
5
6
7
|
WSRS–NUM1: +000.00
WSRS–NUM2: .000000
WSRS–NUM3: –123.45
WSRS–NAME: ABCDEF
WSRS–ID: A121$
|
Sample program -4
This is a demo program to learn and understand the concept of value clause.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
IDENTIFICATION DIVISION.
PROGRAM–ID. WORLD01.
DATA DIVISION.
WORKING–STORAGE SECTION.
01 WRKR–NUM1 PIC 99V9 VALUE IS 5.5.
01 WRKR–NAME PIC A(6) VALUE ‘ EFG ‘.
01 WRKR–ID PIC 99 VALUE ZERO.
PROCEDURE DIVISION.
DISPLAY “WRKR-NUM1: “WRKR–NUM1.
DISPLAY “WRKR-NAME: “WRKR–NAME.
DISPLAY “WRKR-ID: “WRKR–ID.
STOP RUN.
|
THE OUTPUT WILL BE:
1
2
3
4
5
|
WRKR–NUM1: 05.5
WRKR–NAME: EFG
WRKR–ID: 00
|
SAMPLE PROGRAM -5
This is a demo program to create record of an employee.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
IDENTIFICATION DIVISION.
PROGRAM–ID. HELLO–WORLD.
DATA DIVISION.
WORKING–STORAGE SECTION.
01 EMP–DTL.
02 EMP–NUM PIC 9(03).
02 EMP–NAME.
05 EMP–NAME–INIT PIC X(01).
88 EMP–NAME–VALID VALUE ‘A’ THRU ‘Z’.
88 EMP–NAME–INVALID VALUE ‘0’ THRU ‘9’.
05 EMP–NAME–REST PIC X(14).
02 EMP–GENDER PIC X(01).
88 VALID–GENDER VALUE ‘M’ ‘F’.
88 MALE VALUE ‘M’.
88 FEMALE VALUE ‘F’.
02 EMP–MARKS PIC 9(03).
88 FIRST–CLASS VALUE 060 THRU 100.
88 SECOND–CLASS VALUE 050 THRU 059.
88 THIRD–CLASS VALUE 035 THRU 049.
88 FAIL VALUE 000 THRU 034.
PROCEDURE DIVISION.
ACCEPT EMP–NUM.
ACCEPT EMP–NAME.
ACCEPT EMP–GENDER.
ACCEPT EMP–MARKS.
DISPLAY ‘ EMPLOYEE DETAILS . . . . . ‘.
DISPLAY ‘ EMPLOYEE NUMBER : ‘ EMP–NUM.
IF EMP–NAME–VALID
DISPLAY ‘ EMPLOY NAME : ‘ EMP–NAME
ELSE
DISPLAY ‘ EMPLOY NAME WAS INVALID ‘
END–IF.
IF VALID–GENDER
IF MALE
DISPLAY ‘EMPLOY GENDER : MALE’
ELSE
DISPLAY ‘EMPLOY GENDER : FEMALE’
END–IF
ELSE
DISPLAY ‘ INVALID GENDER WAS GIVEN ‘
END–IF.
EVALUATE TRUE
WHEN FIRST–CLASS
DISPLAY ‘ EMPLOY GOT FIRST CLASS ‘
WHEN SECOND–CLASS
DISPLAY ‘ EMPLOYE GOT SECOND CLASS ‘
WHEN THIRD–CLASS
DISPLAY ‘ EMPLOY GOT THIRD CLASS ‘
WHEN OTHER
DISPLAY ‘ EMPLOY GOT FAILED ‘
END–EVALUATE.
STOP RUN.
|
THE OUTPUT WILL BE:
1
2
3
4
5
6
7
|
EMPLOYEE DETAILS . . . . .
EMPLOYEE NUMBER : 000
EMPLOY NAME WAS INVALID
INVALID GENDER WAS GIVEN
EMPLOY GOT FAILED
|
1
2
3
|
|
Here in this program, we have created a record of an employee working for a firm XYZ. This program is designed to display all the details of an employee, such as, his gender and his scores acquired in previous survey.