MisterTootor M.S., B.S., A.S., A.S.B
I'm a paragraph. Click here to add your own text and edit me. It's easy.
​
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. ShortestProgram.
PROCEDURE DIVISION.
​
​​
DisplayPrompt.
DISPLAY "I did it".
STOP RUN.
A very short COBOL program
​
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEN ASSIGN TO INPUT.
ORGANIZATION IS SEQUENTIAL.
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILEN 01 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT PIC A(30).
01 WS-ID PIC 9(5).
LOCAL-STORAGE SECTION.
01 LS-CLASS PIC 9(3).
LINKAGE SECTION. 01 LS-ID PIC 9(5).
PROCEDURE DIVISION.
DISPLAY 'Executing COBOL program using JCL'.
STOP RUN.
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
Output
Display a Message
Executing COBOL program using JCL
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. Conditions.
AUTHOR. Michael Coughlan.
* An example program demonstrating the use of
* condition names (level 88's).
* The EVALUATE and PERFORM verbs are also used.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Char PIC X.
88 Vowel VALUE "a", "e", "i", "o", "u".
88 Consonant VALUE "b", "c", "d", "f", "g", "h"
"j" THRU "n", "p" THRU "t", "v" THRU "z".
88 Digit VALUE "0" THRU "9".
88 ValidCharacter VALUE "a" THRU "z", "0" THRU "9".
PROCEDURE DIVISION.
Begin.
DISPLAY "Enter lower case character or digit. No data ends.".
ACCEPT Char.
PERFORM UNTIL NOT ValidCharacter
EVALUATE TRUE
WHEN Vowel DISPLAY "The letter " Char " is a vowel."
WHEN Consonant DISPLAY "The letter " Char " is a consonant."
WHEN Digit DISPLAY Char " is a digit."
WHEN OTHER DISPLAY "problems found"
END-EVALUATE
END-PERFORM
STOP RUN.
Demonstrating Conditions
Reads a sequential file and displays the record
​
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. SeqRead.
AUTHOR. Michael Coughlan.
* An example program showing how to read a sequential file.
* This is the definitive example
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
88 EndOfStudentFile VALUE HIGH-VALUES.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(4).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Gender PIC X.
PROCEDURE DIVISION.
Begin.
OPEN INPUT StudentFile
READ StudentFile
AT END SET EndOfStudentFile TO TRUE
END-READ
PERFORM UNTIL EndOfStudentFile
DISPLAY StudentId SPACE StudentName SPACE CourseCode SPACE YOBirth
READ StudentFile
AT END SET EndOfStudentFile TO TRUE
END-READ
END-PERFORM
CLOSE StudentFile
STOP RUN.
​
​
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. MonthTable.
AUTHOR. Michael Coughlan.
* This program counts the number of students born in each month and
* displays the result.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
88 EndOfStudentFile VALUE HIGH-VALUES.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(4).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Gender PIC X.
WORKING-STORAGE SECTION.
01 MonthTable.
02 TableValues.
03 FILLER PIC X(18) VALUE "January February".
03 FILLER PIC X(18) VALUE "March April".
03 FILLER PIC X(18) VALUE "May June".
03 FILLER PIC X(18) VALUE "July August".
03 FILLER PIC X(18) VALUE "SeptemberOctober".
03 FILLER PIC X(18) VALUE "November December".
02 FILLERh REDEFINES TableValues.
03 Month OCCURS 12 TIMES PIC X(9).
01 MonthCount OCCURS 12 TIMES PIC 999 VALUE ZEROS.
01 MonthIdx PIC 999.
01 HeadingLine PIC X(19) VALUE " Month StudCount".
01 DisplayLine.
02 PrnMonth PIC X(9).
02 FILLER PIC X(4) VALUE SPACES.
02 PrnStudentCount PIC ZZ9.
PROCEDURE DIVISION.
Begin.
OPEN INPUT StudentFile
READ StudentFile
AT END SET EndOfStudentFile TO TRUE
END-READ
PERFORM UNTIL EndOfStudentFile
ADD 1 TO MonthCount(MOBirth)
READ StudentFile
AT END SET EndOfStudentFile TO TRUE
END-READ
END-PERFORM
DISPLAY HeadingLine
PERFORM VARYING MonthIdx FROM 1 BY 1 UNTIL MonthIdx > 12
MOVE Month(MonthIdx) TO PrnMonth
MOVE MonthCount(MonthIdx) TO PrnStudentCount
DISPLAY DisplayLine
END-PERFORM.
CLOSE StudentFile
STOP RUN.
Counts the number of students born each month
Given this Excel table below:
Cde County ID IdnCase Applicant Last Name Applicant First Name Address1 Address2 City State Zip
1 9999979 Name Joe 13 mockingbird lane Somewhere1 NJ 99999
2 9999980 Name2 Joe 14 mockingbird lane Somewhere2 NJ 99999
3 9999981 Someone Joe 15 mockingbird lane Somewhere3 NJ 99999
4 9999982 Somebody Joe 16 mockingbird lane Somewhere4 NJ 99999
5 9999983 Jones Harry 17 mockingbird lane Somewhere5 NJ 99999
6 9999984 Jones2 Harry 18 mockingbird lane Somewhere6 NJ 99999
7 9999985 Jones3 Tom 19 mockingbird lane Somewhere7 NJ 99999
8 9999986 Jones4 Tom 20 mockingbird lane Somewhere8 NJ 99999
9 9999987 Jones5 Tom 21 mockingbird lane Somewhere9 NJ 99999
10 9999988 Jones6 moe 22 mockingbird lane Somewhere10 NJ 99999
11 9999989 Jones7 Tom 23 mockingbird lane Somewhere11 NJ 99999
12 9999990 Jones8 BOB 24 mockingbird lane Somewhere12 NJ 99999
13 9999991 Jones9 BOB 25 mockingbird lane Somewhere13 NJ 99999
14 9999992 Jones10 BOB 26 mockingbird lane Somewhere14 NJ 99999
15 9999993 Jones11 BOB 27 mockingbird lane Somewhere15 NJ 99999
16 9999994 Jones12 BOB 28 mockingbird lane Somewhere16 NJ 99999
17 9999995 Jones13 BOB 29 mockingbird lane Somewhere17 NJ 99999
18 9999996 Jones14 BOB 30 mockingbird lane Somewhere18 NJ 99999
19 9999997 Jones15 BOB 31 mockingbird lane Somewhere19 NJ 99999
20 9999998 Jones16 BOB 32 mockingbird lane Somewhere20 NJ 99999
21 9999999 Jones17 BOB 33 mockingbird lane Somewhere21 NJ 99999
​
IDENTIFICATION DIVISION.
PROGRAM-ID. ORGANIZE-TABLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TABLE-ENTRY.
05 COUNTY-ID PIC X(10).
05 CASE-ID PIC X(10).
05 APPLICANT-LAST-NAME PIC X(30).
05 APPLICANT-FIRST-NAME PIC X(30).
05 ADDRESS1 PIC X(30).
05 ADDRESS2 PIC X(30).
05 CITY PIC X(30).
05 STATE PIC X(2).
05 ZIP PIC X(10).
01 INPUT-TABLE.
05 INPUT-ENTRY OCCURS 21 TIMES.
10 COUNTY-ID PIC X(10).
10 CASE-ID PIC X(10).
10 APPLICANT-LAST-NAME PIC X(30).
10 APPLICANT-FIRST-NAME PIC X(30).
10 ADDRESS1 PIC X(30).
10 ADDRESS2 PIC X(30).
10 CITY PIC X(30).
10 STATE PIC X(2).
10 ZIP PIC X(10).
PROCEDURE DIVISION.
MAIN-PROCEDURE.
OPEN INPUT 'YOUR_TABLE_FILE_NAME'
PERFORM READ-TABLE UNTIL EOF
CLOSE INPUT 'YOUR_TABLE_FILE_NAME'
DISPLAY 'Data has been read successfully.'
STOP RUN.
READ-TABLE.
READ TABLE-FILE INTO TABLE-ENTRY
AT END SET EOF TO TRUE
END-READ
MOVE TABLE-ENTRY TO INPUT-ENTRY
```
​​
​
​
Replace `'YOUR_TABLE_FILE_NAME'` with the name of the file containing your table data. This COBOL program reads the table data from the file, organizes it into an array of input entries, and stops when it reaches the end of the file.
```
The shell properties below, contain the detail table above:
The Output
Show me the source code to build a basic Cobol module along with an output
IDENTIFICATION DIVISION.
PROGRAM-ID. HelloWorld.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-GREETING PIC A(20) VALUE 'Hello, World!'.
PROCEDURE DIVISION.
DISPLAY WS-GREETING.
STOP RUN.
Sample Output:
Hello, World!
How to Compile the COBOL Program​:
cobc -x HelloWorld.cob
How to run the COBOL Program​
./HelloWorld
The PIC Environment
IDENTIFICATION DIVISION.
PROGRAM-ID. STATE-DFD-INPUT-REPORT.
​
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPFILE ASSIGN TO 'DataTest.txt'
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-FILE-STATUS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-DATA.
05 CDE-COUNT PIC X(2).
05 IDN-CASE PIC X(7).
05 APPL-LAST-NAME PIC X(15).
05 APPL-FIRST-NAME PIC X(15).
05 ADDRESS1 PIC X(25).
05 ADDRESS2 PIC X(20).
05 CITY PIC X(15).
05 STATE PIC X(2).
05 ZIP PIC X(5).
05 CDE-TIER-LEVEL-CCDBG PIC X(1).
05 POVERTY-LEVEL PIC X(25).
05 NUM-PROGRAM-ACCOUNT PIC X(3).
05 IDN-CHILD PIC X(7).
05 NME-FIRST PIC X(20).
05 NME-LAST PIC X(20).
05 CDE-RACE PIC X(2).
05 CDE-ETHNIC PIC X(1).
05 CAT-CARE PIC X(1).
05 AGE PIC X(3).
05 DTE-BIRTH PIC X(10).
05 F-P-CARE PIC X(1).
05 IDN-PROVIDER PIC X(6).
05 NME-PROVIDER PIC X(20).
05 ADD1 PIC X(20).
05 ADD-CITY PIC X(20).
05 ADD-STATE PIC X(2).
05 ADD-ZIP PIC X(5).
05 CDE-PROVIDER-CARE-TYPE PIC X(1).
05 NUMB-GNJK-CARE-TYPE PIC X(1).
05 NUM-GNJK-LEVEL PIC X(1).
05 EMAIL PIC X(30).
The Code to Run
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT EMPFILE.
PERFORM READ-PARA THRU READ-PARA-EXIT UNTIL WS-EOF="Y".
CLOSE EMPFILE.
STOP RUN.
MAIN-PARA-EXIT.
EXIT.
READ-PARA.
MOVE ZERO TO CDE-Count.
READ EMPFILE
AT END
MOVE "Y" TO WS-EOF
NOT AT END
DISPLAY '*******', NEWFILE
END-READ.
READ-PARA-EXIT.
EXIT.
Show me the source code with a table of data I can use to run a basic cobol program
IDENTIFICATION DIVISION.
PROGRAM-ID. TableExample.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE-SIZE PIC 9(02) VALUE 5.
01 WS-INDEX PIC 9(02) VALUE 1.
01 WS-NAME-TABLE.
05 WS-NAME OCCURS 5 TIMES INDEXED BY IDX.
10 WS-FIRST-NAME PIC A(20).
10 WS-LAST-NAME PIC A(20).
PROCEDURE DIVISION.
* Initialize table data
MOVE 'John' TO WS-FIRST-NAME (1).
MOVE 'Doe' TO WS-LAST-NAME (1).
MOVE 'Jane' TO WS-FIRST-NAME (2).
MOVE 'Smith' TO WS-LAST-NAME (2).
MOVE 'Alice' TO WS-FIRST-NAME (3).
MOVE 'Johnson' TO WS-LAST-NAME (3).
MOVE 'Bob' TO WS-FIRST-NAME (4).
MOVE 'Brown' TO WS-LAST-NAME (4).
MOVE 'Charlie' TO WS-FIRST-NAME (5).
MOVE 'Davis' TO WS-LAST-NAME (5).
* Display header
DISPLAY 'Name List:'.
* Loop through the table and display names
PERFORM DISPLAY-NAMES UNTIL WS-INDEX > WS-TABLE-SIZE.
STOP RUN.
DISPLAY-NAMES.
DISPLAY 'First Name: ' WS-FIRST-NAME (WS-INDEX)
' Last Name: ' WS-LAST-NAME (WS-INDEX).
ADD 1 TO WS-INDEX.
Sample Output:
​
Name List:
First Name: John Last Name: Doe
First Name: Jane Last Name: Smith
First Name: Alice Last Name: Johnson
First Name: Bob Last Name: Brown
First Name: Charlie Last Name: Davis