Monday, October 21, 2024

COBOL programming

 

COBOL programming





 COBOL (Common Business-Oriented Language)  is a high-level programming language designed primarily for business, finance, and administrative systems. It was first introduced in 1959 and became one of the most widely used languages in areas where data processing is critical, such as banking, insurance, and government applications.

  Key Features of COBOL:

1.  Business-Oriented : COBOL was specifically created for business applications and data processing, rather than scientific or systems programming. It's well-suited for handling large volumes of data, which is common in financial and administrative systems.

 

2.  Readable and Self-Documenting : COBOL uses English-like syntax, making it more readable and easier to understand than many other programming languages, especially for non-technical users. For example, keywords like `ADD`, `SUBTRACT`, `MOVE`, and `DISPLAY` make the code highly descriptive.

 

3.  Structured Programming : While older versions of COBOL supported unstructured code with the heavy use of `GOTO` statements, modern COBOL has evolved to include structured programming concepts like `IF`, `PERFORM` (looping), and `EVALUATE` (switch-case logic).

 

4.  Legacy Systems : COBOL is considered a "legacy" language, but it is still in widespread use today, particularly in mainframe environments where massive data processing and record-keeping systems are critical. Many older systems are still written in COBOL, leading to a high demand for COBOL programmers in specific industries.

 

5.  File Handling and Reporting : COBOL excels at file manipulation and generating reports from data, making it ideal for tasks like payroll processing, transaction records, and other financial operations.

  Basic Structure of a COBOL Program:

 A COBOL program is divided into four main  divisions , each serving a specific purpose:

 

1.  IDENTIFICATION DIVISION : Provides basic information about the program, such as its name and author.

2.  ENVIRONMENT DIVISION : Describes the hardware and operating system requirements, like input/output device details.

3.  DATA DIVISION : Defines the variables, constants, and data structures the program will use.

4.  PROCEDURE DIVISION : Contains the logic and actual code for manipulating data and performing operations.

 

 Example COBOL Program: Adding Two Numbers

 

```cobol

IDENTIFICATION DIVISION.

PROGRAM-ID. AddTwoNumbers.

 

ENVIRONMENT DIVISION.

 

DATA DIVISION.

WORKING-STORAGE SECTION.

   01 NUM1 PIC 9(3) VALUE 0.

   01 NUM2 PIC 9(3) VALUE 0.

   01 RESULT PIC 9(4) VALUE 0.

 

PROCEDURE DIVISION.

   DISPLAY "Enter first number: ".

   ACCEPT NUM1.

   DISPLAY "Enter second number: ".

   ACCEPT NUM2.

 

   ADD NUM1 TO NUM2 GIVING RESULT.

 

   DISPLAY "The result is: " RESULT.

   STOP RUN.

```

  Explanation:

-  IDENTIFICATION DIVISION : This division tells us that the program's name is "AddTwoNumbers."

-  ENVIRONMENT DIVISION : In this simple example, there are no special hardware or OS requirements, so it's left blank.

-  DATA DIVISION : We define three variables, `NUM1`, `NUM2`, and `RESULT`, with specific picture (PIC) clauses. The PIC clause specifies the format of the variables (`9(3)` means a 3-digit number).

-  PROCEDURE DIVISION : The logic of the program. The user is prompted to input two numbers, they are added together, and the result is displayed.

 COBOL Syntax Elements:

1.  Variables : Declared in the `DATA DIVISION` using the `PIC` (Picture) clause to specify the size and type. For example, `PIC 9(3)` represents an integer with 3 digits, while `PIC X(10)` represents a string with 10 characters.

 

2.  Arithmetic Operations : COBOL uses commands like `ADD`, `SUBTRACT`, `MULTIPLY`, `DIVIDE`, and `COMPUTE`. The results of these operations can be stored in a variable or directly displayed.

 3.  Conditions : COBOL supports standard conditional operations using `IF` statements.

   ```cobol

   IF NUM1 > NUM2 THEN

      DISPLAY "NUM1 is greater than NUM2."

   ELSE

      DISPLAY "NUM1 is less than or equal to NUM2."

   END-IF.

   ```

 

