In this section, we will cover the basic syntax rules of COBOL, which are essential for writing and understanding COBOL programs. COBOL (Common Business-Oriented Language) has a unique syntax that is designed to be readable and easy to understand, even for those who are not programmers.
Key Concepts
-
Divisions: COBOL programs are divided into four main divisions:
- IDENTIFICATION DIVISION: Contains metadata about the program.
- ENVIRONMENT DIVISION: Specifies the environment in which the program will run.
- DATA DIVISION: Defines the variables and data structures used in the program.
- PROCEDURE DIVISION: Contains the executable instructions of the program.
-
Sections and Paragraphs: Divisions are further divided into sections, and sections can contain paragraphs. Paragraphs are the smallest units of code in COBOL.
-
Sentences and Statements: A sentence in COBOL is a series of one or more statements ending with a period. Statements are the individual instructions in the program.
-
Columns and Formatting: COBOL has specific column requirements:
- Columns 1-6: Sequence numbers (optional).
- Column 7: Indicator area (e.g., '*' for comments).
- Columns 8-11: Area A (for division, section, and paragraph names).
- Columns 12-72: Area B (for statements and code).
- Columns 73-80: Identification area (optional, often used for version control).
Basic Structure of a COBOL Program
Here is a simple example of a COBOL program to illustrate the basic structure and syntax:
IDENTIFICATION DIVISION. PROGRAM-ID. HelloWorld. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. DISPLAY 'Hello, World!'. STOP RUN.
Explanation
- IDENTIFICATION DIVISION: This division contains the
PROGRAM-ID
which uniquely identifies the program. In this case, the program is namedHelloWorld
. - ENVIRONMENT DIVISION: This division is used to specify the environment in which the program will run. It is empty in this simple example.
- DATA DIVISION: This division is used to define variables and data structures. It is also empty in this example.
- PROCEDURE DIVISION: This division contains the executable code. The
DISPLAY
statement outputs the text 'Hello, World!' to the screen, and theSTOP RUN
statement ends the program.
Practical Example
Let's look at a more detailed example that includes variables and arithmetic operations:
IDENTIFICATION DIVISION. PROGRAM-ID. SimpleMath. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM1 PIC 9(2) VALUE 10. 01 NUM2 PIC 9(2) VALUE 20. 01 RESULT PIC 9(3). PROCEDURE DIVISION. ADD NUM1 TO NUM2 GIVING RESULT. DISPLAY 'The result of adding ' NUM1 ' and ' NUM2 ' is ' RESULT. STOP RUN.
Explanation
- WORKING-STORAGE SECTION: This section in the DATA DIVISION is used to declare variables.
NUM1
andNUM2
are defined with a picture clause (PIC
) indicating they are two-digit numbers.RESULT
is a three-digit number. - ADD Statement: The
ADD
statement addsNUM1
andNUM2
and stores the result inRESULT
. - DISPLAY Statement: This statement outputs the result to the screen.
Exercises
Exercise 1: Basic Program Structure
Write a COBOL program that displays your name on the screen.
Solution:
IDENTIFICATION DIVISION. PROGRAM-ID. DisplayName. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. DISPLAY 'Your Name'. STOP RUN.
Exercise 2: Simple Arithmetic
Write a COBOL program that subtracts two numbers and displays the result.
Solution:
IDENTIFICATION DIVISION. PROGRAM-ID. SubtractNumbers. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM1 PIC 9(2) VALUE 30. 01 NUM2 PIC 9(2) VALUE 10. 01 RESULT PIC 9(2). PROCEDURE DIVISION. SUBTRACT NUM2 FROM NUM1 GIVING RESULT. DISPLAY 'The result of subtracting ' NUM2 ' from ' NUM1 ' is ' RESULT. STOP RUN.
Common Mistakes and Tips
- Column Formatting: Ensure that you follow the column formatting rules strictly. Misplacing code outside the designated columns can lead to syntax errors.
- Periods: Every sentence in COBOL must end with a period. Missing periods can cause unexpected behavior or errors.
- Case Sensitivity: COBOL is not case-sensitive, but it is a good practice to follow a consistent style (e.g., using uppercase for keywords).
Conclusion
In this section, we covered the basic syntax rules of COBOL, including the structure of a COBOL program, key concepts, and practical examples. Understanding these fundamentals is crucial for writing and reading COBOL programs effectively. In the next section, we will delve into the various data types available in COBOL.