COBOL (Common Business-Oriented Language) is one of the oldest programming languages still in use today. Initially developed in the late 1950s, COBOL has been the backbone of many business applications, especially in finance, insurance, and government sectors. Despite its age, COBOL remains relevant, powering critical systems around the world. In this article, we’ll introduce you to the basics of COBOL programming and walk you through a simple example.
What is COBOL?
COBOL was designed for business computing, with a focus on readability and simplicity. Its syntax is English-like, making it easier to understand for non-programmers. COBOL programs are typically used for processing large volumes of data in batch processing systems.
Setting Up the Environment
To get started with COBOL programming, you’ll need a COBOL compiler. There are several open-source and commercial compilers available. For this tutorial, we’ll use the free and open-source GNU COBOL compiler, also known as GnuCOBOL. You can install it on Linux, macOS, or Windows.
Installing GnuCOBOL on Linux (Ubuntu)
Open a terminal and run the following commands:
sudo apt-get update
sudo apt-get install open-cobol
You can also download GnuCOBOL from the official website: GnuCOBOL Downloads.
Installing GnuCOBOL on macOS
If you have Homebrew installed, you can install GnuCOBOL with:
brew install gnu-cobol
Installing GnuCOBOL on Windows
For Windows, you can use Cygwin, which provides a Unix-like environment on Windows. Follow the instructions on the Cygwin website to install Cygwin with the GnuCOBOL package.
Your First COBOL Program
Let’s write a simple “Hello, World!” program in COBOL.
Step 1: Create a New File
Open your favorite text editor and create a new file called hello.cbl
.
Step 2: Write the COBOL Code
Copy and paste the following code into hello.cbl
:
IDENTIFICATION DIVISION.
PROGRAM-ID. HelloWorld.
PROCEDURE DIVISION.
DISPLAY "Hello, World!".
STOP RUN.
Step 3: Save the File
Save the file hello.cbl
in your preferred directory.
Step 4: Compile the Program
Open a terminal or command prompt, navigate to the directory where you saved hello.cbl
, and compile the program with the following command:
cobc -x -o hello hello.cbl
This command tells the COBOL compiler (cobc
) to create an executable (-x
) named hello
from the source file hello.cbl
. The -o
option specifies the output file name.
Step 5: Run the Program
After successfully compiling, you can run the hello
program:
- Linux/macOS:
./hello
- Windows:
hello.exe
You should see the output:
Hello, World!
Congratulations! You’ve just written and executed your first COBOL program.
Understanding the Code
Let’s break down the simple “Hello, World!” program:
IDENTIFICATION DIVISION
: This division identifies the program and its author.PROGRAM-ID
is a mandatory paragraph that defines the program’s name.PROCEDURE DIVISION
: This division contains the procedural logic of the program. It starts with thePROCEDURE DIVISION
header.DISPLAY "Hello, World!"
: TheDISPLAY
statement outputs text to the screen. In this case, it prints “Hello, World!”.STOP RUN
: This statement terminates the program.
Conclusion
COBOL may have been around for decades, but its simplicity and readability make it a valuable language, especially in industries where reliability and stability are crucial. In this article, we’ve covered the basics of setting up a COBOL development environment and writing a simple program. From here, you can explore more advanced features of COBOL and start building powerful applications. Happy coding!