COBOL Programming in 2023: A Timeless Language Evolving with the Future

Post Stastics

  • This post has 3409 words.
  • Estimated read time is 16.23 minute(s).

Introduction:

COBOL, an acronym for Common Business-Oriented Language, continues to make its mark in the programming landscape even in the year 2023. Developed in the late 1950s, COBOL was specifically designed for business data processing, and its longevity is a testament to its enduring relevance in industries such as banking, finance, and government. In this article, we will explore the essence of COBOL, its historical significance, current popularity, and delve into key programming concepts, showcasing examples of program layout, data types, conditions, iteration structures, I/O operations, and data structures.

COBOL emerged during a time when businesses were transitioning from manual data processing to automated systems. Its primary objective was to create a programming language that could facilitate the efficient handling of large-scale business data. Developed by a committee of computer industry experts, COBOL emphasized readability, English-like syntax, and clear semantics, making it accessible to business professionals and programmers alike.

In its early days, COBOL gained widespread adoption, becoming one of the most popular programming languages. Its success was driven by the increasing need for business-oriented applications and the language’s ability to process vast amounts of data reliably. COBOL was instrumental in the development of critical systems that continue to support crucial operations even today.

Despite being viewed as a legacy language, COBOL has proven its resilience and adaptability. It continues to play a vital role in maintaining and modernizing existing systems, enabling businesses to leverage their investments in established infrastructure. Furthermore, the language has evolved over time, incorporating modern features and practices, making it a viable choice for new development projects as well.

COBOL Concepts

Let’s now dive into some essential programming concepts in COBOL:

Program Layout:

COBOL programs are divided into sections, such as IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE, and more.

Each section contains specific information, such as program name, input/output specifications, data declarations, and procedural logic.

Data Types:

COBOL supports various data types, including numeric (e.g., integer, decimal), alphanumeric (text), and date/time.

01 Employee-Id PIC 9(6).
01 Employee-Name PIC X(20).
01 Employee-Salary PIC 9(5)V99.

In the COBOL code example, we have three data fields representing employee information: Employee-Id, Employee-Name, and Employee-Salary. Let’s break down each field and its associated data type.

  1. Employee-Id:
    • PIC 9(6): This indicates that the Employee-Id field is a numeric field (Packed-Decimal format) with a length of 6 digits. The ‘9’ represents a single digit, and the ‘(6)’ denotes the number of digits the field can hold.
  2. Employee-Name:
    • PIC X(20): Here, the Employee-Name field is an alphanumeric (text) field with a length of 20 characters. The ‘X’ represents an alphanumeric character, and the ‘(20)’ specifies the maximum number of characters the field can accommodate.
  3. Employee-Salary:
    • PIC 9(5)V99: The Employee-Salary field is a numeric field (Packed-Decimal format) with a length of 7 digits and a scale of 2 decimal places. The ‘9’ indicates a single digit, the ‘(5)’ specifies the total number of digits (excluding the decimal), and the ‘V99’ denotes the decimal portion of the number.

In COBOL, the “PIC” (Picture Clause) is used to define the format and characteristics of data fields. It allows programmers to specify the data type, length, and any additional formatting requirements for each field.

It’s worth noting that the PIC clause in COBOL is quite versatile and provides various options for defining data types and formats, such as usage clauses (e.g., DISPLAY, COMP, COMP-3), editing clauses (e.g., Z, -, $), and more. These options allow for precise control over data representation and manipulation in COBOL programs.

In the given example, the data types chosen for each field reflect the expected characteristics of employee information. The Employee-Id is a numeric identifier, the Employee-Name is a textual field, and the Employee-Salary is a numerical value with a specific precision and scale.

By defining data fields with specific data types, COBOL enables programmers to handle and process data efficiently and accurately within their applications.

Conditions:

COBOL utilizes conditions for decision-making and control flow.

IF Employee-Salary > 5000 THEN
    PERFORM Process-High-Salary
ELSE
    PERFORM Process-Low-Salary
END-IF.