4.  Loops : COBOL uses `PERFORM` to create loops. You can repeat a set of instructions for a fixed number of times or until a condition is met.

   ```cobol

   PERFORM TIMES-10

      DISPLAY "Loop iteration: " I.

      ADD 1 TO I.

   END-PERFORM.

   ```

 5.  File Handling : COBOL has strong support for working with files, making it ideal for handling large datasets such as payroll or inventory records. Files can be read and written using commands like `OPEN`, `READ`, `WRITE`, and `CLOSE`.

 

 COBOL in the Modern World:

Despite being over 60 years old, COBOL is still actively used in critical industries, especially in financial institutions and government systems where reliability and stability are paramount. Many banks, insurance companies, and governments still use COBOL to run their core systems, often on mainframe computers. These systems process billions of transactions daily, and rewriting them in a modern language would be extremely costly and risky, leading to the continued demand for COBOL expertise. 

While it's not as trendy as languages like Python or Java, the ability to maintain and enhance COBOL-based systems is a valuable skill, especially as many COBOL experts retire, creating a need for new programmers to manage legacy systems.

  Summary:

 -  COBOL  is a high-level language designed for business applications, focusing on data processing and reporting.

- It has a highly readable syntax, which makes it accessible for both developers and business analysts.

- COBOL is still widely used in industries where stable, large-scale transaction processing systems are required, and it has a specific strength in handling data files and generating reports.

=====

Yes,  COBOL  is still widely used in industries, particularly in sectors that rely on large-scale transaction processing and data management systems. Despite being over 60 years old, COBOL remains crucial for many organizations due to its stability, efficiency, and the significant role it has played in the development of critical business applications over the decades.

 

   Key Industries Where COBOL is Still Widely Used: 

1.  Banking and Financial Services :

   - COBOL powers the backend of many banks and financial institutions worldwide. It is used for tasks such as handling transactions, managing customer accounts, generating statements, and supporting ATMs. Large-scale operations, such as processing billions of daily transactions, are often managed using COBOL because of its reliability and performance with large data volumes.

 

2.  Insurance :

   - Insurance companies use COBOL for policy management, claims processing, underwriting, and premium calculations. The long-term nature of insurance policies makes COBOL systems ideal, as they require stability and the ability to store and retrieve data reliably over extended periods. 

3.  Government Systems :

   - Many government agencies still rely on COBOL for critical systems related to taxation, social security, and unemployment benefits. For instance, the U.S. Social Security Administration and IRS use COBOL to manage vast datasets related to citizens' financial and personal records.

   - COBOL systems are deeply embedded in public sector infrastructure because they have proven dependable over the decades. 

4.  Retail and Manufacturing :

   - COBOL plays a significant role in supply chain management, inventory control, and point-of-sale (POS) systems. Many large retailers and manufacturing companies rely on COBOL-based systems for managing orders, stock, and logistics. 

5.  Airlines and Transportation :

   - The airline industry uses COBOL for reservation systems, ticketing, flight scheduling, and other logistics operations. Many legacy systems in the travel and transportation industry are built on COBOL because of the large amount of data involved and the need for high transaction throughput. 

   Why COBOL Continues to Be Used: 

1.  Legacy Systems :

   - Many organizations invested heavily in COBOL systems decades ago, and these systems have proven to be reliable, efficient, and scalable. Rewriting or replacing these systems would be risky, expensive, and time-consuming, as they handle mission-critical operations.

   - In many cases, businesses and governments have found that maintaining and updating existing COBOL systems is more cost-effective than attempting a full system migration to a modern language. 

2.  Stability and Reliability :

   - COBOL is known for its robustness and ability to process large volumes of transactions without failure. This is vital in sectors like finance, where system downtime can lead to huge financial losses.

   - Over time, COBOL systems have evolved to handle new requirements, but they retain their core strengths of performance and reliability. 

