BudiBadu Logo
Samplebadu

COBOL by Example: Hello World

COBOL 2002

Writing your first COBOL program to display text output on the console, understanding the mandatory IDENTIFICATION DIVISION with PROGRAM-ID paragraph, implementing executable logic in the PROCEDURE DIVISION with the DISPLAY statement for output, and terminating program execution properly with STOP RUN to return control to the operating system.

Code

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

Explanation

Every COBOL program starts with the IDENTIFICATION DIVISION, which serves as the program's identity card. It must contain the PROGRAM-ID paragraph, which specifies the program's name as known to the operating system. This division is the only truly mandatory division in COBOL and is always the first section of code in any standard COBOL program.

The PROCEDURE DIVISION is where the actual work happens. It contains the executable instructions (statements) that the computer follows. COBOL syntax is designed to be English-like, making it readable even to non-programmers. This example demonstrates the DISPLAY statement, which outputs text to the standard output device (usually the terminal or console ). String literals in COBOL can be enclosed in either single quotes or double quotes, depending on compiler preferences.

Finally, the STOP RUN statement is crucial. It acts as the termination command, telling the operating system that the program has finished its execution and control should be returned. Without this, the program might not exit correctly or could fall through to unintended code in larger applications. This is the standard way to exit a main program in COBOL.

Code Breakdown

1
The IDENTIFICATION DIVISION header. This marks the beginning of the program's metadata section. It is strictly required by the COBOL standard.
2
PROGRAM-ID. HELLO-WORLD. defines the program name. This name is often used by the linker or loader. Note the period at the end; COBOL relies heavily on periods to end sentences.
4
The PROCEDURE DIVISION header. This marks the start of the executable code block. Everything after this line is an instruction to be performed.
5
DISPLAY 'Hello, World!' writes the text string to the system's output. The string is enclosed in single quotes (some compilers accept double quotes).
6
STOP RUN halts the program execution immediately. It is the standard way to exit a main program in COBOL.