Introduction to COBOL
COBOL (Common Business-Oriented Language) is a high-level programming language designed specifically for business applications. Created in 1959, it remains vital in financial institutions, government systems, and enterprise applications handling massive transaction processing. Despite its age, COBOL powers approximately 70% of global business transactions, including ATM withdrawals, credit card processing, and insurance claims. Its English-like syntax, data processing capabilities, and exceptional reliability make it irreplaceable for many mission-critical systems.
COBOL Program Structure
Every COBOL program follows a standardized divisional structure:
Division | Purpose | Required? |
---|
IDENTIFICATION DIVISION | Identifies the program and provides documentation | Yes |
ENVIRONMENT DIVISION | Specifies the computing environment | No |
DATA DIVISION | Defines data items and structures | No |
PROCEDURE DIVISION | Contains executable statements | Yes |
Sample Basic Program Structure
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
AUTHOR. PROGRAMMER-NAME.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. YOUR-COMPUTER.
OBJECT-COMPUTER. YOUR-COMPUTER.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-MESSAGE PIC X(20) VALUE 'HELLO, WORLD!'.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
DISPLAY WS-MESSAGE.
STOP RUN.
COBOL Coding Rules
- COBOL code is traditionally written in uppercase letters
- Each line is divided into specific areas:
- Columns 1-6: Sequence number area (optional)
- Column 7: Indicator area (* for comments, / for page eject, – for continuation)
- Columns 8-11: Area A (for divisions, sections, paragraphs)
- Columns 12-72: Area B (for statements and clauses)
- Columns 73-80: Identification area (optional)
- Statements end with a period (.)
- Indentation is not required but improves readability
Data Types and Variables
PICTURE (PIC) Clause Symbols
Symbol | Description | Example |
---|
9 | Numeric digit | PIC 9(3) for 3-digit number (000-999) |
X | Alphanumeric character | PIC X(10) for 10-character string |
A | Alphabetic character | PIC A(5) for 5-letter string |
V | Implied decimal point | PIC 9(3)V99 for number with 2 decimal places |
S | Sign indicator | PIC S9(3) for signed number |
Z | Leading zero suppression | PIC Z(3) for suppressing leading zeros |
$ | Currency symbol | PIC $(3) for dollar sign |
, | Comma insertion | PIC 9(3),99 for number like 123,45 |
. | Actual decimal point | PIC 9(3).99 for number like 123.45 |
Data Level Numbers
Level | Purpose |
---|
01 | Record description |
02-49 | Group items and elementary items |
66 | RENAMES clause items |
77 | Independent elementary items |
88 | Condition names (values that make conditions true) |
Data Variable Declaration Examples
WORKING-STORAGE SECTION.
* Simple variables
01 WS-NUM PIC 9(5) VALUE 12345.
01 WS-NAME PIC X(20) VALUE 'JOHN DOE'.
01 WS-AMOUNT PIC 9(5)V99 VALUE 123.45.
01 WS-SIGNED-NUM PIC S9(5) VALUE -12345.
* Group items
01 WS-EMPLOYEE.
05 WS-EMP-ID PIC 9(5).
05 WS-EMP-NAME PIC X(20).
05 WS-EMP-DEPT.
10 WS-DEPT-ID PIC 9(3).
10 WS-DEPT-NAME PIC X(15).
* Condition names (88 levels)
01 WS-STATUS PIC 9 VALUE 0.
88 STATUS-ACTIVE VALUE 1.
88 STATUS-INACTIVE VALUE 0.
Basic Operations and Statements
MOVE Statement (Assignment)
* Simple move
MOVE 123 TO WS-NUM.
MOVE 'HELLO' TO WS-NAME.
* Multiple destinations
MOVE 0 TO WS-NUM1, WS-NUM2, WS-NUM3.
* Move corresponding fields
MOVE CORRESPONDING WS-EMPLOYEE TO WS-NEW-EMPLOYEE.
Arithmetic Operations
* ADD statement
ADD 5 TO WS-NUM.
ADD WS-NUM1, WS-NUM2 TO WS-RESULT.
ADD WS-NUM1, WS-NUM2, WS-NUM3 GIVING WS-RESULT.
* SUBTRACT statement
SUBTRACT 5 FROM WS-NUM.
SUBTRACT WS-NUM1, WS-NUM2 FROM WS-NUM3 GIVING WS-RESULT.
* MULTIPLY statement
MULTIPLY WS-NUM1 BY WS-NUM2.
MULTIPLY WS-NUM1 BY WS-NUM2 GIVING WS-RESULT.
* DIVIDE statement
DIVIDE WS-NUM1 BY WS-NUM2.
DIVIDE WS-NUM1 BY WS-NUM2 GIVING WS-RESULT REMAINDER WS-REMAINDER.
* COMPUTE statement (more complex expressions)
COMPUTE WS-RESULT = WS-NUM1 + WS-NUM2 * WS-NUM3.
COMPUTE WS-DISCOUNT = WS-PRICE * 0.1.
Conditional Statements
* Simple IF statement
IF WS-NUM > 10
DISPLAY "NUMBER IS GREATER THAN 10"
END-IF.
* IF-ELSE statement
IF WS-NUM > 10
DISPLAY "NUMBER IS GREATER THAN 10"
ELSE
DISPLAY "NUMBER IS NOT GREATER THAN 10"
END-IF.
* Nested IF statements
IF WS-NUM > 0
IF WS-NUM < 100
DISPLAY "NUMBER IS BETWEEN 0 AND 100"
END-IF
END-IF.
* IF with multiple conditions
IF WS-NUM > 10 AND WS-NUM < 20
DISPLAY "NUMBER IS BETWEEN 10 AND 20"
END-IF.
IF WS-NUM = 1 OR WS-NUM = 2 OR WS-NUM = 3
DISPLAY "NUMBER IS 1, 2, OR 3"
END-IF.
* Using 88 levels
IF STATUS-ACTIVE
DISPLAY "STATUS IS ACTIVE"
END-IF.
* EVALUATE statement (similar to switch/case)
EVALUATE WS-GRADE
WHEN 'A'
DISPLAY "EXCELLENT"
WHEN 'B'
DISPLAY "GOOD"
WHEN 'C'
DISPLAY "AVERAGE"
WHEN OTHER
DISPLAY "BELOW AVERAGE"
END-EVALUATE.
* EVALUATE with multiple conditions
EVALUATE TRUE
WHEN WS-NUM > 90
DISPLAY "GRADE A"
WHEN WS-NUM > 80
DISPLAY "GRADE B"
WHEN WS-NUM > 70
DISPLAY "GRADE C"
WHEN OTHER
DISPLAY "BELOW AVERAGE"
END-EVALUATE.
Loops and Iteration
PERFORM Statements
* Basic PERFORM (execute a paragraph)
PERFORM DISPLAY-MESSAGE.
* PERFORM with TIMES (fixed loop)
PERFORM DISPLAY-MESSAGE 5 TIMES.
* PERFORM with UNTIL (conditional loop)
PERFORM PROCESS-RECORD UNTIL END-OF-FILE.
* PERFORM with VARYING (counter loop)
PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10
DISPLAY WS-COUNTER
END-PERFORM.
* Nested PERFORM VARYING
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5
PERFORM VARYING WS-J FROM 1 BY 1 UNTIL WS-J > 3
DISPLAY WS-I " x " WS-J " = " WS-I * WS-J
END-PERFORM
END-PERFORM.
* Inline PERFORM
PERFORM
DISPLAY "HELLO"
DISPLAY "WORLD"
END-PERFORM.
File Handling Basics
File Descriptions in DATA DIVISION
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPLOYEE-FILE ASSIGN TO 'EMPLOYEE.DAT'
ORGANIZATION IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD EMPLOYEE-FILE.
01 EMPLOYEE-RECORD.
05 EMP-ID PIC 9(5).
05 EMP-NAME PIC X(20).
05 EMP-DEPT PIC X(10).
Basic File Operations
* Opening a file
OPEN INPUT EMPLOYEE-FILE.
OPEN OUTPUT REPORT-FILE.
OPEN I-O UPDATE-FILE.
* Reading from a file
READ EMPLOYEE-FILE
AT END SET END-OF-FILE TO TRUE
END-READ.
* Writing to a file
WRITE REPORT-RECORD.
* Closing a file
CLOSE EMPLOYEE-FILE.
CLOSE REPORT-FILE.
Sequential File Processing Pattern
PROCEDURE DIVISION.
MAIN-PROCEDURE.
OPEN INPUT EMPLOYEE-FILE
OPEN OUTPUT REPORT-FILE
READ EMPLOYEE-FILE
AT END SET END-OF-FILE TO TRUE
END-READ
PERFORM UNTIL END-OF-FILE
PERFORM PROCESS-RECORD
READ EMPLOYEE-FILE
AT END SET END-OF-FILE TO TRUE
END-READ
END-PERFORM
CLOSE EMPLOYEE-FILE
CLOSE REPORT-FILE
STOP RUN.
PROCESS-RECORD.
* Process the current record
MOVE EMP-ID TO RPT-EMP-ID
MOVE EMP-NAME TO RPT-EMP-NAME
WRITE REPORT-RECORD.
String Handling
String Operations
* String concatenation
STRING 'HELLO ' 'WORLD' DELIMITED BY SIZE
INTO WS-MESSAGE
END-STRING.
* String with delimiter
STRING FIRST-NAME DELIMITED BY SPACE
' ' DELIMITED BY SIZE
LAST-NAME DELIMITED BY SPACE
INTO FULL-NAME
END-STRING.
* UNSTRING (splitting strings)
UNSTRING WS-FULL-NAME DELIMITED BY SPACE
INTO WS-FIRST-NAME
WS-LAST-NAME
END-UNSTRING.
* INSPECT (character manipulation)
INSPECT WS-STRING REPLACING ALL 'a' BY 'A'.
INSPECT WS-STRING TALLYING WS-COUNT FOR ALL 'A'.
Common COBOL Intrinsic Functions
* Numeric functions
COMPUTE WS-RESULT = FUNCTION MAX(WS-NUM1, WS-NUM2, WS-NUM3)
COMPUTE WS-RESULT = FUNCTION MIN(WS-NUM1, WS-NUM2, WS-NUM3)
COMPUTE WS-RESULT = FUNCTION INTEGER(WS-NUM1)
COMPUTE WS-RESULT = FUNCTION INTEGER-PART(WS-NUM1)
COMPUTE WS-RESULT = FUNCTION REM(WS-NUM1, WS-NUM2)
COMPUTE WS-RESULT = FUNCTION SUM(WS-NUM1, WS-NUM2, WS-NUM3)
COMPUTE WS-RESULT = FUNCTION SQRT(WS-NUM1)
* String functions
MOVE FUNCTION UPPER-CASE(WS-NAME) TO WS-NAME-UPPER
MOVE FUNCTION LOWER-CASE(WS-NAME) TO WS-NAME-LOWER
MOVE FUNCTION REVERSE(WS-STRING) TO WS-STRING-REV
MOVE FUNCTION LENGTH(WS-STRING) TO WS-STRING-LEN
MOVE FUNCTION TRIM(WS-STRING) TO WS-STRING-TRIMMED
* Date and time functions
MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE
MOVE FUNCTION YEAR-TO-YYYY(WS-YEAR) TO WS-YEAR-YYYY
Debugging Techniques
Common DISPLAY Methods
* Basic display
DISPLAY "DEBUG: ENTERING PROCESS-CUSTOMER".
* Display variables
DISPLAY "CUSTOMER ID: " CUST-ID.
DISPLAY "AMOUNT: " AMOUNT.
* Display with line information
DISPLAY "ERROR AT LINE 100: " ERROR-MESSAGE.
* Display program state
DISPLAY "PROCESSING RECORD " WS-RECORD-COUNT " OF " WS-TOTAL-RECORDS.
Compiler Directives for Debugging
* Some implementations use >>D for debugging lines
>>D DISPLAY "DEBUG: VALUE OF X = " X.
* Compile with appropriate option to enable/disable debug lines
* (e.g., -debug or -fdebug-lines in GnuCOBOL)
Common COBOL Errors and Solutions
Error | Possible Causes | Solutions |
---|
Missing period | Forgetting to end statements with a period | Add missing periods after statements |
Misplaced sections | Sections in wrong order | Follow proper division and section order |
Unmatched parentheses | Incomplete expression | Ensure all parentheses are properly matched |
Level number errors | Improper hierarchy | Check level numbers for proper nesting |
Undefined paragraph | Calling non-existent paragraph | Ensure all referenced paragraphs are defined |
Numeric overflow | Values exceeding defined size | Increase the size of numeric fields |
Division by zero | Zero divisor in calculations | Add validation before division operations |
File status errors | Various I/O issues | Check file status code and handle accordingly |
Best Practices in COBOL Programming
Naming Conventions
- Use consistent prefixes (e.g., WS- for Working Storage)
- Use meaningful names (EMPLOYEE-RECORD instead of ER)
- Use hyphens for readability (CUSTOMER-NAME vs CUSTOMERNAME)
Structural Guidelines
- Limit paragraphs to a single function
- Use structured programming techniques
- Keep nested IF statements to a minimum
- Comment liberally, especially for complex logic
Performance Optimization
- Minimize I/O operations
- Use COPY statements for common code
- Use SEARCH ALL for sorted tables (binary search)
- Compile with optimization flags when available
Maintainability Tips
- Document program purpose and logic
- Use 88-level items for meaningful conditions
- Structure code with clear sections
- Avoid ALTER and GOTO statements
- Use END-IF, END-PERFORM instead of periods for scope termination
Resources for Further Learning
Books
- “Murach’s Mainframe COBOL” by Mike Murach
- “Beginning COBOL for Programmers” by Michael Coughlan
- “The COBOL 85 Example Book” by E. Thane Nilsen
Online Resources
- IBM COBOL Language Reference
- GnuCOBOL Guides and Documentation
- Open Mainframe Project (COBOL Programming Course)
- COBOL Center of Excellence (tutorials and examples)
Tools and Compilers
- GnuCOBOL (Free COBOL compiler)
- IBM Enterprise COBOL
- Micro Focus Visual COBOL
- COBOL-IT (Enterprise-ready COBOL compiler)
This cheatsheet provides a solid foundation for COBOL programming, covering essential syntax, structures, and techniques for building and maintaining business applications effectively.