Introduction to COBOL Basics

Welcome back! Now that you’ve written your first COBOL program and become familiar with its basic structure, it’s time to explore further. In this lesson, we'll dive into the Data Division and learn about defining simple variables in COBOL. Plus, we'll expand on using the DISPLAY statement to output more complex information.

What You'll Learn

In this lesson, we will focus on these key aspects:

  1. Data Division: Understanding how to define and use variables in COBOL.
  2. Simple Variables: Learning to create and manipulate alphabetic variables.
  3. More on Display: Using the DISPLAY statement to show variable values on the screen.

Here’s a glimpse of what your code will look like:

IDENTIFICATION DIVISION.
PROGRAM-ID. BankProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Customer-Name PIC A(25). *> Alphabetic variable with length 25
01 CustomerSupportName PIC A(25) VALUE 'Alice Smith'. *> Alphabetic variable with length 25 and initial value
PROCEDURE DIVISION.
    MOVE "John Doe" TO Customer-Name.
    DISPLAY "Customer Name: " Customer-Name.
    DISPLAY "Customer Support: " CustomerSupportName.
    DISPLAY Customer-Name " is supported by " CustomerSupportName.
    STOP RUN.
Understanding Data Division and Working-Storage Section

The Data Division is a critical part of a COBOL program where you define all the variables and data structures that your program will use. It provides a way to specify different types of data that will be used in the program, and it's divided into several sections, with the Working-Storage Section being the most commonly used.

The Working-Storage Section within the Data Division is where you declare variables that will retain their values for the duration of the program's execution. This section is essential for defining any temporary data your program needs to manipulate. For example, in the code snippet, Customer-Name and CustomerSupportName are declared in the Working-Storage Section.

Explanation of Variable Declarations

In the code snippet, you see the declaration of alphabetic variables in the Working-Storage Section:

01 Customer-Name PIC A(25). *> Alphabetic variable with length 25
01 CustomerSupportName PIC A(25) VALUE 'Alice Smith'. *> Alphabetic variable with length 25 and initial value
  • Let's examine the first line: 01 Customer-Name PIC A(25).:

    • The 01 is the level number, which indicates that this is the highest level of the variable hierarchy – we will discuss this in upcoming courses.
    • Customer-Name is the name of the variable.
    • PIC A(25) specifies that this is an alphabetic variable with a length of 25 characters.
  • Let's now check out the second line: 01 CustomerSupportName PIC A(25) VALUE 'Alice Smith'.:

    • Similar to Customer-Name, this variable also has a level number and a length of 25 characters.
    • The VALUE 'Alice Smith' clause initializes the variable with the string 'Alice Smith' when the program starts.

Notice, that COBOL supports dash - and underscore _ characters in variable names.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal