top

Search

Software Key Tutorial

.

UpGrad

Software Key Tutorial

Cobol Tutorial

Introduction

COBOL, short for Common Business Oriented Language, is an old but vital programming language. COBOL is mainly used in the banking sector. While this programming language might seem old-fashioned compared to modern languages, understanding COBOL basics is important for anyone dealing with legacy systems or in industries like banking and insurance.

 One of COBOL's unique features is the concept of 'Area A in COBOL'.  Area A helps organize the COBOL code structure. Thus, this COBOL tutorial aims to break down the complexities of COBOL syntax to ensure that you understand the foundational concepts of this language.

Whether you're new to COBOL code or refreshing your knowledge, this tutorial covers critical points in the COBOL language and builds a solid foundation for advanced topics.

Overview

COBOL, a vital programming language, plays a key role in developing significant software, primarily employed in business, finance, and administrative systems.

Over time, the COBOL programming language has affirmed its usefulness and resilience. This tutorial offers insight through a COBOL code example and a COBOL program example. These instances offer practical experience to learners, highlighting how the language manages real-world problems.

In our COBOL tutorial, you'll learn the theoretical foundations and practical usage in everyday programming situations.

What is COBOL?

Common Business Oriented Language (COBOL) is a high-level programming language—developers designed this language for business applications. COBOL is still used in many financial and business applications. This language is known for its verbose syntax, making the codes highly readable.

Example of COBOL Code:

Consider a simple COBOL program that adds two numbers.

In this example:

  • The IDENTIFICATION DIVISION gives information about the program.

  • The DATA DIVISION is where you declare variables.

  • The PROCEDURE DIVISION has the actual logic or instructions.

How Does COBOL Work?

COBOL is a high-level programming language that was designed to be both machine-independent and human-readable. This means that COBOL programs can run on different types of machines without modification. People can understand the program's logic just by reading it.

COBOL Program Structure

A typical COBOL program is divided into four main divisions:

  1. Identification Division

  2. Environment Division

  3. Data Division

  4. Procedure Division

Example of COBOL Code:

Consider a COBOL program to calculate and display the area of a rectangle:

Explanation:

  • Identification Division: It identifies the program. Every COBOL program begins with this.

  • Data Division: This is where all data items (variables) are declared.

  • Procedure Division: This contains the logical instructions. In the example, it multiplies length and breadth to find the area and then displays the result.

The History and Evolution of COBOL Programming

COBOL has a rich history that spans over six decades.

The 1950s: Birth of COBOL

COBOL's story began in the late 1950s. At that time, programming lacked standardization, and every machine had its unique language. Recognizing the need for a universal language that could run on multiple machines, the US Department of Defense convened a committee. With experts like Grace Hopper at the helm, the aim was to create a business-centric language. The result was COBOL, officially introduced in 1959.

The 1960s: The Expansion Era

In the 1960s, COBOL's popularity soared. It became synonymous with business computing, with its user-friendly syntax resembling English. Major corporations adopted COBOL, leading to its further enhancement. COBOL-61 Extended was one such revision, addressing the initial version's limitations.

1970s-1980s: Standardization and Growth

The 1970s and 1980s saw efforts to standardize COBOL. The American National Standards Institute (ANSI) released many standard versions, starting with COBOL-74 and followed by COBOL-85, which added new features and further refined the language.

1990s: The Y2K Role

The 1990s heralded the internet era, and many believed COBOL's days were numbered. However, the impending Y2K crisis gave it renewed importance. The majority of critical business systems were written in COBOL, and they needed updates to handle the year 2000 correctly. This event highlighted COBOL's deep roots in the business world.

2000s-Present: Legacy and Modernization

Post-Y2K, while the latest languages dominated app and web development, COBOL maintained its stronghold in legacy systems, especially in the banking, insurance, and airline sectors. Current estimates suggest that billions of lines of COBOL code are still in operation today.

The modern era has seen efforts to integrate COBOL with the latest tech trends. Tools have emerged to transition COBOL applications to the web or to interface them with modern systems, ensuring that the language remains relevant.

Fundamentals of COBOL Syntax: A Deep Dive

COBOL's syntax is renowned for its English-like structure, making it one of the programming languages with enhanced readability. This design was intentional, aiming to create a user-friendly language for business applications.

  1. Divisions: Every COBOL program is structured into four primary divisions:

  • Identification Division: This section introduces the program, specifying the name and other meta information.

  • Environment Division: Describes the system and devices the program will interact with.

  • Data Division: Here, data structures and variables are defined.

  • Procedure Division: The heart of the program, where actual processing and business logic are coded.

  1. Sections and Paragraphs: Within divisions, the code can be further broken down into sections and paragraphs, providing a hierarchical structure. For instance, in the Data Division, you can have a 'Working-Storage Section' to declare temporary variables.

  2. COBOL Words: COBOL has reserved words with exact meanings. Like DISPLAY, ADD, and SUBTRACT. These words, or verbs, guide the actions in a program.

  3. Clauses and Sentences: Sentences make up the primary logic in the Procedure Division. A sentence ends with a period (.) and contains one or more statements. Clauses provide details for statements.

Example:

Here, DISPLAY is a COBOL verb. The entire line is a sentence.

  1. Data Types and PICTURE Clause: COBOL doesn't use data types like int or string. Instead, it uses the PICTURE (or PIC) clause to describe the format of data items.

Example:

This defines ITEM-PRICE as a numeric data item, with five digits before the decimal point and two after.

  1. Conditionals and Loops: COBOL offers various constructs for condition checking, like IF...ELSE...END-IF and looping constructs such as PERFORM and PERFORM UNTIL.

  2. Sequential and Indexed Files: Handling data files, whether sequential or indexed, is a strong point for COBOL. Syntax related to file operations is designed to manage large-scale data processing efficiently.

