Cobol
Cobol
COBOL was created to match natural language. The COBOL program is made up of
paragraphs, sections, sentences, verbs and other common speaking language terms.
The 1st 6 characters are reserved for sequence numbers used for organizing punch
cards. The 7th is reserved for an * which denotes a comment. SOURCE FORMAT FREE
allows you to avoid this formatting and other such rules.
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. intro.
AUTHOR. Derek Banas .
DATE-WRITTEN. April 20th 2020
The identification division contains information about the program. Like the name
that is used to call for this programs code to execute. Also the authors name and
date created.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
Environment contains environment info being the computer it is running on, devices
available, country specific information.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
Data describes the data and has 4 sections being the file, working-storage, linkage
and report. The File section describes data sent or received from storage. Working-
storage defines variables in the program. Linkage defines data available to other
programs. Report deals with generating reports.
hello.cob
---------------------------------------------------------------------------------
Data
There are 3 types of data being Numeric, Alphanumeric and Alphabetic. While you can
declare a variable has a certain type you can still assign other types to it. The
programmer has the responsibility of enforcing types.
3 Types of Data
Figurative Constants
Figurative constants are named values that can be used to write a value to every
character in a variable. MOVE ZEROS TO PayCheck would make all values in PayCheck
zero.
---------------------------------------------------------------------------------
WORKING-STORAGE SECTION.
*> Can hold a alphanumeric with max length
*> of 30 and starting value You
01 UserName PIC X(30) VALUE "You".
PROCEDURE DIVISION.
*> Displays the string and doesn't skip to a newline
DISPLAY "What is your name " WITH NO ADVANCING
*> Stores the value entered
ACCEPT UserName
DISPLAY "Hello " UserName
tutorial2.cob
WORKING-STORAGE SECTION.
*> Defines an alphanumeric type 10 spaces long
*> with the default value of "Stuff" (System Defined Max Length)
*> The Picture Clause is where we define the
*> data type and size (COBOL isn't a typed language)
*> X means any type of character on your keyboard
01 SampleData PIC X(10) VALUE "Stuff".
PROCEDURE DIVISION.
*> MOVE is used to assign values
MOVE "More Stuff" TO SampleData
MOVE "123" TO SampleData
*> You can assign numerics to alphanumerics
*> because typing isn't enforced
MOVE 123 TO SampleData
DISPLAY SampleData
DISPLAY PayCheck
*> Entering data this way requires additonal
*> filled spaces
MOVE "123Bob Smith 12211974" TO Customer
DISPLAY CustName
DISPLAY MOB "/" DOB "/" YOB
*> Math
STOP RUN.
tutorial3.cob
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Age PIC 99 VALUE 0.
01 Grade PIC 99 VALUE 0.
01 Score PIC X(1) VALUE "B".
PROCEDURE DIVISION.
*> If Conditional
DISPLAY "Enter Age : " WITH NO ADVANCING
ACCEPT Age
IF Age > 18 THEN
DISPLAY "You can vote"
ELSE
DISPLAY "You can't vote"
END-IF
STOP RUN.
tutorial4.cob
PROCEDURE DIVISION.
*> Gravity driven programming falls through the
*> code until a condition or goto redirects it
*> Open paragraphs are executed through gravity
*> while closed paragraphs are executed by name.
*> Open paragraphs are basically ways to name blocks
*> of code (tags).
*> Data created in a closed paragraph can't be
*> accessed outside of it. They are traditional functions.
SubThree.
DISPLAY "In Paragraph 3".
SubTwo.
DISPLAY "In Paragraph 2"
PERFORM SubThree
DISPLAY "Returned to Paragraph 2".
Subroutines
You can compile a subroutine separately and then use its code in another program.
GETSUM.cob
EXIT PROGRAM.
coboltut45.cob
STOP RUN.
tutorial5.cob
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Ind PIC 9(1) VALUE 0.
PROCEDURE DIVISION.
WhileLoop.
*> Works like while loop that executes while the index
*> is greater than 5
PERFORM OutputData WITH TEST AFTER UNTIL Ind > 5
*> Jumps to another paragraph
GO TO ForLoop.
OutputData.
DISPLAY Ind.
ADD 1 TO Ind.
*> Perform varying works like a for loop where Ind starts
*> with a value of 1 defined after FROM and increments by
*> 1 defined after BY until the condition is met
ForLoop.
PERFORM OutputData2 VARYING Ind FROM 1 BY 1 UNTIL Ind=5
STOP RUN.
OutputData2.
DISPLAY Ind.
tutorial20.cob
PROCEDURE DIVISION.
MOVE StartNum TO NoZero
DISPLAY NoZero
MOVE StartNum TO NoZPlusC
DISPLAY NoZPlusC
MOVE StartNum TO Dollar
DISPLAY Dollar
MOVE BDay TO ADate
DISPLAY ADate
STOP RUN.
tutorial21.cob
PROCEDURE DIVISION.
DISPLAY "Enter the Price : " WITH NO ADVANCING
ACCEPT Price
COMPUTE FullPrice ROUNDED = Price + (Price * TaxRate)
DISPLAY "Price + Tax : " FullPrice.
STOP RUN.
tutorial22.cob
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SampStr PIC X(18) VALUE 'eerie beef sneezed'.
01 NumChars PIC 99 VALUE 0.
01 NumEs PIC 99 VALUE 0.
01 FName PIC X(6) VALUE 'Martin'.
01 MName PIC X(11) VALUE 'Luther King'.
01 LName PIC X(4) VALUE 'King'.
01 FLName PIC X(11).
01 FMLName PIC X(18).
01 SStr1 PIC X(7) VALUE "The egg".
01 SStr2 PIC X(9) VALUE "is #1 and".
01 Dest PIC X(33) VALUE "is the big chicken".
01 Ptr PIC 9 VALUE 1.
01 SStr3 PIC X(3).
01 SStr4 PIC X(3).
PROCEDURE DIVISION.
*> Takes string SampStr counts all characters and
*> stores the value in NumChars
INSPECT SampStr TALLYING NumChars FOR CHARACTERS.
DISPLAY "Number of Characters : " NumChars.
STOP RUN.
tutorial6.cob
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
*> Connect the name of the customer file name in this
*> code to a file. Records on separate lines
FILE-CONTROL.
SELECT CustomerFile ASSIGN TO "Customer.dat"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL.
DATA DIVISION.
*> File section describes data in files
FILE SECTION.
*> FD (File Description) describes the file layout
FD CustomerFile.
*> Design the customer record
01 CustomerData.
02 IDNum PIC 9(8).
02 CustName.
03 FirstName PIC X(15).
03 LastName PIC X(15).
WORKING-STORAGE SECTION.
01 WSCustomer.
02 WSIDNum PIC 9(5).
02 WSCustName.
03 WSFirstName PIC X(15).
03 WSLastName PIC X(15).
PROCEDURE DIVISION.
*> COBOL focuses on working with external files or
*> databases. Here we will work with sequential files
*> which are files you must work with in order. They
*> differ from direct access files in that direct access
*> files have keys associated with data.
*> Field : Individual piece of information (First Name)
*> Record : Collection of fields for an individual object
*> File : Collection of numerous Records
tutorial7.cob
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CustomerFile ASSIGN TO "Customer.dat"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD CustomerFile.
01 CustomerData.
02 IDNum PIC 9(8).
02 CustName.
03 FirstName PIC X(15).
03 LastName PIC X(15).
WORKING-STORAGE SECTION.
01 WSCustomer.
02 WSIDNum PIC 9(5).
02 WSCustName.
03 WSFirstName PIC X(15).
03 WSLastName PIC X(15).
PROCEDURE DIVISION.
*> Extend adds new data to the end of the file
OPEN EXTEND CustomerFile.
DISPLAY "Customer ID " WITH NO ADVANCING
ACCEPT IDNum.
DISPLAY "Customer First Name " WITH NO ADVANCING
ACCEPT FirstName.
DISPLAY "Customer Last Name " WITH NO ADVANCING
ACCEPT LastName.
WRITE CustomerData
END-WRITE.
CLOSE CustomerFile.
*> Enter customers using ascending keys for later example
STOP RUN.
tutorial8.cob
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CustomerFile ASSIGN TO "Customer.dat"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD CustomerFile.
01 CustomerData.
02 IDNum PIC 9(8).
02 CustName.
03 FirstName PIC X(15).
03 LastName PIC X(15).
WORKING-STORAGE SECTION.
01 WSCustomer.
02 WSIDNum PIC 9(5).
02 WSCustName.
03 WSFirstName PIC X(15).
03 WSLastName PIC X(15).
*> NEW : Used to react to end of file
01 WSEOF PIC A(1).
PROCEDURE DIVISION.
*> Input is used to read from the file
OPEN INPUT CustomerFile.
PERFORM UNTIL WSEOF='Y'
READ CustomerFile INTO WSCustomer
AT END MOVE 'Y' TO WSEOF
NOT AT END DISPLAY WSCustomer
END-READ
END-PERFORM.
CLOSE CustomerFile.
STOP RUN.
tutorial9.cob
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
*> Define the file to save the report to
SELECT CustomerReport ASSIGN TO "CustReport.rpt"
ORGANIZATION IS LINE SEQUENTIAL.
*> The file that provides the data
SELECT CustomerFile ASSIGN TO "Customer.dat"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
*> Define FD and custom print line
FD CustomerReport.
01 PrintLine PIC X(44).
WORKING-STORAGE SECTION.
*> Break the report up into pieces
01 PageHeading.
02 FILLER PIC X(13) VALUE "Customer List".
01 PageFooting.
02 FILLER PIC X(15) VALUE SPACE.
02 FILLER PIC X(7) VALUE "Page : ".
02 PrnPageNum PIC Z9.
*> Column headings for data
01 Heads PIC X(36) VALUE "IDNum FirstName LastName".
*> Customer data to print with spaces defined
01 CustomerDetailLine.
02 FILLER PIC X VALUE SPACE.
02 PrnCustID PIC 9(8).
02 FILLER PIC X(4) VALUE SPACE.
02 PrnFirstName PIC X(15).
02 FILLER PIC XX VALUE SPACE.
02 PrnLastName PIC X(15).
*> Printed at end of report
01 ReportFooting PIC X(13) VALUE "END OF REPORT".
*> Tracks number of lines used, when to print footer
*> and new heading
01 LineCount PIC 99 VALUE ZERO.
88 NewPageRequired VALUE 40 THRU 99.
*> Track number of pages
01 PageCount PIC 99 VALUE ZERO.
PROCEDURE DIVISION.
PrintReport.
OPEN INPUT CustomerFile
OPEN OUTPUT CustomerReport
PERFORM PrintPageHeading
*> Read customer file until end
READ CustomerFile
AT END SET WSEOF TO TRUE
END-READ
PERFORM PrintReportBody UNTIL WSEOF
*> Advancing moves down defined number of lines
WRITE PrintLine FROM ReportFooting AFTER ADVANCING 5 LINES
CLOSE CustomerFile, CustomerReport
STOP RUN.
tutorial10.cob
>>SOURCE FORMAT FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. tutorial10.
*> This program has a menu system and allows you to
*> Add, Update, Delete and Display Customer Data
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
*> Select to use a file with keys (Indexed File)
*> We will randomly access data vs. sequential
*> Define the name associated with the key
SELECT CustomerFile ASSIGN TO "customers.txt"
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS IDNum.
DATA DIVISION.
FILE SECTION.
*> Model customer data
FD CustomerFile.
01 CustomerData.
02 IDNum PIC 99.
02 FirstName PIC X(15).
02 LastName PIC X(15).
WORKING-STORAGE SECTION.
*> Customer menu choice
01 Choice PIC 9.
*> Tracks whether to exit
01 StayOpen PIC X VALUE 'Y'.
*> Tracks whether the customer exists
01 CustExists PIC X.
PROCEDURE DIVISION.
StartPara.
*> To access data randomly you must use I-O mode
OPEN I-O CustomerFile.
*> Continue execution until StayOpen is N which
*> happens if the user enters a number not 1 thru 4
PERFORM UNTIL StayOpen='N'
DISPLAY " "
DISPLAY "CUSTOMER RECORDS"
DISPLAY "1 : Add Customer"
DISPLAY "2 : Delete Customer"
DISPLAY "3 : Update Customer"
DISPLAY "4 : Get Customer"
DISPLAY "0 : Quit"
DISPLAY ": " WITH NO ADVANCING
ACCEPT Choice
*> Execute different paragraphs based on option
EVALUATE Choice
WHEN 1 PERFORM AddCust
WHEN 2 PERFORM DeleteCust
WHEN 3 PERFORM UpdateCust
WHEN 4 PERFORM GetCust
*> When N we jump out of the loop
WHEN OTHER move 'N' TO StayOpen
END-EVALUATE
END-PERFORM.
*> Close the file and stop execution
CLOSE CustomerFile
STOP RUN.
AddCust.
DISPLAY " ".
DISPLAY "Enter ID : " WITH NO ADVANCING.
ACCEPT IDNum.
DISPLAY "Enter First Name : " WITH NO ADVANCING.
ACCEPT FirstName.
DISPLAY "Enter Last Name : " WITH NO ADVANCING.
ACCEPT LastName.
DISPLAY " ".
*> Write customer data or display error if ID taken
WRITE CustomerData
INVALID KEY DISPLAY "ID Taken"
END-WRITE.
DeleteCust.
DISPLAY " ".
DISPLAY "Enter Customer ID to Delete : " WITH NO ADVANCING.
ACCEPT IDNum.
*> Delete customer based on ID
DELETE CustomerFile
INVALID KEY DISPLAY "Key Doesn't Exist"
END-DELETE.
UpdateCust.
MOVE 'Y' TO CustExists.
DISPLAY " ".
DISPLAY "Enter ID to Update : " WITH NO ADVANCING.
ACCEPT IDNum.
*> Read customer or mark N if doesn't exist
READ CustomerFile
INVALID KEY MOVE 'N' TO CustExists
END-READ.
*> Display error because ID doesn't exist
IF CustExists='N'
DISPLAY "Customer Doesn't Exist"
ELSE
DISPLAY "Enter the New First Name : " WITH NO ADVANCING
ACCEPT FirstName
DISPLAY "Enter the New Last Name : " WITH NO ADVANCING
ACCEPT LastName
END-IF.
*> Update record for matching ID
REWRITE CustomerData
INVALID KEY DISPLAY "Customer Not Updated"
END-REWRITE.
GetCust.
*> Assume customer exists
MOVE 'Y' TO CustExists.
DISPLAY " ".
DISPLAY "Enter Customer ID to Find : " WITH NO ADVANCING.
ACCEPT IDNum.
*> Mark N if customer ID doesn't exist
READ CustomerFile
INVALID KEY MOVE 'N' TO CustExists
END-READ.
*> Display error
IF CustExists='N'
DISPLAY "Customer Doesn't Exist"
ELSE
DISPLAY "ID : " IDNum
DISPLAY "First Name : " FirstName
DISPLAY "Last Name : " LastName
END-IF.
tutorial11.cob
WORKING-STORAGE SECTION.
*> Declare a 1 dimensional table
01 Table1.
02 Friend PIC X(15) OCCURS 4 TIMES.
PROCEDURE DIVISION.
*> Fill 1D table with data and output
MOVE 'Joy' TO Friend(1).
MOVE 'Willow' TO Friend(2).
MOVE 'Ivy' TO Friend(3).
DISPLAY Friend(1).
DISPLAY Table1.
GetProduct.
DISPLAY Product(I).
*> Get associated product sizes
PERFORM GetSizes VARYING J FROM 1 BY 1 UNTIL J>3.
GetSizes.
DISPLAY ProdSize(I,J).
LookUp.
SET I TO 1.
*> Search will look for supplied value or
*> output Not Found
SEARCH Product
AT END DISPLAY 'Product Not Found'
WHEN ProdName(I) = 'Red Shirt'
DISPLAY 'Red Shirt Found'
END-SEARCH.
STOP RUN.
tutorial12.cob
PROCEDURE DIVISION.
DISPLAY Shirt(1).
MOVE '123456' TO TextNum.
DISPLAY FloatNum.
STOP RUN.
tutorial13.cob
PROCEDURE DIVISION.
SORT WorkFile ON ASCENDING KEY SIDNum
USING OrgFile
GIVING SortedFile.
STOP RUN.
tutorial14.cob
PROCEDURE DIVISION.
MERGE WorkFile ON ASCENDING KEY NIDNum
USING File1, File2
GIVING NewFile.
STOP RUN.
REPOSITORY.
CLASS ToDoCls AS "todolist".
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WorkToDo USAGE OBJECT REFERENCE ToDoCls.
01 ToDoToAdd PIC X(20).
88 EndOfInput VALUE SPACES.
01 DescToAdd PIC X(50).
PROCEDURE DIVISION.
INVOKE ToDoCls "new" USING BY CONTENT "Work ToDo"
RETURNING WorkToDo
STOP RUN.
AddToToDoList.
DISPLAY "Enter To Do : " WITH NO ADVANCING
ACCEPT ToDoToAdd
DISPLAY "Enter Description : " WITH NO ADVANCING
ACCEPT DescToAdd
INVOKE WorkToDo "AddItemToToDo"
USING BY CONTENT ToDoToAdd, DescToAdd.