3.  Performance with Large-Scale Data Processing :

   - COBOL is excellent at handling batch processing, making it ideal for tasks that require the processing of millions or even billions of records at once, such as payrolls, banking transactions, and insurance claims. 

4.  Mainframe Compatibility :

   - COBOL runs on  mainframes , which are still heavily used by large corporations for critical business applications. Mainframes are known for their ability to handle massive workloads and their long uptime (sometimes running for years without interruption). As COBOL was designed to work efficiently on mainframes, it remains the go-to language for mainframe applications. 

5.  Long Lifespan of Business Systems :

   - Business systems, especially in banking, insurance, and government, tend to have very long lifecycles. COBOL systems, many of which were built in the 1960s and 1970s, have been running for decades and are still fulfilling the needs of these organizations without major failures. 

   Challenges and Modernization Efforts: 

1.  Lack of COBOL Programmers :

   - As COBOL was developed decades ago, many of the original COBOL programmers are retiring. This has led to a shortage of skilled professionals capable of maintaining and updating COBOL systems, which presents a challenge for organizations that still rely on the language.

   - To address this, some institutions are offering COBOL training programs to bring new developers into the field. 

2.  Modernization and Integration :

   - While COBOL remains reliable, some organizations are modernizing their systems by integrating COBOL programs with newer technologies, such as web services, cloud computing, and mobile apps.

   -  COBOL modernization  projects involve refactoring COBOL code, updating system interfaces, or even re-hosting COBOL applications on modern platforms. Hybrid systems allow companies to maintain their COBOL core while interacting with modern technologies. 

3.  Attempts to Replace COBOL :

   - Although many organizations are exploring alternatives to COBOL, complete rewrites in modern languages like Java or Python are costly and risky. COBOL's long track record of reliability and the sheer complexity of migrating decades-old systems have made it difficult for companies to phase it out entirely. 

   COBOL's Continued Importance: 

COBOL remains indispensable for large organizations and governments where massive amounts of data need to be processed efficiently and reliably. Many critical systems still rely on COBOL because the risk of replacing or fully re-engineering these systems is too high. While other programming languages may dominate newer software projects, COBOL continues to quietly power the world’s financial systems, government programs, and essential business operations. 

In summary, COBOL remains vital because of:

-  Its proven stability and performance .

-  The enormous cost and complexity of replacing legacy systems .

-  Its specific strengths in transaction processing, data management, and reporting .

-  Its strong presence on mainframes, which are still used for mission-critical applications . 

While COBOL may not be glamorous, it is the backbone of many essential industries.

=====

Introduction to COBOL

Sections

§ A section is a block of code made up of one or more paragraphs.

§ A section begins with the section name and ends where the next

section name is encountered or where the program text ends.

§ A section name consists of a name devised by the programmer

or defined by the language followed by the word SECTION

followed by a full stop.

Introduction to COBOL

Paragraphs

§ Each section consists of one or more paragraphs.

§ A paragraph is a block of code made up of one or more

sentences.

§ A paragraph begins with the paragraph name and ends with

the next paragraph or section name or the end of the

program text.

§ The paragraph name consists of a name devised by the

programmer or defined by the language followed by a full

stop.

PrintFinalTotals.

PROGRAM-ID.

Introduction to COBOL

Sentences and Statements

§ A paragraph consists of one or more sentences.

§ A sentence consists of one or more statements and is terminated by a full

stop.

MOVE .21 TO VatRate

COMPUTE VatAmount = ProductCost * VatRate.

DISPLAY "Enter name " WITH NO ADVANCING

ACCEPT StudentName

DISPLAY "Name entered was " StudentName.

§ A statement consists of a COBOL verb and an operand or operands.

SUBTRACT Tax FROM GrossPay GIVING NetPay

READ StudentFile

AT END SET EndOfFile TO TRUE

END-READ.

IDENTIFICATION DIVISION.