The example above, showcases a conditional statement using the IF-THEN-ELSE construct. Let’s discuss its functionality and execution flow:

In this code snippet, the condition being evaluated is “Employee-Salary > 5000”. The IF statement checks if the value stored in the variable “Employee-Salary” is greater than 5000.

If the condition evaluates to true, meaning the employee’s salary is indeed greater than 5000, the program will execute the statement “PERFORM Process-High-Salary”. This statement indicates that the program will call a subroutine or perform a specific set of instructions defined under the label “Process-High-Salary”. This subroutine or set of instructions will handle the processing required for employees with high salaries.

On the other hand, if the condition evaluates to false, indicating that the employee’s salary is not greater than 5000, the program will execute the statement “PERFORM Process-Low-Salary”. This statement indicates that the program will call a different subroutine or perform a different set of instructions defined under the label “Process-Low-Salary”. This subroutine or set of instructions will handle the processing required for employees with low salaries.

The ELSE keyword marks the beginning of the alternative block of code to be executed when the condition evaluates to false.

The END-IF statement marks the end of the conditional construct, enclosing the IF-THEN-ELSE logic.

Overall, this code demonstrates a branching logic structure in COBOL, allowing the program to make decisions based on the value of the “Employee-Salary” variable. By utilizing the IF-THEN-ELSE construct, different actions can be performed based on whether the condition is true or false, providing flexibility in handling different scenarios within the program’s execution flow.

Iteration Structures:

COBOL provides various looping structures, such as PERFORM loops and DO WHILE loops.

PERFORM UNTIL Counter > 10
    ADD 1 TO Counter
    DISPLAY "Count: " Counter
END-PERFORM.

This code demonstrates a looping construct using the PERFORM UNTIL statement. Let’s discuss its functionality and how it controls the execution flow:

In this code snippet, the loop begins with the PERFORM UNTIL statement, which specifies the condition for the loop to continue executing. The condition being evaluated is “Counter > 10”. The loop will continue as long as the value stored in the variable “Counter” is not greater than 10.

Inside the loop, two actions are performed sequentially:

  1. ADD 1 TO Counter: This statement increments the value of the variable “Counter” by 1 in each iteration of the loop. It ensures that the loop moves closer to the termination condition by gradually increasing the value of “Counter”.
  2. DISPLAY “Count: ” Counter: This statement displays a message along with the current value of the “Counter” variable. It outputs the value of “Counter” to the screen or other output device, providing visibility of the count during the loop execution.

After executing these actions, the control returns to the PERFORM UNTIL statement. The condition is re-evaluated, and if the value of “Counter” is still not greater than 10, the loop continues to execute. This process repeats until the termination condition is met.

Once the condition “Counter > 10” evaluates to true, indicating that the value of “Counter” has reached or exceeded 10, the loop terminates, and the program continues with the next instruction following the END-PERFORM statement.

This looping construct allows for repetitive execution of a set of instructions until a specific condition is met. In the provided code, the loop iterates until the value of “Counter” becomes greater than 10. During each iteration, the value of “Counter” is incremented by 1, and its current value is displayed. This construct provides a flexible and efficient way to perform repetitive tasks and control the program flow based on the condition being evaluated.

PERFORM UNTIL

PERFORM VARYING Counter FROM 1 BY 1 UNTIL Counter > 10
   DISPLAY "Counter: " Counter
END-PERFORM.

PERFORM VARYING

The PERFORM VARYING loop is used when you need to execute a block of code a specific number of times. It allows you to define a loop variable and specify its initial value, final value, and the increment/decrement value. The loop variable is automatically incremented or decremented with each iteration until the final value is reached.

PERFORM VARYING Counter FROM 1 BY 1 UNTIL Counter > 10
   DISPLAY "Counter: " Counter
END-PERFORM.

PERFORM WITH TEST

The PERFORM WITH TEST loop is used when you want to repeat a block of code until a specific condition is met. It checks the condition at the end of each iteration and continues executing the loop as long as the condition evaluates to true.

PERFORM UNTIL Condition-Met
   ...
   IF Counter > 10 THEN
      SET Condition-Met TO TRUE
   END-IF
