An Introduction to programming with COBOL

An Introduction to programming with COBOL

The Problem(s)

As businesses began relying more heavily on computers for data processing—such as payroll, inventory, banking, and more—in the 1950s, it became clear that change was needed. At the time, each computer had its own proprietary programming language, which was often hardware-specific. As a result, sharing programs between different systems was incredibly difficult. It's also important to note that programming languages at the time were highly mathematical, making them inaccessible to non-technical users.

The Solution

Realizing how much money was being wasted on porting programs to different systems, the U.S. Department of Defense (DoD) began advocating for a standardized programming language that could be easily used across different systems. With this goal in mind, a group of experts gathered at the Conference on Data Systems Languages (CODASYL) in 1959. Among them was Grace Hopper, a pioneering computer scientist who was a driving force behind the development of the first compiler, playing a pivotal role in its creation alongside other contributors.

Out of this conference came the COmmon Business-Oriented Language (COBOL), a revolutionary language designed to meet the needs of business data processing. Among the features that made COBOL groundbreaking was its English-like syntax, which allowed non-technical people to read, understand, and even write programs. This was akin to how modern drag-and-drop website builders have democratized—or disrupted, depending on your perspective—web development today.

The Structure of a COBOL program

Now that we've learned a bit about COBOL and some of the problems it solved, we'll take a look at the structure of a COBOL program.

Columns in COBOL

Since COBOL dates back to the era of punched cards, it’s no surprise that its code is organized into specific areas, each made up of one or more columns with a unique purpose.

COBOL Column Specification
The COBOL Column Specification

  • Columns 1-6 - Known as the Sequence Number Area, in modern implementations of COBOL, these columns are often ignored.
  • Column 7 - Known as the Indicator Area, in modern COBOL programming, this column is primarily used for comments (with *) or left blank for regular code lines. Other uses of Column 7 (such as /, -, or D) are less common in contemporary COBOL development.
  • Columns 8-11: Area A (Margin A) - Used for division headers, section headers, paragraph names, and level indicators.
  • Columns 12-72: Area B (Margin B) - Contains the actual COBOL statements, data definitions, and procedural code.
  • Columns 73-80 - Known as the Identification Area, in modern COBOL programming, this area is rarely used.

The Structure of a COBOL program

Because COBOL is English-like, perhaps it comes as no surprise that a COBOL program is structured much like a document, with divisions, sections, paragraphs, sentences, and statements. Let's explore these a little more.

  • Divisions: Major segments of a COBOL program that categorize different aspects like program identification, environment setup, data definitions, and procedural logic.
  • Sections: Group related functionality within divisions.
  • Paragraphs: Small blocks of code within sections for specific tasks.
  • Sentences: Groups of statements that perform operations within paragraphs.
  • Statements: Individual commands or instructions within sentences.

The Four Divisions of a COBOL program

Every COBOL program consists of four divisions; however, only two are required to write a basic COBOL program: the IDENTIFICATION DIVISION and the PROCEDURE DIVISION.

Everything COBOL program has four divisions
The Four Divisions of a COBOL Program

IDENTIFICATION DIVISION

It is mandatory for every COBOL program. If your program doesn't start with these "magical words", it's not a valid COBOL program. It provides a place to store some valuable metadata, such as the program name, author, and other details. Within this division the the PROGRAM-ID paragraph must appear directly after the IDENTIFICATION DIVISION and is the only required paragraph. Other optional paragraphs include: AUTHOR, DATE-WRITTEN, DATE-COMPILED, and SECURITY.

ENVIRONMENT DIVISION

This division must follow the IDENTIFICATION DIVISION and describes the system the program will run on, including:

  • The computer hardware and operating system.
  • Any I/O devices (e.g., printers, disk drives, etc.) the program will use.

The ENVIRONMENT DIVISION is divided into two sections:

  • CONFIGURATION SECTION - Describes the system environment.
  • INPUT-OUTPUT SECTION - Defines files and I/O devices, controlling how data is read from or written to these resources.

DATA DIVISION

This is where your COBOL program defines and stores all the data it will use. It’s like the program’s "storage room" for variables, records, and files.

The DATA DIVISION is divided into the following sections:

  1. FILE SECTION - Defines the structure of files used for input/output.
  2. WORKING-STORAGE SECTION - Stores temporary data and variables used during program execution.
  3. LINKAGE SECTION - Defines data shared between programs (used in subprograms).

PROCEDURE DIVISION

The PROCEDURE DIVISION is mandatory for any COBOL program since it's where the program is brought to life. This is where you define the logic, operations, and all the processing that will occur happens.

A Basic COBOL Program

If you've done any programming before, this program will not shock you. It's the basic 'Hello World' program.

We start by declaring our IDENTIFICATION DIVISION followed by a PROGRAM-ID, which is the only required paragraph in this division.

Next, we declare the PROCEDURE DIVISION, which is where the action happens. Now, because we want to see something, we need to tell COBOL to write something to the output device; these days, it's typically a terminal screen, but back in the day, it was often a printer.

Finally, we instruct COBOL that immediately after it displays Hello World, we want to stop everything, and that's done with the STOP RUN statement.

A simple Hello World program in COBOL
A Hello World Program in COBOL

Summary

In this article, I've described the history and reasoning behind COBOL, the structure of COBOL, and shown you how to write a basic program using COBOL.

As I'm still learning COBOL, if you notice anything that needs correction, please let me know, and I'll do my best to update this article.


As I always wonder why people "code on Windows/Ubuntu/..." and then use a mainframe-centric extension that is best when ... actually connecting to a mainframe instead of a solution for distributed systems... Have you checked out the superbol extension from OCamlPro as well? It also features a COBOL source level debugging option that can come in handy when you get "deep into COBOL programs" (or, you know, just wondering why your "Hello World Extended" does not operate as you wish). If you run on Windows you're likely interesting in their "All-In-One" binary Package offering GnuCOBOL 64bit along with GCSORT and GixSQL (also supported in their vscode extension). https://superbol.eu/en/

good understanding... ready for the ride

See Manas Kumar he has the best COBOL practices and sample code ... I have been COBOL since 1976

My father focused on mainframes and COBOL. It's seriously one of the most important languages businesses still rely on. It should be taught in schools more cause most CS students really don't know how in demand this skill is.

To view or add a comment, sign in

More articles by Stephen Johnston

  • PowerShell 101: Data Types

    A data type allows us to classify the value a variable holds. This covers some of the most common data types used.

  • PowerShell 101: The Basics

    PowerShell is a scripting language developed by Microsoft. It provides a way to automate repetitive or tedious tasks.

  • Ingredients of delightful testing

    Testing our application is critical and shouldn't feel like you've been kicked by a mule. It gives us; the developers…

  • Add custom map controls with HERE in Javascript

    Recently, I found myself needing to add custom map controls to a Proof of Concept I was working on using the HERE JS…

    1 Comment
  • Placing Markers on a map in React with HERE

    In my previous article, we looked at creating a simple HERE map within a react application. This map was an example of…

  • Getting started with HERE Maps using React

    We will look at how to get started with HERE by creating a simple map using create-react-app. Create React App is used…

    1 Comment

Others also viewed

Explore content categories