PROGRAM-ID. SAMPLE1.

AUTHOR. Michael Coughlan.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 Num1 PIC 9 VALUE ZEROS.

01 Num2 PIC 9 VALUE ZEROS.

01 Result PIC 99 VALUE ZEROS.

PROCEDURE DIVISION

CalculateResult.

ACCEPT Num1.

ACCEPT Num2.

MULTIPLY Num1 BY Num2 GIVING Result.

DISPLAY "Result is = ", Result.

STOP RUN

==

The minimum COBOL program

IDENTIFICATION DIVISION.

PROGRAM-ID. SAMPLE2.

PROCEDURE DIVISION.

DisplayPrompt.

DISPLAY "I 

==

Almost all COBOL compilers treat a line of COBOL code as if it contained

two distinct areas. These are known as;

Area A and Area B

§ When a COBOL compiler recognizes these two areas, all division, section,

paragraph names, FD entries and 01 level numbers must start in Area A. All

other sentences must start in Area B.

§ Area A is four characters wide and is followed by Area B.

===

Name Construction

All user defined names, such as data names, paragraph names,

section names and mnemonic names, must adhere to the following

rules;

– They must contain at least one character and not more than 30

characters.

– They must contain at least one alphabetic character and they must

not begin or end with a hyphen.

– They must be contructed from the characters A to Z, the number 0 to

9 and the hyphen. e.g. TotalPay, Gross-Pay,

PrintReportHeadings, Customer10-Rec

§ All data-names should describe the data they contain.

§ All paragraph and section names should describe the function of

the paragraph or section.

COBOL Basics 1

N=====

Describing DATA

There are basically three kinds of data used in COBOL programs;

1. Variables.

2. Literals.

3. Figurative Constants.

Unlike other programming languages, COBOL does not support

user defined constants.

====

Data-Names / Variables

A variable is a named location in memory into which a program can put

data and from which it can retrieve data.

§ A data-name or identifier is the name used to identify the area of memory

reserved for the variable.

§ Variables must be described in terms of their type and size.

§ Every variable used in a COBOL program must have a description in the

DATA DIVISION. 

=====

While   COBOL   may not be seen as a "modern" or "trendy" programming language like Python, Java, or JavaScript, it plays a   crucial role as the backbone   of many essential industries due to its stability, reliability, and efficiency in handling large-scale business operations. Its ability to process massive amounts of transactions and data on a daily basis is invaluable in sectors where accuracy, speed, and uninterrupted service are non-negotiable.

 

   Key Reasons Why COBOL Remains the Backbone of Essential Industries: 

1.   Stability and Reliability  :

   - COBOL has been used in production for over 60 years, and many of the systems running today were developed decades ago. These systems continue to operate with minimal failures, providing   consistent and reliable performance  .

   - For industries such as banking, insurance, and government, where downtime can lead to massive financial losses or disruptions to essential services, COBOL's reliability is crucial. 

2.   Handling Large-Scale Transactions  :

   - Industries that deal with   millions or even billions of transactions   daily, such as banks processing credit card payments or payroll systems handling employee salaries, rely on COBOL’s ability to efficiently process batch data.

   - COBOL’s   transactional integrity   and ability to handle large volumes of data make it the go-to language for applications where accurate record-keeping is critical. 

3.   Compatibility with Mainframes  :

   - Mainframes are still heavily used by organizations that require massive data processing power and high availability. COBOL was designed to run on these systems, making it ideal for companies with established mainframe infrastructures.

   - COBOL applications running on   mainframes   have been fine-tuned over the years, providing an unmatched level of performance and reliability for business-critical operations. 

4.   Long-Term Use and Investment  :

   - Companies have invested heavily in COBOL systems over the past few decades. These systems form the   core infrastructure   of many businesses and government agencies.

   - Replacing COBOL systems with modern software is not just expensive but   risky  . The sheer complexity of migrating essential systems without causing disruptions to business operations has deterred many organizations from moving away from COBOL entirely. 