END-PERFORM.

EVALUATE

The EVALUATE statement in COBOL provides a structured way to perform multiple tests and execute corresponding code blocks based on the evaluated result. It allows for branching based on the value of a given variable or condition.

EVALUATE Counter
   WHEN 1
      DISPLAY "Value is 1."
   WHEN 2
      DISPLAY "Value is 2."
   WHEN OTHER
      DISPLAY "Value is neither 1 nor 2."
END-EVALUATE.

These are just a few examples of looping structures available in COBOL. Each structure offers unique functionality and can be chosen based on the specific requirements of the program. COBOL’s rich set of looping constructs provides flexibility and control for executing code repetitively and managing program flow.

I/O Operations:

COBOL allows reading and writing data from/to files using predefined file structures.

READ Employee-File
    AT END
        DISPLAY "End of file reached."
    NOT AT END
        DISPLAY "Employee Name: " Employee-Name
END-READ.

This COBOL code demonstrates file handling, specifically reading data from a file. Let’s discuss its functionality and execution flow:

In this code snippet, the READ statement is used to read a record from the file named “Employee-File”. The READ statement is followed by two options: AT END and NOT AT END.

  • AT END: If the READ operation reaches the end of the file, meaning there are no more records to be read, the program executes the statements within the AT END block. In this case, the code displays the message “End of file reached.” This provides an indication that the file reading process has completed.
  • NOT AT END: If there are still more records to be read in the file, the program executes the statements within the NOT AT END block. In this case, the code displays the message “Employee Name: ” followed by the value of the variable “Employee-Name”. This allows the program to process and display the data from each record as it is read from the file.

The END-READ statement marks the end of the READ operation, indicating that the program has finished reading the current record and is ready to proceed to the next.

Overall, this code snippet demonstrates a common pattern for reading records from a file in COBOL. It handles the scenario where there are no more records to be read (AT END) and performs operations on each record that is successfully read (NOT AT END).

Reading and Writing

Now, let’s move on to the File Write example and an example of printing to the console and getting input from the console:

WRITE Employee-Record FROM Input-Data

In this code, the WRITE statement is used to write data from the “Input-Data” area into the “Employee-Record” data structure. This is typically followed by other statements to handle any necessary file operations, such as opening and closing the file.

The WRITE statement allows you to write data to a file, either as a new record or by replacing an existing record.

Console

DISPLAY "Enter your name: "
ACCEPT User-Name

In this example, the DISPLAY statement is used to print a message to the console, prompting the user to enter their name. The ACCEPT statement allows the program to receive input from the user and store it in the “User-Name” variable.This combination of DISPLAY and ACCEPT statements enables interactive communication between the program and the user, allowing for input and output operations through the console.

These examples demonstrate common operations in COBOL for file handling, printing to the console, and obtaining input from the console. COBOL provides specific statements and constructs to handle various input/output operations, enabling the program to interact with files and users effectively.

Data Structures:

COBOL supports data structures such as arrays and records for organizing and manipulating data.

