In the fast-paced realm of technology, COBOL, or COmmon Business-Oriented Language, emerges as a timeless cornerstone in the world of programming. Developed in the late 1950s with a clear focus on business applications, COBOL remains relevant today due to its readability, adaptability, and crucial role in maintaining the backbone of various industries.
Despite its age, COBOL’s simplicity and efficiency in handling business logic, data processing, and file management make it an enduring force, ensuring the smooth operation of critical systems in finance, insurance, and more. So here is a tutorial to help introduce those of you who have interest to the language.
Getting started
Just like with any compilled language, first thing to do is choose a compiler and code editor. I recommend using VS Code + a popular extension for COBOL programming(listed below) and GnuCOBOL (formerly OpenCOBOL), a widely used COBOL compiler for Linux systems(also works on Windows and MacOS). It is open-source and supports modern COBOL features.
Setting up VS Code for COBOL Development:
- Install the “COBOL Language Support” extension directly from the VS Code Marketplace.
- Configure the extension to recognize the location of your COBOL compiler and other build tools.
- Utilize the integrated terminal within VS Code to compile and run COBOL programs directly from the editor.
A Language for Business:
COBOL’s strength lies in its alignment with the business domain. Its syntax is intentionally designed to be readable and comprehensible, making it accessible not only to seasoned programmers but also to domain experts and business analysts. Let’s take a simple “Hello, World!” example:
IDENTIFICATION DIVISION.
PROGRAM-ID. HelloWorld.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MESSAGE PIC X(50) VALUE "Hello, World!".
PROCEDURE DIVISION.
DISPLAY MESSAGE.
STOP RUN.
In this example, DISPLAY
is a COBOL verb used to output text to the console. The STOP RUN
statement signifies the end of the program execution.
Data Division
The Data Division in COBOL is where the structure of data is defined. Variables and records are explicitly declared, and their types are specified using a Picture clause. Consider this data definition snippet:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMPLOYEE-NAME PIC X(30).
01 EMPLOYEE-ID PIC 9(5).
01 EMPLOYEE-SALARY PIC 9(7)V99.
Here, EMPLOYEE-NAME
is a character string, EMPLOYEE-ID
is a numeric identifier, and EMPLOYEE-SALARY
is a numeric field with two decimal places.
Procedure Division
The Procedure Division contains the actual instructions for the program. COBOL’s English-like syntax enhances readability. Let’s manipulate some data:
PROCEDURE DIVISION.
MOVE "John Doe" TO EMPLOYEE-NAME.
MOVE 12345 TO EMPLOYEE-ID.
ADD 5000.75 TO EMPLOYEE-SALARY.
DISPLAY "Employee: " EMPLOYEE-NAME
" ID: " EMPLOYEE-ID
" Salary: " EMPLOYEE-SALARY.
STOP RUN.
In this snippet, we assign values to variables and perform arithmetic operations, demonstrating the simplicity of COBOL syntax.
File Handling
COBOL excels in handling files – a critical aspect for business applications. Verbs like OPEN, READ, WRITE, and CLOSE facilitate efficient file operations. Consider a scenario with an employee file:
DATA DIVISION.
FILE SECTION.
FD EMPLOYEE-FILE.
01 EMPLOYEE-RECORD.
05 EMPLOYEE-NAME PIC X(30).
05 EMPLOYEE-ID PIC 9(5).
05 EMPLOYEE-SALARY PIC 9(7)V99.
PROCEDURE DIVISION.
OPEN INPUT EMPLOYEE-FILE.
READ EMPLOYEE-FILE INTO EMPLOYEE-RECORD.
DISPLAY "Employee: " EMPLOYEE-NAME
" ID: " EMPLOYEE-ID
" Salary: " EMPLOYEE-SALARY.
CLOSE EMPLOYEE-FILE.
STOP RUN.
This snippet demonstrates how COBOL can read data from an employee file and display it.
Conditional Statements
COBOL supports various conditional statements, making it adaptable to different scenarios. Let’s explore a simple salary condition:
PROCEDURE DIVISION.
IF EMPLOYEE-SALARY > 10000
DISPLAY "High Salary Bracket".
ELSE
DISPLAY "Standard Salary Bracket".
STOP RUN.
Depending on the employee’s salary, COBOL responds with a relevant message.
Paragraphs and Sections
COBOL’s organizational structure involves paragraphs and sections for better readability and maintainability. The PERFORM
statement executes a specific paragraph or section. Consider a salary calculation example:
PROCEDURE DIVISION.
PERFORM CALCULATE-SALARY.
STOP RUN.
CALCULATE-SALARY.
ADD 5000 TO EMPLOYEE-SALARY.
DISPLAY "New Salary: " EMPLOYEE-SALARY.
Here, the CALCULATE-SALARY
paragraph is invoked, performing a salary increase.
Subprograms and Linkage Section
COBOL allows the creation of subprograms, enhancing modularity and code reuse. The LINKAGE SECTION
facilitates communication between programs. Consider a simple example:
IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMPLOYEE-SALARY PIC 9(7)V99.
LINKAGE SECTION.
01 BONUS AMOUNT PIC 9(5)V99.
PROCEDURE DIVISION USING BONUS.
ADD BONUS TO EMPLOYEE-SALARY.
DISPLAY "New Salary with Bonus: " EMPLOYEE-SALARY.
STOP RUN.
In this example, MainProgram
calls another program, passing a bonus amount through the LINKAGE SECTION
.
Error Handling with EXCEPTIONS
COBOL allows for the handling of exceptional conditions using the EXCEPTION/END-EXCEPTION construct:
PROCEDURE DIVISION.
PERFORM CALCULATE-SALARY
ON EXCEPTION
DISPLAY "An error occurred in salary calculation!".
STOP RUN.
CALCULATE-SALARY.
DIVIDE TOTAL-SALARY BY WORKING-DAYS GIVING DAILY-SALARY
ON SIZE ERROR
DISPLAY "Error: Division by zero!".
Here, ON SIZE ERROR is triggered if an attempt is made to divide by zero.
In conclusion, COBOL stands as a resilient force in the programming world, playing a crucial role in legacy systems that power essential functions across various industries. Its simplicity and readability contribute to its enduring significance, being part of important systems world wide. This is a language that will continue to exist for a long time, and pay hefty wages to those who are proficient in it.