<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stephen Johnston</title>
    <description>The latest articles on DEV Community by Stephen Johnston (@stephenkjohnston).</description>
    <link>https://dev.to/stephenkjohnston</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F235889%2Fb341e2fa-6537-4b15-896b-98acb9e5d4e2.png</url>
      <title>DEV Community: Stephen Johnston</title>
      <link>https://dev.to/stephenkjohnston</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stephenkjohnston"/>
    <language>en</language>
    <item>
      <title>COBOL Fundamentals: Data Types &amp; Variables</title>
      <dc:creator>Stephen Johnston</dc:creator>
      <pubDate>Sat, 22 Feb 2025 01:05:58 +0000</pubDate>
      <link>https://dev.to/stephenkjohnston/cobol-fundamentals-data-types-variables-180d</link>
      <guid>https://dev.to/stephenkjohnston/cobol-fundamentals-data-types-variables-180d</guid>
      <description>&lt;h2&gt;
  
  
  Data Types in COBOL
&lt;/h2&gt;

&lt;p&gt;The COBOL standard categorizes data based on its purpose into five distinct types. These are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Alphabetic&lt;/strong&gt; - Used when you need to include letters (e.g., A-Z, a-z). While it might seem ideal for storing names, you should be cautious, as names can contain non-alphabetic characters. This data type can also include spaces.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alphanumeric&lt;/strong&gt; - Used when you need to include letters, numbers, and special characters (e.g., A-Z, 0-9, %, #). This type is most commonly used for storing names, coupon codes, or other mixed-type information.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Numeric&lt;/strong&gt; - Used for numbers (e.g., 0-9). This data type is designed for arithmetic operations and calculations, making it essential for financial, scientific, and statistical applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alphanumeric Edited&lt;/strong&gt; - Used to format alphanumeric data for display purposes. This type allows for the insertion of fixed characters (e.g., hyphens, slashes, or spaces) into alphanumeric data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Numeric Edited&lt;/strong&gt; - Used to format numeric data for display purposes. This type allows for the insertion of fixed characters (e.g., currency symbols, commas, or decimal points) into numeric data.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  A Note on Edited Data Types 🤦‍♂️
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Alphanumeric Edited&lt;/strong&gt; and &lt;strong&gt;Numeric Edited&lt;/strong&gt; data types can be a bit tricky at first — but simply put, it's COBOL's way of saying, "Let's make this data look fancy!" They're designed for formatting data for display, like adding commas to numbers, currency symbols, or even hyphens in text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables in COBOL
&lt;/h2&gt;

&lt;p&gt;In COBOL, all variables are defined in the &lt;code&gt;DATA DIVISION&lt;/code&gt;, which is where your COBOL program defines and stores all the data it will use. COBOL variables are hierarchical and are made up of level numbers, &lt;code&gt;PICTURE&lt;/code&gt; (or &lt;code&gt;PIC&lt;/code&gt;) clauses, and optional default values (more on this later):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;strong&gt;LEVEL-NUMBER&lt;/strong&gt;, which determines if it's an individual field, group item, or elementary item. So, what's the difference? They are:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;01&lt;/strong&gt; - Indicates a top-level item. It can be standalone or the start of a group item. Fun fact: anything starting with a zero is pronounced "OH" followed by the level number (e.g., OH-ONE, OH-TWO).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;02 - 49&lt;/strong&gt; - Indicates subordinate levels. These are used within groups to break down data into smaller, more manageable chunks — think of an &lt;strong&gt;OBJECT&lt;/strong&gt; in modern programming languages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;66&lt;/strong&gt; - Used for what's known as RENAMES. I've not used this yet, but it seems similar to creating a view in SQL, allowing you to create an alternative view of the same data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;77&lt;/strong&gt; - Indicates a standalone item that doesn't need to be part of a group.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;88&lt;/strong&gt; - Most often used for condition names. They're incredibly useful for making your code more readable. The closest comparison might be an ENUM in modern languages, though you can't access it like one (e.g., ORDER-STATUS.NEW-ORDER). Instead, you use the condition name in logical statements.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;VARIABLE NAME&lt;/strong&gt;, the name we'll use to access this item later in the program. Like in every programming language, it has some rules — one of which goes against everything I was taught about variables 😀. These rules are:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Length&lt;/strong&gt; - A variable name can be as short as 1 character (please don't do this 😱—it's terrible for readability!!) and as long as 30 characters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Allowed Characters&lt;/strong&gt; - Pretty standard stuff here: letters (A-Z, a-z), numbers (0-9), and hyphens (-).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Starting &amp;amp; Ending Characters&lt;/strong&gt; - In COBOL, they can start and end with a letter or a number — that's right, a number! However, you cannot start or end with a hyphen. At least they put some restrictions on the names 🤣.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Reserved Words&lt;/strong&gt; - This one makes sense. Don't use reserved words (e.g., DISPLAY, ACCEPT, MOVE).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;PICTURE (or PIC) Clause&lt;/strong&gt; - This is where the magic happens. It defines the type, size, and display format of a variable using symbols (e.g., A, 9, X). Formatting is one of COBOL's neatest tricks. I can't tell you how many times I've pulled my hair out trying to make something look a certain way when it's displayed. Well, COBOL does the heavy lifting for you — once you wrap your head around how it works! We'll dive deeper into this shortly.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;VALUE Clause&lt;/strong&gt; - This is where you can set an initial value for your variable. It's optional.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  A PICTURE Is Worth A Thousand Words
&lt;/h3&gt;

&lt;p&gt;As mentioned earlier, the &lt;strong&gt;PICTURE&lt;/strong&gt; clause allows you to define the structure of your data and format it in a way that makes you happy. While you might find references to the full keyword &lt;strong&gt;PICTURE&lt;/strong&gt; in older code, most examples today use the abbreviated &lt;strong&gt;PIC&lt;/strong&gt;. It really comes down to personal preference or your company's programming style guide. The TL;DR on the PICTURE clause is that it's a blueprint for how your data should appear.&lt;/p&gt;

&lt;p&gt;Like any blueprint, the &lt;strong&gt;PICTURE&lt;/strong&gt; clause uses special symbols that might make you go "HUH!" at first glance. I'll do my best to explain them, and while there are more than listed here, I'm focusing on the most common ones. With symbols, you can either use a single character and specify the length in parentheses (e.g., &lt;code&gt;PIC 9(5)&lt;/code&gt;), or write it out longhand like &lt;code&gt;PIC 99999&lt;/code&gt;. I don't recommend the long route, though — it's just more typing!&lt;/p&gt;

&lt;h4&gt;
  
  
  Common Symbols
&lt;/h4&gt;

&lt;p&gt;The following are probably the most commonly used symbols based on what I've learned so far.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;9&lt;/strong&gt; - This symbol is pretty self-explanatory and represents a numeric field. Example: &lt;code&gt;PIC 9(5)&lt;/code&gt; defines a 5-digit numeric field.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt; - This represents an alphabetic field. Example: &lt;code&gt;PIC A(10)&lt;/code&gt; defines a 10-character field that can only contain alphabetic characters (A-Z). Note that in practice, &lt;code&gt;PIC X&lt;/code&gt; is often preferred over &lt;code&gt;PIC A&lt;/code&gt; because it's more flexible, allowing alphanumeric characters (letters, numbers, and special characters).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;X&lt;/strong&gt; - This represents an alphanumeric field. Example: &lt;code&gt;PIC X(10)&lt;/code&gt; defines a 10-character alphanumeric field.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;V&lt;/strong&gt; - This is where things get interesting. It represents an implied decimal point. Example: &lt;code&gt;PIC 9(5)V99&lt;/code&gt; defines a 7-digit numeric field with two decimal places.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;S&lt;/strong&gt; - This indicates a signed number (e.g., -5.00, +5.00). I've not used this one yet.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Assigning Values to Variables
&lt;/h2&gt;

&lt;p&gt;In COBOL, you can assign values to variables using the &lt;strong&gt;MOVE&lt;/strong&gt; statement. This is one of the most common ways to initialize or update variables in your program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Examples
&lt;/h2&gt;

&lt;p&gt;If you're like me, reading about something is fine and dandy, but seeing the code is more helpful. That said, let's dive into some code examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Assigning a Value to a Numeric Field
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       IDENTIFICATION DIVISION.
       PROGRAM-ID. EXP01.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 SALARY PIC 9(5).
       PROCEDURE DIVISION.
         MOVE 50000 TO SALARY.
         DISPLAY "Salary: " SALARY.
         STOP RUN.
       END PROGRAM EXP01.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Salary: 50000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 2: Assigning a Value to an Alphanumeric Field
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       IDENTIFICATION DIVISION.
       PROGRAM-ID. EXP02.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 EMPLOYEE-NAME PIC X(20).
        PROCEDURE DIVISION.
           MOVE "John Doe" TO EMPLOYEE-NAME.
           DISPLAY "Employee Name: " EMPLOYEE-NAME.
           STOP RUN.
       END PROGRAM EXP02.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Employee Name: John Doe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 3: Assigning a Value to a Grouped Field
&lt;/h3&gt;

&lt;p&gt;For this example we're going to go over a couple ways to accomplish the task at hand. In COBOL, you can assign values to a group field directly with &lt;code&gt;MOVE&lt;/code&gt;, but you need to account for the full length of subfields, or you can move data directly into the group using MOVE ... TO ... IN. In larger COBOL applications, you might encounter multiple groups with fields of the same name (e.g., EMPLOYEE-NAME in different groups). Using MOVE ... TO IN ensures values are assigned to the correct field, avoiding potential bugs or confusion. You can also use &lt;code&gt;OF&lt;/code&gt; instead of &lt;code&gt;IN&lt;/code&gt; as a synonym when referencing subfields — e.g., &lt;code&gt;NAME OF EMPLOYEE-DETAILS&lt;/code&gt;. Let's take a look at a couple examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MOVE ... TO&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This pair of statements allows you to assign values to individual fields within a group.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       IDENTIFICATION DIVISION.
       PROGRAM-ID. EXP03A.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 EMPLOYEE-DETAILS.
          05 EMPLOYEE-NAME PIC X(20).
          05 EMPLOYEE-ID   PIC 9(5).
       PROCEDURE DIVISION.
           MOVE "Jane Smith     56789" TO EMPLOYEE-DETAILS.
           DISPLAY "Employee Name: " EMPLOYEE-NAME.
           DISPLAY "Employee ID: " EMPLOYEE-ID.
           STOP RUN.
       END PROGRAM EXP03A.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the string &lt;code&gt;"Jane Smith     56789"&lt;/code&gt; is padded with spaces to match the 20-character length of &lt;code&gt;EMPLOYEE-NAME&lt;/code&gt;. The first 20 characters fill &lt;code&gt;EMPLOYEE-NAME&lt;/code&gt;, and &lt;code&gt;"56789"&lt;/code&gt; goes to &lt;code&gt;EMPLOYEE-ID&lt;/code&gt;. Without padding, some compilers might space-fill &lt;code&gt;EMPLOYEE-NAME&lt;/code&gt; and leave &lt;code&gt;EMPLOYEE-ID&lt;/code&gt; undefined or zeroed out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MOVE ... TO ... IN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This group of statements allows you to move data directly into the group, even if they share similar names. While I won't say never, I will say it shouldn't happen within smaller COBOL program, but if you work in a larger COBOL program you're probably going to see this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       IDENTIFICATION DIVISION.
       PROGRAM-ID. MULTIPLE-GROUPS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 EMPLOYEE-DETAILS.
          05 NAME PIC X(20).
       01 MANAGER-DETAILS.
          05 NAME PIC X(20).
       PROCEDURE DIVISION.
      *&amp;gt; Assigning values to fields in EMPLOYEE-DETAILS
           MOVE "John Doe" TO NAME IN EMPLOYEE-DETAILS.
           DISPLAY "Employee Name: " NAME IN EMPLOYEE-DETAILS.

      *&amp;gt; Assigning values to fields in MANAGER-DETAILS
           MOVE "Jane Smith" TO NAME IN MANAGER-DETAILS.
           DISPLAY "Manager Name: " NAME IN MANAGER-DETAILS.

           STOP RUN.
       END PROGRAM MULTIPLE-GROUPS.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;MOVE ... TO ... OF&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's the same example using OF instead of IN, showing they work interchangeably:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       IDENTIFICATION DIVISION.
       PROGRAM-ID. MULTIPLE-GROUPS-OF.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 EMPLOYEE-DETAILS.
          05 NAME PIC X(20).
       01 MANAGER-DETAILS.
          05 NAME PIC X(20).
       PROCEDURE DIVISION.
           MOVE "John Doe" TO NAME OF EMPLOYEE-DETAILS.
           DISPLAY "Employee Name: " NAME OF EMPLOYEE-DETAILS.
           MOVE "Jane Smith" TO NAME OF MANAGER-DETAILS.
           DISPLAY "Manager Name: " NAME OF MANAGER-DETAILS.
           STOP RUN.
       END PROGRAM MULTIPLE-GROUPS-OF.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 4: Dates
&lt;/h3&gt;

&lt;p&gt;Another common example we'd find in a business application might be a date. Now, this is one place where COBOL, at least in all my research, seems to be lacking. COBOL doesn't natively support a dedicated &lt;strong&gt;DATETIME&lt;/strong&gt; field type, so we need to handle dates ourselves. For consistency, I recommend storing dates in ISO format (YYYYMMDD) — it's unambiguous and sorts easily in files. Here are two approaches:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Date as Grouped Field&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want to store a date in a structured way, you can use a grouped field to break it down into its components (year, month, and day). Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       IDENTIFICATION DIVISION.
       PROGRAM-ID. CBLDATES1.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 BIRTH-DATE.
          02 BIRTH-YEAR   PIC 9(4).
          02 BIRTH-MONTH  PIC 9(2).
          02 BIRTH-DAY    PIC 9(2).
       PROCEDURE DIVISION.
           MOVE 2025 TO BIRTH-YEAR
           MOVE 02   TO BIRTH-MONTH
           MOVE 21   TO BIRTH-DAY
           DISPLAY "Year: " BIRTH-YEAR
           DISPLAY "Month: " BIRTH-MONTH
           DISPLAY "Day: " BIRTH-DAY
           STOP RUN.
       END PROGRAM CBLDATES1.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Date as Numeric Field (ISO Format)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using YYYYMMDD (e.g., 19850715 for July 15, 1985) avoids confusion — unlike MMDDYYYY or DDMMYYYY, where 07012025 could be July 1st or January 7th depending on the region.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       IDENTIFICATION DIVISION.
       PROGRAM-ID. CBLDATES2.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 BIRTH-DATE PIC 9(8).
       PROCEDURE DIVISION.
           MOVE 19850715 TO BIRTH-DATE
           DISPLAY "Birth Date (YYYYMMDD): " BIRTH-DATE
           STOP RUN.
       END PROGRAM CBLDATES2.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Birth Date (YYYYMMDD): 19850715
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Date as Individual Edited Field&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want to display a date with slashes (e.g., YYYY/MM/DD) from an ISO format input, you can use an Alphanumeric Edited field. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       IDENTIFICATION DIVISION.
       PROGRAM-ID. CBLDATES3.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 BIRTH-DATE PIC 9999/99/99.
       PROCEDURE DIVISION.
           MOVE 19850815 TO BIRTH-DATE
           DISPLAY "Birth Date: " BIRTH-DATE
           STOP RUN.
       END PROGRAM CBLDATES3.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the PIC 9999/99/99 clause formats the date with slashes for display. The raw numeric value 19850815 is transformed into 1985/08/15.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Birth Date: 1985/08/15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we explored the fundamentals of COBOL data types and variables, including how to define and format them using PICTURE clauses. We also looked at practical examples of assigning values to variables and formatting data for display. By mastering these concepts, you'll be well-equipped to handle data effectively in COBOL programs, whether you're working with numeric calculations, alphanumeric strings, or formatted displays.&lt;/p&gt;

&lt;p&gt;If you're an experienced COBOL developer and spot any inaccuracies or areas where I could improve my explanations, I'd greatly appreciate your feedback! And if you're new to COBOL but found this article interesting, I'd love to hear from you as well. Your thoughts and questions are always welcome—I enjoy learning and growing alongside others in this journey!&lt;/p&gt;

&lt;h2&gt;
  
  
  Acknowledgments
&lt;/h2&gt;

&lt;p&gt;Big thanks to &lt;code&gt;harrywwc&lt;/code&gt;, a knowledgeable COBOL enthusiast who provided invaluable feedback on this article, helping me refine the details and make it more accurate. Your insights were spot on!&lt;/p&gt;

</description>
      <category>cobol</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Introduction to COBOL</title>
      <dc:creator>Stephen Johnston</dc:creator>
      <pubDate>Thu, 20 Feb 2025 03:40:36 +0000</pubDate>
      <link>https://dev.to/stephenkjohnston/cobol-an-introduction-4eno</link>
      <guid>https://dev.to/stephenkjohnston/cobol-an-introduction-4eno</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;As businesses began relying more heavily on computers for data processing—such as payroll, inventory, banking, and more—in the 1950s, it became clear that change was needed. At the time, each computer manufacturer used its own proprietary programming language, which was often tied to specific hardware. For instance, languages like FORTRAN and Assembly required a strong mathematical background, making them inaccessible to non-technical users.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Recognizing the inefficiency and cost of porting programs across systems, the U.S. Department of Defense (DoD) spearheaded efforts to create a standardized programming language that could be used universally. With this goal in mind, a group of experts gathered at the Conference on Data Systems Languages (CODASYL) in 1959. Among them was &lt;a href="https://en.wikipedia.org/wiki/Grace_Hopper" rel="noopener noreferrer"&gt;Grace Hopper&lt;/a&gt;, a pioneering computer scientist who was a driving force behind the development of the first compiler, playing a pivotal role in its creation alongside other contributors.&lt;/p&gt;

&lt;p&gt;Out of this conference came the COmmon Business-Oriented Language (COBOL), a revolutionary language designed to meet the needs of business data processing. Among the features that made COBOL groundbreaking was its English-like syntax, which allowed non-technical people to read, understand, and even write programs. This was similar to how modern drag-and-drop website builders have democratized—or, some might argue, disrupted—web development today.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Structure of a COBOL program
&lt;/h2&gt;

&lt;p&gt;Now that we've learned a bit about COBOL and some of the problems it solved, we'll take a look at the structure of a COBOL program.&lt;/p&gt;

&lt;h3&gt;
  
  
  Columns in COBOL
&lt;/h3&gt;

&lt;p&gt;Since COBOL dates back to the era of punched cards—where data was physically encoded on cards—it's no surprise that its code is organized into specific column-based areas, each serving a unique purpose.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Columns 1-6 - known as the Sequence Number Area, are often ignored in modern COBOL implementations.&lt;/li&gt;
&lt;li&gt;Column 7 - Column 7, known as the Indicator Area, is primarily used for comments (marked with *) or left blank for regular code lines in modern COBOL programming.&lt;/li&gt;
&lt;li&gt;Columns 8-11: Area A (Margin A) - Used for division headers, section headers, paragraph names, and level indicators.&lt;/li&gt;
&lt;li&gt;Columns 12-72: Area B (Margin B) - Contains the actual COBOL statements, data definitions, and procedural code.&lt;/li&gt;
&lt;li&gt;Columns 73-80 - known as the Identification Area, are rarely used in modern COBOL programming.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Structure of a COBOL program
&lt;/h3&gt;

&lt;p&gt;Because COBOL is English-like, perhaps it comes as no surprise that a COBOL program is structured much like a document, with divisions, sections, paragraphs, sentences, and statements. Let's explore these a little more.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Divisions: Major segments of a COBOL program that categorize different aspects like program identification, environment setup, data definitions, and procedural logic.&lt;/li&gt;
&lt;li&gt;Sections: Group related functionality within divisions.&lt;/li&gt;
&lt;li&gt;Paragraphs: Small blocks of code within sections for specific tasks.&lt;/li&gt;
&lt;li&gt;Sentences: Groups of statements that perform operations within paragraphs.&lt;/li&gt;
&lt;li&gt;Statements: Individual commands or instructions within sentences.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Four Divisions of a COBOL program
&lt;/h3&gt;

&lt;p&gt;Every COBOL program consists of four divisions; however, only two are required to write a basic COBOL program: the IDENTIFICATION DIVISION and the PROCEDURE DIVISION.&lt;/p&gt;

&lt;h4&gt;
  
  
  IDENTIFICATION DIVISION
&lt;/h4&gt;

&lt;p&gt;The IDENTIFICATION DIVISION is mandatory for every COBOL program. Without it, the program is invalid. It provides a place to store some valuable metadata, such as the program name, author, and other details. Within this division, the PROGRAM-ID paragraph must appear directly after the IDENTIFICATION DIVISION and is the only required paragraph. Other optional paragraphs include AUTHOR, DATE-WRITTEN, DATE-COMPILED, and SECURITY.&lt;/p&gt;

&lt;h4&gt;
  
  
  ENVIRONMENT DIVISION
&lt;/h4&gt;

&lt;p&gt;This division, which follows the IDENTIFICATION DIVISION, describes the system environment where the program will run, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The computer hardware and operating system.&lt;/li&gt;
&lt;li&gt;Any I/O devices (e.g., printers, disk drives, etc.) the program will use.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The ENVIRONMENT DIVISION is divided into two sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CONFIGURATION SECTION - Describes the system environment.&lt;/li&gt;
&lt;li&gt;INPUT-OUTPUT SECTION - Defines files and I/O devices, controlling how data is read from or written to these resources.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  DATA DIVISION
&lt;/h4&gt;

&lt;p&gt;This is where your COBOL program defines and stores all the data it will use. It's like the program's "storage room" for variables, records, and files.&lt;/p&gt;

&lt;p&gt;The DATA DIVISION is divided into the following sections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;FILE SECTION - Defines the structure of files used for input/output.&lt;/li&gt;
&lt;li&gt;WORKING-STORAGE SECTION - Stores temporary data and variables used during program execution.&lt;/li&gt;
&lt;li&gt;LINKAGE SECTION - Defines data shared between programs (used in subprograms).&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  PROCEDURE DIVISION
&lt;/h4&gt;

&lt;p&gt;The PROCEDURE DIVISION is mandatory for any COBOL program, as it contains the logic and operations that bring the program to life. This is where you define the logic, operations, and all the processing that will occur happens.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Basic COBOL Program
&lt;/h3&gt;

&lt;p&gt;If you have prior programming experience, this program will look familiar. It's the basic 'Hello World' program.&lt;/p&gt;

&lt;p&gt;We start by declaring our IDENTIFICATION DIVISION followed by a PROGRAM-ID, which is the only required paragraph in this division.&lt;/p&gt;

&lt;p&gt;Next, we declare the PROCEDURE DIVISION, which is where the action happens. To display output, we use the DISPLAY statement, which writes to the output device. While this is typically a terminal screen today, it was often a printer in the past.&lt;/p&gt;

&lt;p&gt;Finally, we instruct COBOL that immediately after it displays Hello World, we want to stop everything, and that's done with the STOP RUN statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      *******************************************************************
      *                   IDENTIFICATION DIVISION                       *
      *******************************************************************
      * The IDENTIFICATION DIVISION provides metadata about our program,*
      * and is one of two mandatory DIVISIONS in COBOL. The PROGRAM-ID  *
      * is the only required entry within this DIVISION.                *
      *******************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLOWORLDPROGRAM.

      *******************************************************************
      *                     PROCEDURE DIVISION                          *
      *******************************************************************
      * The PROCEDURE DIVISION is where the program's logic is written. *
      *******************************************************************
       PROCEDURE DIVISION.

      * The DISPLAY statement prints the contents to the output device,
      * in this case, the console window.
           DISPLAY 'Hello, World'.

      * The STOP RUN statement ends the program
           STOP RUN.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, I've described the history and reasoning behind COBOL, the structure of COBOL, and shown you how to write a basic program using COBOL.&lt;/p&gt;

&lt;p&gt;As I continue to learn COBOL, I welcome any feedback or corrections. Please let me know, and I'll update the article accordingly. &lt;/p&gt;

&lt;h2&gt;
  
  
  Acknowledgments
&lt;/h2&gt;

&lt;p&gt;Big thanks to &lt;code&gt;harrywwc&lt;/code&gt;, a knowledgeable COBOL enthusiast who provided invaluable feedback on this article, helping me refine the details and make it more accurate. Your insights were spot on!&lt;/p&gt;

</description>
      <category>cobol</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
