Software Engineers Handbook:Language Dictionary:COBOL
From Wikibooks, the open-content textbooks collection
Contents |
[edit] COBOL
Here is the wikipedia page.
One of COBOL's great strengths is fixed-point arithmetic. You don't want to wind up eating CROE adding up thousands of purchase amounts - business needs it to be accurate to the penny.
Also, the vast majority of useful COBOL applications came thru the Y2K crisis just fine.
[edit] Type
COBOL is a full procedural language.
[edit] Execution Entry Point
Unlike C, which requires a function named "main", the COBOL execution entry point is the first statement in the PROCEDURE DIVISION. The COBOL language standard requires a paragraph name or section name (in other words, a label), but some compilers do not enforce this strictly. But before which we have a structured way in which I/O files, variables, etc. are declared. First we have the IDENTIFICATION DIVISION. Then we have the PROGRAM ID. Usually the program ID is the same as the program name. Then we have the FILE SECTION. where we declare all the input and output files which are going to be used in the program. It is compulsory we declare them here. Then we have the DATA DIVISION. Here all the variables which are going to be used in the program are declared. We shall discuss the way they are declared later. Then comes the PROCEDURE DIVISION.
[edit] General Syntax
It would be very difficult to give an overview of COBOL's general syntax, since the language has over 500 reserved words, which is too large for some versions of LEX and YACC. Here are some sample assignment statements:
MOVE A TO B. COMPUTE GROSS-PAY = HOURS-WORKED * HOURLY-RATE SET MY-INDEX TO 1 SET ADDRESS OF MY-LINKAGE-SECTION-ITEM TO MY-POINTER READ TRANSACTION-FILE INTO TRANSACTION-RECORD-WS
While many sites still use all uppercase characters, modern COBOL compilers are case-insensitive. The following two statements are absolutely equivalent:
MOVE MY-NUMBER TO YOUR-NUMBER. Move my-number to Your-Number.
Character case is respected for quoted literals (character strings).
[edit] Comments
Normally the only way to specifiy a comment is by putting an asterisk in the indicator-area (column 7). The entire line is a comment. Block comments must have * in column 7 of every line. There is a 2002 standard that allows part of a line to be a comment:
MOVE A TO B. *> THIS IS A COMMENT ON THE SAME LINE AS A STATEMENT
[edit] Variable Declarations
There are a few variables of a type know as special registers that do not need to be defined, but generally all variables must be defined in COBOL, unlike interpreted BASIC.
COBOL has a number of data types, but the most common are numeric (with specifiers for internal format), numeric edited data, alphanumeric data, and records (similar to struct in C):
01 TRANSACTION-RECORD. 05 RECORD-NUMBER PIC S9(7) COMP-3 VALUE ZERO. 05 RECORD-DESCRIPTION PIC X(30) VALUE SPACES. 05 EDITED-AMOUNT PIC $$$$,$$$,$$$.99-. 05 FILLER PIC X(60) VALUE SPACES.
Your simple example, declaring an integer, might look something like this:
77 I pic s9(4) usage is binary.
COBOL also supports redefinition, which is somewhat similar to union in C
[edit] Method Declaration/Implementation
Object-oriented extensions are available to COBOL, but the vast majority of COBOL programmers don't know them, and see no need for them.
However if a COBOL programer wants to create a class it is quite easy. For example here is an HelloWorld class with a InstanceMethod!
class-id. HelloWorld as "HelloWorld".
environment division.
configuration section.
repository.
object.
data division.
working-storage section.
method-id. "InstanceMethod".
local-storage section.
linkage section.
procedure division.
display "Hello World"
exit method.
end method "InstanceMethod".
end object.
end class HelloWorld
If the COBOL programmer wants to interact with external classes such as the .NET Framework it can do too. For example here is a example of obtaining the current date/time from .NET.
repository.
class sys-datetime as "System.DateTime"
.
01 now sys-datetime.
procedure division.
set now to sys-datetime::"Now"
display now
[edit] Scope
All data contained working-storage section of the COBOL program is global. Some data items may be defined as external, and visible to other programs in a load module. The data items in a COBOL subprogram are global to the subprogram, but local since the caller cannot see them. Typically, data is "passed" from a COBOL main program to a subprogram by reference. There are alternatives of "by content" or "by value", but they are typically only used to call subprograms written in C or other languages that do not normally use call by reference.
Some vendors provide extensions, such as thread-local-storage section and local-storage section. These extensions allow data to scoped as either thread-local or temporary to the program.
[edit] Conditional Statements
There are a lot of them. Here are some examples:
If my-number is numeric continue else display 'data field "my-number" is not numeric' end-if
evaluate record-type when 'a' perform process-record-type-a when 'b' perform process-record-type-b when 'c' continue when 'd' when 'e' perform process-record-type-d-or-e when other perform process-invalid-record-type end-evaluate
READ TRANSACTION-FILE INTO MY-TRAN-REC-WS AT END SET ALL-RECORDS-PROCESSED TO TRUE NOT AT END ADD +1 TO TRAN-REC-COUNT PERFORM PROCESS-1-TRANSACTION END-READ
[edit] Looping Statements
There are several of those too:
Perform 6 times add +1 to loop-count end-perform
Perform process-1-billing-record until all-records-processed
Perform clear-1-table-entry varying tbl-index from +1 by +1 until tbl-index is greater than max-table-entries
Search table-entry varying tbl-index at end set entry-not-found to true when table-key equal customer-id move tbl-customer-name to print-customer-name end-search
[edit] Output Statements
Here are several examples.
DISPLAY "Hello, World!" display 'hello, world!' with no advancing
Write billing-record from transaction-record-ws
For key-sequenced files, there is support for Start, Read, Write, Rewrite, and Delete.
[edit] Containers
Not applicable to COBOL.
[edit] Algorithms
Recursion is not generally supported, although it can be simulated by iteration.
[edit] Garbage collection
Memory allocated dynamically via system APIs such as malloc should be free'ed with the appriopiate call to "free".
COBOL programs that uses CLASS'es may need to invoke the "finalize" method to release the object. For example:
invoke myObject "finalize" returning myObject
If the COBOL application is executing under a VM such as a Java VMs or Microsoft's CLR standard garbage collection rules apply.
Otherwise..
COBOL programmers do not normally know or care what garbage collection is. They rarely have to allocate memory or clean it up afterwards.
[edit] Physical Structure
A COBOL source file may have (but need not have) copybooks, somewhat similar to .h files in C. The compiled program generally makes use of a COBOL runtime library of service routines that are dynamically loaded. The COBOL source file itself consists of four divisions: IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION, and PROCEDURE DIVISION.
[edit] Tips
<Please include tips that make it easier to switch to this language from another language.>
[edit] Web References
- COBOL FAQ
- COBOL User Groups (COBUG)
- cobol.org
- cob-rte
- svalgard
- swbell page
- dissert
- csis.ul.ie page
- infogoal page
- COBOL Blog
[edit] Books and Articles
<List additional books and articles that may be helpful. Please include for what level reader the references are appropriate. (beginner/intermediate/advanced)>