5.   Business-Centric Features  :

   - COBOL was built for   business applications  , particularly those focused on   data processing  ,   reporting  , and   financial operations  . It has built-in support for handling things like large data files, formatted reports, and precise decimal calculations, all of which are critical in industries like finance and government.

   - Its highly readable, English-like syntax also makes COBOL easy to maintain for professionals in business roles who may not be computer scientists but need to understand how these systems operate. 

6.   Regulatory and Compliance Needs  :

   - In industries like   banking  ,   insurance  , and   government  , COBOL systems are often closely tied to   regulatory compliance  . These industries require strict adherence to rules regarding financial reporting, auditing, and customer data security.

   - COBOL’s long-established processes, along with the fact that it’s been optimized over decades for these tasks, make it a trusted choice for managing sensitive, regulated data. 

   The Glamour of COBOL’s Dependability: 

Though COBOL may not be seen as "glamorous" in the way cutting-edge languages and tools are, it serves as a   foundation   that allows essential industries to operate smoothly. These industries require software that can handle high-volume transactions with impeccable accuracy and reliability. While newer technologies may offer fancy features or faster development times for consumer-facing applications, COBOL remains   indispensable   for the critical systems that keep the world's financial, government, and business sectors functioning. 

   Examples of COBOL in Use:

1.   Banks  : Large banks like JPMorgan Chase, Bank of America, and Wells Fargo still rely on COBOL systems to manage customer accounts, process transactions, and run ATMs. COBOL handles   90% of all ATM transactions   globally. 

2.   Insurance  : Companies like MetLife and Prudential run core policy and claims management systems on COBOL. 

3.   Governments  : Government agencies like the U.S.   Social Security Administration   and   IRS   use COBOL for managing citizens' financial data, taxes, and benefits. COBOL handles a significant portion of social security and unemployment benefits calculations. 

4.   Retail  : Major retailers use COBOL for   inventory management   and   supply chain logistics  , ensuring that products move efficiently from manufacturers to customers. 

   In Conclusion: 

COBOL may not be cutting-edge or flashy, but it quietly powers the systems that the world depends on every day. Whether it's processing billions of dollars in transactions or managing essential public services, COBOL is the   unsung hero   behind the scenes, enabling the modern economy to function smoothly. Its longevity and continued use in critical systems demonstrate that   glamour isn’t always about being new  —sometimes, it’s about being reliable, resilient, and indispensable.

======




Here's an example of a simple **COBOL** program that demonstrates basic functionality, including user input, arithmetic operations, and output. This COBOL program takes two numbers from the user, adds them together, and displays the result.

### COBOL Example: Adding Two Numbers

```cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. AddTwoNumbers.

ENVIRONMENT DIVISION.

DATA DIVISION.
WORKING-STORAGE SECTION.
   01 NUM1 PIC 9(5) VALUE 0.        * Declares the first number (up to 5 digits).
   01 NUM2 PIC 9(5) VALUE 0.        * Declares the second number (up to 5 digits).
   01 RESULT PIC 9(6) VALUE 0.      * Declares the result (up to 6 digits).

PROCEDURE DIVISION.
   DISPLAY "Enter the first number: ".  * Prompts the user for input.
   ACCEPT NUM1.                         * Accepts the input and stores it in NUM1.

   DISPLAY "Enter the second number: ". * Prompts the user for the second input.
   ACCEPT NUM2.                         * Accepts the input and stores it in NUM2.

   ADD NUM1 TO NUM2 GIVING RESULT.      * Adds NUM1 and NUM2, stores result in RESULT.

   DISPLAY "The sum is: " RESULT.       * Displays the result.

   STOP RUN.                            * Ends the program.
```

### Explanation of the Program:

1. **IDENTIFICATION DIVISION**: This section provides the name of the program (`AddTwoNumbers`). Every COBOL program must start with this division.
   
