close
close
hello world program in cobol

hello world program in cobol

2 min read 24-10-2024
hello world program in cobol

Your First Steps in COBOL: Creating a "Hello World" Program

COBOL, short for Common Business Oriented Language, is a high-level programming language designed for business applications. Despite its age (created in 1959!), COBOL is still widely used in various industries, particularly in financial institutions and government agencies.

If you're curious about this powerful language, a great place to start is with the classic "Hello World" program. Let's break down how to create this simple program in COBOL and explore the fundamentals of the language.

The Code:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.

PROCEDURE DIVISION.
    DISPLAY 'Hello, World!'.
    STOP RUN.

Understanding the Code:

  • IDENTIFICATION DIVISION: This division provides essential information about the program, including the program's name (HELLO-WORLD in this case).
  • PROGRAM-ID: Specifies the program's unique identifier.
  • PROCEDURE DIVISION: This is where the actual program logic is written.
  • DISPLAY 'Hello, World!': The core statement that prints the message "Hello, World!" to the output console.
  • STOP RUN: This statement signals the end of the program execution.

Compiling and Running the Program:

You'll need a COBOL compiler to translate your code into executable instructions. Popular options include:

  • GNU COBOL (GnuCOBOL): An open-source compiler available for various operating systems.
  • Micro Focus COBOL: A commercial compiler with a wide range of features.

Once you have a compiler installed, you can compile and run the program. The specific commands may differ depending on your compiler.

Example using GnuCOBOL (assuming the file is saved as "hello.cob"):

cobc -x hello.cob -o hello
./hello

Output:

This will print the following output on your console:

Hello, World!

Key Concepts:

  • IDENTIFICATION DIVISION: This section acts as a program header, containing information like the program name, author, date, and any other relevant details.
  • PROCEDURE DIVISION: This is the core of the program, containing the instructions that the computer will execute.
  • DISPLAY Statement: The DISPLAY statement is used to output data to the console.
  • STOP RUN Statement: Indicates the end of the program execution.

Additional Notes:

  • Case Sensitivity: COBOL is case-insensitive, meaning "DISPLAY" is the same as "display."
  • Keywords: COBOL has a set of reserved words (keywords) that have specific meanings, like "IDENTIFICATION", "PROCEDURE", "DISPLAY", etc. These words should be used correctly in your program.
  • Data Types: COBOL uses a wide range of data types to represent different kinds of information, such as numbers, characters, and dates. You'll learn more about these as you explore the language further.

Next Steps:

The "Hello World" program is just the beginning of your COBOL journey. As you learn more about COBOL, you'll explore more complex concepts, such as:

  • Data Structures: Organizing data in a structured way.
  • Input/Output: Reading data from files and devices.
  • Control Flow: Controlling the order of instructions.
  • Debugging: Finding and fixing errors in your programs.

References:

Attribution:

The initial COBOL code snippet is adapted from the "Hello World" example commonly found in various COBOL resources. This article is written using concepts found in the COBOL documentation and other helpful resources.

This article aims to provide a starting point for understanding COBOL and its basic concepts. As you delve deeper, be sure to explore official documentation and online resources to further enhance your knowledge and skills.

Related Posts


Latest Posts