Basic Syntax in COBOL Language
COBOL (Common Business Oriented Language) has a unique syntax that resembles natural language, making it highly readable for business
-oriented programming. Here’s an overview of some of the basic syntax elements in COBOL:Program Structure in COBOL Language
- COBOL programs are structured into divisions, sections, paragraphs, and sentences.
- The common divisions include Identification Division, Data Division, Environment Division, and Procedure Division.
Comments:
- COBOL comments are designated by an asterisk (*) or slash (/) in columns 7 or 8 of a line.
- Comments can be used to explain code or provide documentation. Example:
* This is a COBOL comment.
Keywords:
- COBOL uses specific keywords to define various program elements.
- Keywords are typically in uppercase. Example:
IDENTIFICATION DIVISION.
PROGRAM-ID. MyProgram.
Variables and Data Types:
- Variables are defined in the Data Division.
- COBOL supports various data types, such as PIC for alphanumeric, numeric, and other data types. Example:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Name PIC X(20).
01 Age PIC 9(3).
Arithmetic Operations:
- COBOL supports common arithmetic operations, such as addition (+), subtraction (-), multiplication (*), and division (/).
- Arithmetic expressions are evaluated from left to right. Example:
COMPUTE Total = Price * Quantity.
Conditional Statements:
- COBOL uses conditionals like IF, ELSE, and END-IF to control program flow.
- Conditionals are typically followed by a condition and a condition name. Example:
IF Age > 18
Display 'You are an adult.'.
ELSE
Display 'You are a minor.'.
END-IF.
Loops:
- COBOL supports loops with the PERFORM statement.
- PERFORM loops can be controlled by various conditions, and they can include sections or paragraphs. Example:
PERFORM 10-Times
Display 'This is iteration ', Iteration-Counter.
Display and Input:
- The DISPLAY statement is used to show output to the user.
- The ACCEPT statement is used to take input from the user. Example:
Display 'Enter your name: '.
ACCEPT Name.
File Handling:
- COBOL provides FILE-CONTROL statements for working with files, such as opening, reading, writing, and closing files. Example:
FILE-CONTROL.
SELECT MyFile ASSIGN TO 'data.txt'.
Paragraphs and Sections:
- Code is organized into paragraphs, which are grouped into sections.
- Paragraphs are executed sequentially, and sections provide logical organization.
Process-Order.
Display 'Processing order...'.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.