2. **ENVIRONMENT DIVISION**: This section specifies the computer environment in which the program runs. In this simple example, we don’t specify anything, as it's assumed to run in a standard environment.

3. **DATA DIVISION**:
   - In the **WORKING-STORAGE SECTION**, we define three variables:
     - `NUM1` and `NUM2`: Used to store the two numbers the user will input.
     - `RESULT`: Stores the sum of `NUM1` and `NUM2`.
   - The `PIC` (Picture) clause specifies the format and size of the variables:
     - `9(5)` means a five-digit integer.
     - `9(6)` allows up to six digits for the result, to accommodate possible overflow during the addition.

4. **PROCEDURE DIVISION**:
   - This is where the logic and main operations of the program are written.
   - The program displays messages using the `DISPLAY` command to prompt the user for input, and `ACCEPT` reads the input into the corresponding variables (`NUM1` and `NUM2`).
   - The `ADD` command performs the arithmetic operation, adding `NUM1` to `NUM2` and storing the result in `RESULT`.
   - Finally, the program uses `DISPLAY` to show the result, followed by `STOP RUN` to terminate the program.

### Example Output:

```
Enter the first number:
5
Enter the second number:
10
The sum is: 15
```

---

### COBOL Example: Simple Payroll Calculation

This program calculates the gross salary of an employee based on their hours worked and hourly wage rate.

```cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. PayrollCalculator.

ENVIRONMENT DIVISION.

DATA DIVISION.
WORKING-STORAGE SECTION.
   01 EMPLOYEE-NAME PIC A(20).                * Employee name (up to 20 characters).
   01 HOURS-WORKED  PIC 9(3) VALUE 0.         * Hours worked (up to 999 hours).
   01 HOURLY-RATE   PIC 9V99 VALUE 0.         * Hourly wage rate (dollars and cents).
   01 GROSS-PAY     PIC 9(5)V99 VALUE 0.      * Gross pay (up to $99999.99).

PROCEDURE DIVISION.
   DISPLAY "Enter employee name: ".
   ACCEPT EMPLOYEE-NAME.

   DISPLAY "Enter hours worked: ".
   ACCEPT HOURS-WORKED.

   DISPLAY "Enter hourly wage rate: ".
   ACCEPT HOURLY-RATE.

   MULTIPLY HOURLY-RATE BY HOURS-WORKED GIVING GROSS-PAY.  * Calculates gross pay.

   DISPLAY "Employee: " EMPLOYEE-NAME.
   DISPLAY "Gross Pay: $" GROSS-PAY.

   STOP RUN.
```

### Explanation of the Payroll Program:

1. **DATA DIVISION**:
   - `EMPLOYEE-NAME` stores the name of the employee (up to 20 characters).
   - `HOURS-WORKED` holds the total hours the employee worked.
   - `HOURLY-RATE` is the wage rate, allowing for dollars and cents.
   - `GROSS-PAY` stores the result of multiplying the hours worked by the hourly rate.

2. **PROCEDURE DIVISION**:
   - The program prompts the user to input the employee's name, hours worked, and hourly rate using `DISPLAY` and `ACCEPT`.
   - The `MULTIPLY` statement calculates the gross pay by multiplying the hourly rate by the number of hours worked and stores the result in `GROSS-PAY`.
   - The result is displayed to the user.

### Example Output:

```
Enter employee name:
John Smith
Enter hours worked:
40
Enter hourly wage rate:
15.75
Employee: John Smith
Gross Pay: $630.00
```

---
These examples illustrate some of the basic COBOL concepts like arithmetic operations, working with variables, and interacting with users via input and output. COBOL’s syntax is highly readable, making it easy to understand, even for people who are new to programming or from non-technical backgrounds. This readability is one of the reasons COBOL has remained in use for so many decades, particularly in industries that need stable, long-running business applications.

====
















No comments:

Post a Comment

Small coding languages

  Yes, Hack is a programming language developed by Facebook (now Meta) as a dialect of PHP. It was designed to address some of the limitatio...