Cobol for Beginners: A look at Syntax, Data Types, and Program Structure
- Wim Driessens
- Oct 16, 2024
- 2 min read
Cobol may seem somewhat antiquated compared to modern programming languages, but its structured nature and closeness to the English language make it relatively easy to understand. Let's take a look at the basics of this enduring language.
The four Divisions
Every Cobol program is divided into four main sections, called divisions:
Identification Division: This is where the program is identified, e.g., with a name and author.
Environment Division: This division describes the environment in which the program runs, such as the computer and the required files.
Data Division: This is where the data the program works with is defined, e.g., variables and their data types.
Procedure Division: This division contains the actual program code that processes the data and performs actions.
Syntax
The syntax of Cobol is strict and resembles the structure of English sentences. Each statement begins with a verb (e.g., MOVE, ADD, DISPLAY) and follows fixed rules.
Data Types
Cobol offers various data types, the most important being:
Numeric: For integers and decimal numbers (e.g., PIC 9(5) for a five-digit integer).
Alphanumeric: For character strings (e.g., PIC X(20) for a string of 20 characters).
Alphabetic: For character strings consisting only of letters.
Program Structure Cobol programs follow a hierarchical structure. Within the Procedure Division, sections, paragraphs, and even individual statements can be named and called in any order.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
DISPLAY "Hello, World!".
STOP RUN.
This simple program outputs the text "Hello, World!". It demonstrates the basic structure and straightforward syntax of Cobol..
Conclusion
Although Cobol is no longer one of the most modern languages, its basic principles - structured approach, clear syntax, strict data types - are still relevant today. Anyone who learns Cobol gains insights into the history of programming and can transfer their knowledge to other programming languages.
Comments