Skip to content

Add Article for Hello World in COBOL #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions _data/authors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ stuin:
web: http://stuintech.com
bio:

sudhanshu_dubey:
name: Sudhanshu Dubey
email: [email protected]
web: https://hacksd.wordpress.com/
bio: |
An aspiring computer science engineer with experience of Linux,
Android App Development and General Programming.
Also enthusiastically learning Mainframe technology.

the_renegade_coder:
name: Jeremy Grifski
email: [email protected]
Expand Down
69 changes: 69 additions & 0 deletions projects/hello-world/_posts/2021-10-08-cobol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: Hello World in COBOL
layout: default
last-modified: 2021-10-08
tags: [COBOL, hello-world]
authors:
- sudhanshu_dubey
---

In this article, we will see how Hello World can be written in one of the oldest and still relevant programming language, COBOL.

## How to Implement the Solution

From it's very nature, COBOL is a very readable language.
But even though it's readable, it follows very strict rules.

```cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
DISPLAY "Hello, World!".
STOP RUN.
```

The structure of a COBOL program follows like this:

- At the highest level are "Divisions".
- "Divisions" consist of "Sections".
- "Sections" consist of "Paragraphs".
- "Paragraphs" consist of "Sentences".
- "Sentences" consist of "Statements".
- "Statements" consist of "Characters".

We know that Python as language cares a lot about indentation but COBOL is even more particular about it.
A COBOL program is divided into columns. The Divisions must start from column 8 (Area A).
Paragraphs and Sentences should start from column 12 (Area B) which you can see is 1 tab or 4 spaces away from Area A.
This indentation is also a reason why COBOL is so readable.
Now that we have cleared some basics, let's start reading the Hello World program.

The `IDENTIFICATION DIVISION` is one of the 4 Divisions that we have and it is mandatory.
As the name suggests, it consists of information that identifies the program like the author name, date of creation, etc.
One mandatory information for program identification is, you guessed it, the `PROGRAM-ID` statement.
Here we are declaring the `PROGRAM-ID` as `HELLO-WORLD`.
As a side note, we should ideally keep the `PROGRAM-ID` same as the file name and only 8 characters long. But since we will be running this program on a Linux environment, it's fine.

Next we have the other mandatory Division, `PROCEDURE DIVISION`.
This contains all the Paragraphs and Sentences that do the actual work.
And here, the Sentence doing our work is `DISPLAY "Hello, World!"`.
`DISPLAY` is our Statement here and it does what it says it does.
Then we have another Statement ,which is a Sentence in itself, `STOP RUN`.
And well, it stops the run and returns the control to either the calling program or the OS.
Also you should end any part of the program with a period. It might be optional at some places but it is recommended.
That's it! Apart from the little nuances of indentation and program structure, it's a very readable program.

## How to run the Solution

To run the solution we will need [a COBOL compiler][1] installed and of the course [the actual code file][2].
Finally we need to run these commands in order:

```console
cobc -x hello-world.cbl
$ ./hello-world
```
The commands first compile the source code into an executable and then execute it.
Alternatively, you might want to use an [online COBOL compiler][3]

[1]: https://gnucobol.sourceforge.io/
[2]: https://github.com/TheRenegadeCoder/sample-programs/blob/main/archive/c/cobol/hello-world.cbl
[3]: https://www.jdoodle.com/execute-cobol-online/