01 Employee-Record.
    05 Employee-Name PIC X(20).
    05 Employee-Salary PIC 9(5

The provided COBOL code snippet represents the data structure for an “Employee-Record” in a COBOL program. Let’s discuss its structure and the data types used:

  1. Employee-Record:
  • The name “Employee-Record” signifies a data structure that holds information related to an employee.
  • The structure is defined using the 01 level number, which is the highest level in COBOL for declaring data items or records.
  • The structure contains two subordinate levels: “Employee-Name” and “Employee-Salary”.
  1. Employee-Name:
  • This field represents the employee’s name.
  • The “PIC X(20)” clause indicates that the field is an alphanumeric (text) field with a length of 20 characters.
  • The “PIC” clause, short for Picture Clause, is used to define the data type and characteristics of the field.
  1. Employee-Salary:
  • This field represents the employee’s salary.
  • The “PIC 9(5)” clause indicates that the field is a numeric field with a length of 5 digits.
  • The ‘9’ represents a single digit, and the ‘(5)’ denotes the number of digits the field can hold.
  • The absence of a decimal point or scaling factor implies that the salary is represented as a whole number.

By defining the “Employee-Record” data structure with its corresponding fields, COBOL allows programmers to organize and manipulate employee data efficiently. The alphanumeric field “Employee-Name” can store the name of the employee, while the numeric field “Employee-Salary” can store the salary information.

COBOL’s use of the Picture Clause (PIC) allows for precise control over data types and lengths, ensuring data integrity and appropriate formatting. The defined data structure can be used to store and process multiple employee records within a COBOL program.

This code snippet represents a foundational component for managing employee data in a COBOL program. It sets the structure and data types, providing a basis for storing and manipulating employee records throughout the program’s execution.

01 Scores.
   05 Test-Scores OCCURS 5 TIMES.
      10 Score PIC 99.

...

PERFORM VARYING Index FROM 1 BY 1 UNTIL Index > 5
   DISPLAY "Enter test score for student " Index ": "
   ACCEPT Test-Scores(Index)
END-PERFORM.

...

PERFORM VARYING Index FROM 1 BY 1 UNTIL Index > 5
   DISPLAY "Test score for student " Index ": " Test-Scores(Index)
END-PERFORM.

...

PERFORM VARYING Index FROM 1 BY 1 UNTIL Index > 5
   ADD 5 TO Test-Scores(Index)
END-PERFORM.

In this example, we have an array named “Test-Scores” that can hold five test scores for different students. Each score is defined as a two-digit numeric field.

To populate the array, we use a PERFORM VARYING loop to iterate over the array indices (Index) from 1 to 5. Within the loop, we display a prompt to enter the test score for each student using the current index (Index) and accept the user input, storing it in the corresponding array element Test-Scores(Index).

After populating the array, we use another PERFORM VARYING loop to access the data and display the test scores for each student. Similar to the previous loop, we display the test score for each student using the current index (Index) and access the corresponding array element Test-Scores(Index).

Finally, we update the test scores by adding 5 to each score. This is accomplished by looping over the array with the PERFORM VARYING construct and adding 5 to each array element Test-Scores(Index).

The use of an array in this example allows us to store and manipulate multiple test scores in a structured manner. By utilizing loops and array indexing, we can easily access, update, and display the test scores for different students.

Arrays in COBOL provide a powerful mechanism for organizing and processing data elements of the same type. They offer flexibility in handling data sets with a fixed number of elements, such as test scores, employee data, or any other data that can be represented in a tabular or indexed format.

Binary Search in COBOL:

Now let’s conclude our exploration with an implementation of the binary search algorithm in COBOL. This will give a real-world example:

IDENTIFICATION DIVISION.
PROGRAM-ID. Binary-Search.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 Numbers.
   05 Array OCCURS 10 TIMES.
      10 Value PIC 9(2).
01 Search-Key PIC 9(2).
01 Found-Flag PIC X(1) VALUE 'N'.
01 Low-Index PIC 9(2) VALUE 1.
01 High-Index PIC 9(2) VALUE 10.
01 Mid-Index PIC 9(2).

PROCEDURE DIVISION.
Begin-Program.
   DISPLAY "Enter 10 numbers in ascending order:".
   PERFORM Read-Array.
   DISPLAY "Enter a number to search:".
   ACCEPT Search-Key.
   PERFORM Binary-Search.
   IF Found-Flag = 'N'
      DISPLAY "Number not found."
   ELSE
      DISPLAY "Number found at index" Mid-Index.
   END-IF.
   STOP RUN.

Read-Array.
   PERFORM VARYING Low-Index FROM 1 BY 1 UNTIL Low-Index > 10
      ACCEPT Array(Low-Index).
   END-PERFORM.

Binary-Search.
   PERFORM UNTIL Found-Flag = 'Y' OR Low-Index > High-Index
      COMPUTE Mid-Index = (Low-Index + High-Index) / 2
      IF Array(Mid-Index) = Search-Key
         MOVE 'Y' TO Found-Flag
      ELSE IF Array(Mid-Index) > Search-Key
         MOVE Mid-Index - 1 TO High-Index
      ELSE
         MOVE Mid-Index + 1 TO Low-Index
      END-IF
   END-PERFORM.

The above code snippet implements the binary search algorithm. Let’s discuss its structure and functionality:

  1. Structure:
  • The code begins with the IDENTIFICATION DIVISION, where the program name is specified as “Binary-Search”.
  • The DATA DIVISION defines the data items used in the program, including the array “Numbers” that can hold 10 values, the search key “Search-Key”, and several control variables.
  • The PROCEDURE DIVISION contains the main logic of the program, starting with the Begin-Program paragraph.
  1. Execution Flow:
  • The program prompts the user to enter 10 numbers in ascending order and stores them in the “Numbers” array through the Read-Array paragraph.
  • Next, the program prompts the user to enter a number to search and accepts the input in the “Search-Key” variable.
  • The Binary-Search paragraph performs the binary search algorithm to find the position of the search key within the array.
  • The search algorithm compares the search key with the middle element of the array and adjusts the low and high indices accordingly until the key is found or the search range is exhausted.
  • If the search key is found, the program sets the “Found-Flag” to ‘Y’ and displays the index where the key was found. Otherwise, it displays a message indicating that the number was not found.
  • Finally, the program stops execution using the STOP RUN statement.
  1. Read-Array:
  • The Read-Array paragraph utilizes the PERFORM VARYING loop to accept 10 numbers from the user and store them in the “Numbers” array. The loop iterates from 1 to 10, with each number being stored in the respective array element.
  1. Binary-Search:
  • The Binary-Search paragraph performs the binary search algorithm by repeatedly dividing the search range in half until the search key is found or the range is exhausted.
  • The loop continues until the “Found-Flag” becomes ‘Y’ or the low index exceeds the high index.
  • The mid-index is computed as the average of the low and high indices.
  • The algorithm compares the search key with the element at the mid-index, adjusts the search range by updating the low or high index accordingly, and continues the loop until the key is found or the search range is exhausted.

This COBOL code demonstrates the implementation of the binary search algorithm to search for a specific number within a sorted array. It showcases various COBOL programming constructs such as loops, conditionals, arithmetic operations, and data manipulation. The binary search algorithm efficiently reduces the search range by half at each iteration, making it suitable for searching in sorted arrays.

Conclusion:

COBOL, the Common Business-Oriented Language, has stood the test of time and remains a valuable tool in modern programming. Its rich history and continued relevance in critical business applications illustrate its enduring strength. In this article, we explored the basics of COBOL programming, covering program layout, data types, conditions, iteration structures, I/O operations, and data structures.

While COBOL’s syntax and design may differ from more contemporary languages, its adaptability and evolution have ensured its continued usage and integration with modern development practices. The language’s ability to process large-scale business data and its seamless integration with existing systems make it an indispensable asset for organizations worldwide.

Resources:

To further enhance your COBOL journey, here are some current resources:

  • GnuCOBOL – The official website for GnuCOBOL, an open-source COBOL compiler and development environment.
  • Micro Focus COBOL – Micro Focus offers COBOL development tools, compilers, and modernization solutions.
  • COBOL-IT – COBOL-IT provides COBOL compilers, tools, and support services for modernizing COBOL applications.
  • COBOL Cafe – A community-driven website dedicated to COBOL programming, offering forums, resources, and discussions.
  • Open Mainframe Project – The Open Mainframe Project hosts COBOL-related resources, including tools, documentation, and collaborative projects.

These resources provide a range of information and tools for COBOL programming, including compilers, development environments, community support, and modernization solutions. They can serve as valuable references for COBOL developers, offering opportunities for learning, collaboration, and staying up to date with the latest advancements in the COBOL ecosystem.

With the knowledge gained from this article, you are well-equipped to dive deeper into COBOL programming, unlocking the immense potential of this venerable language and contributing to its ongoing legacy in the ever-evolving world of technology.

Leave a Reply

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