Cobol and Mainframe

How to Run COBOL on Linux

Designed primarily for business and financial applications, COBOL is a high-level programming language. COBOL has been widely used for business data processing, financial systems, and government applications for over 60 years and it is still dominating the enterprise computing. COBOL is still relevant and widely used for large-scale legacy systems and applications, especially in the financial and government sectors. COBOL is simple, robust, and easy-to-learn programming language.  The following table describes these five fields.

COBOL Coding Format

 

Columns

Use

Description

1-6

Sequence numbers or Page and line numbers Used in the olden days for sequence checking.

7

Continuation, comment, or form feed (-) To continue the non-numeric literals from the previous line.

(*) To comment the line.

(/) To instruct the printer to move to a new page when printing the source program.

8-11

Area A Used for special entries such as DIVISION, SECTION, Paragraphs names.

12-72

Area B Used for most COBOL entries, including PROCEDURE DIVISION sentences.

73-80

Identification Area Used in the olden days for identification purposes.

 

Overview of the Program Structure in COBOL

It has four main parts: Identification, Environment, Data, and Procedure Divisions.

Identification Division:

Identification Division contains information about the program such as the program name and the author’s name. The Identification Division is where you name your program and provide any additional identifying information that you want. It is compulsory.

Here’s an example of the Identification Division in a COBOL program:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
AUTHOR. YOUR-NAME.

 
Environment Division:

Environment Division is an optional DIVISION. It comprises of the two sections namely CONFIGURATION SECTION and INPUT-OUTPUT SECTION. The Environment Division pertains to the computers that are used to compile the source program and execute the object program. It also encompasses the INPUT-OUTPUT section which specifies the program’s input and output files.

Here’s an example of the Environment Division in a COBOL program:

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
DECIMAL-POINT IS COMMA.
CURRENCY SIGN IS INR.

 
Data Division:

The Data Division contains an information about the data that is used in the program such as data names, data types, and data sizes.

Here’s an example of the Data Division in a COBOL program:

DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY-VARIABLE PIC 9(4).
01 MY-STRING PIC X(20).

 
Procedure Division:

The Procedure Division is the most important section of a COBOL program as it contains the actual logic of the program such as calculations, input/output operations, and conditional statements. It is the final division in the program and is responsible for executing the program’s instructions.

Here’s an example of the Procedure Division in a COBOL program:

PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE
READ INPUT-FILE AT END SET END-OF-FILE TO TRUE
 NOT AT END
 MOVE INPUT-FIELD TO OUTPUT-FIELD
 WRITE OUTPUT-RECORD
 END-READ
CLOSE INPUT-FILE
STOP RUN.

 

How to Run Some COBOL Programs

Example 1:

IDENTIFICATION DIVISION.
PROGRAM-ID. WELCOME-HOME.
PROCEDURE DIVISION.
DISPLAY "Welcome Home!".
STOP RUN.

 
Output:

papan@ubuntu:~/Desktop/cb$ cobc -xjF 1.cbl
Welcome Home!

 
Explanation:

This COBOL code basically shows how to run a COBOL program. This COBOL code uses the IDENTIFICATION DIVISION to identify the program and specify its name, “WELCOME-HOME.” The Procedure Division uses a DISPLAY statement to show the message, “Welcome Home!”

Example 2:

IDENTIFICATION DIVISION.
PROGRAM-ID. SUBTRACTION.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUMBER1 PIC 9(4).
01 NUMBER2 PIC 9(4).
01 RESULT PIC 9(4).

PROCEDURE DIVISION.
    DISPLAY "Enter 1st number: ".
    ACCEPT NUM1.
    DISPLAY "Enter 2nd number: ".
    ACCEPT NUM2.

    SUBTRACT NUM2 FROM NUM1 GIVING RESULT.

    DISPLAY "The result of subtraction is: " RESULT.
STOP RUN.

 
Output:

papan@ubuntu:~/Desktop/cb$ cobc -xjF 2.cbl
Enter 1st number:
80
Enter 2nd number:
60
The result of subtraction is: 0020

 
Explanation:

This is a COBOL program that calculates the subtraction between two numbers that are entered by the user. It uses the DATA division to declare the variables, and the PROCEDURE division for logic, including prompting the user for input, performing the subtraction, and displaying the result.

Example 3:

IDENTIFICATION DIVISION.
PROGRAM-ID. COLLEGE-ADMIN.
ENVIRONMENT DIVISION.
 INPUT-OUTPUT SECTION.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-ADMIN-NAME .
05 WS-FIRST PIC X(10) VALUE SPACE.
05 FILLER PIC X(10) VALUE SPACE.
05 WS-LAST PIC X(15) VALUE SPACE.
PROCEDURE DIVISION.
000-MAIN-LOGIC.
    PERFORM 005-PRT-EMP-DATA
STOP RUN.
005-PRT-EMP-DATA SECTON.
010-MOVE-DATA.
   
    MOVE 'NATH' TO WS-FIRST.
    MOVE 'SOMDEB' TO WS-LAST.
010-EXIT.
    EXIT.
   

020-PRT-DATA.
    DISPLAY ‘ADMIN NAME: ', WS-ADMIN-NAME
020-EXIT.
    EXIT.

 
Output:

papan@ubuntu:~/Desktop/cb$ cobc -xjF 3.cbl
ADMIN NAME: NATH SOMDEB

 
Explanation:

This program stores and displays the name of a college administrator by declaring the name variables in the working-storage section and using two sections in Procedure Division to move the first and last names to the WS-ADMIN-NAME variable and display the name on the screen.

Conclusion

COBOL is still used for large-scale systems, especially in finance and government. By following the steps in this article, one can run the COBOL programs. This article explains the different sections of a COBOL program and how they work together to form a complete COBOL program. A foundation is established to write and run the COBOL programs, regardless if you are new or experienced in COBOL programming.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com