Crafting Your First COBOL Program: Step-by-Step Guide

Step 1: Setting Up Your Environment

Before coding, ensure you have the following:

  • A COBOL compiler is installed, such as GnuCOBOL or Micro Focus COBOL.

  • A text editor or Integrated Development Environment (IDE) like Notepad++ or Open COBOL IDE.

Step 2: Starting Your Program

Begin with the IDENTIFICATION DIVISION, which provides basic details about your program.

Step 3: Defining the Environment

For this basic program, we won't require any specific environment setup, but it's still good to acknowledge the ENVIRONMENT DIVISION.

Step 4: Declaring Data

Even though we won't use variables in this example, we'll still declare the DATA DIVISION.

Step 5: Writing the Procedure

This is where the core logic resides. For our program, we want to display a greeting.

DISPLAY is the COBOL verb used to print to the screen, and STOP RUN signifies the program's end.

Step 6: Saving and Compiling

  1. Save your file with a .cob or .cbl extension, like HelloWorld.cob.

  2. Open your terminal or command prompt.

  3. Navigate to your program's directory.

  4. Compile the program. For GnuCOBOL, you'd use:

Step 7: Running Your Program

If the compilation was successful, you'll have an executable. Run it:

  • For Windows, you'd simply use HelloWorld.exe.

  • You should see the output: Hello, World!

Understanding 'Area A' in COBOL: Why It Matters

COBOL is unique in many ways, and one of its distinguishing features is its attention to formatting, precisely the concept of 'Area A' and 'Area B'. These are not just cosmetic guidelines but are crucial for proper program structuring and execution.

What is 'Area A'?

In COBOL, a coding line is typically 80 characters wide, mirroring the width of old punch cards. This line is divided into two primary areas:

  • Area A: Columns 8 through 11

  • Area B: Columns 12 through 72

'Area A' has a special significance in the structure and readability of the COBOL code.

Role of 'Area A':

  • Division and Section Names: Key COBOL program structures like DATA DIVISIONPROCEDURE DIVISION, and section names such as WORKING-STORAGE SECTION start in 'Area A'. This placement is necessary for the compiler to recognize these as divisions or sections.

  • Labels and Level Numbers: Level numbers, which define the hierarchy of data structures, and paragraph names, which serve as labels in the PROCEDURE DIVISION, begin in 'Area A'. For instance, level numbers like 01 or 05 should be positioned here.

Why 'Area A' Matters:

1. Clarity and Structure: Having a dedicated area for divisions, sections, and labels provides clarity. Readers can instantly recognize structural components, making code easier to understand.

2. Compiler Expectation: COBOL compilers expect certain code elements in specific columns. Not adhering to this can result in compilation errors. 'Area A' placement ensures the compiler correctly identifies and processes these vital parts of the program.

3. Historical Consistency: COBOL's origins trace back to punch card systems, where specific columns had designated meanings. By maintaining these areas, COBOL retains compatibility with older programs, ensuring decades-old codes run effectively on modern systems.

4. Error Prevention: Accidentally starting a division or section name in 'Area B' instead of 'Area A' can lead to subtle errors or misinterpretations. Maintaining the distinction helps avoid such pitfalls.

COBOL's Role in Today's Tech Landscape: Beyond Legacy Systems

Debugging is crucial in any programming language, and COBOL is no exception. A bug-free COBOL program ensures efficient and reliable operations, especially given its prominence in business sectors. Here are some handy tips for smooth COBOL coding:

  1. Use the DISPLAY Verb: This is COBOL's version of the print statement. Use DISPLAY to output variable values and trace program execution.

  2. Consistent Formatting: Stick to 'Area A' and 'Area B' guidelines. Proper indentation helps spot structural issues.

  3. Compiler Options: Use compiler diagnostic options to get detailed error reports. They pinpoint issues for faster resolution.

  4. Initialize Variables: Always initialize data items. Uninitialized variables can lead to unexpected behavior.

  5. Check File Operations: Ensure all file operations (like OPEN, READ, and, WRITE) succeed by checking their status codes.

  6. Interactive Debuggers: Utilize debuggers available with modern COBOL compilers. They allow breakpoints, variable inspections, and step-by-step execution.

Conclusion

In the ever-evolving landscape of programming, COBOL stands as a testament to enduring design and functionality. Its foundational role in the business and finance sectors highlights the significance of clarity, structure, and reliability in coding. COBOL tutorial involves grasping its unique syntax, adhering to best practices, and honing debugging skills. While it may seem old-fashioned to some, understanding COBOL opens doors to vast legacy systems that are still critical today. Embracing its nuances and appreciating its history not only enriches one's programming repertoire but also underscores the importance of creating lasting, impactful solutions in the tech world.

FAQs

  1. Why hasn't COBOL been replaced by modern languages in many legacy systems?

COBOL systems haven't been replaced as they have proven their reliability over decades and are perfect for handling critical data. Plus, migrating to a new system is costly and risky.

  1. How does COBOL handle data storage differently from other modern languages?

COBOL is helpful in business data processing. It uses a detailed data structure definition using the PICTURE clause and hierarchical data models, allowing for significant control over data format, size, and storage.

  1. What's the future of COBOL in the tech industry?

While COBOL might not be the go-to language for new applications, it's far from obsolete. COBOL programmers help maintain, update, and modernize existing systems, securing the language's lasting presence in the industry.

Leave a Reply

Your email address will not be published. Required fields are marked *