Lesson 2
COBOL Basics - Data Division and Simple Variables. More on Display
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:

cobol
1IDENTIFICATION DIVISION. 2PROGRAM-ID. BankProgram. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 501 Customer-Name PIC A(25). *> Alphabetic variable with length 25 601 CustomerSupportName PIC A(25) VALUE 'Alice Smith'. *> Alphabetic variable with length 25 and initial value 7PROCEDURE DIVISION. 8 MOVE "John Doe" TO Customer-Name. 9 DISPLAY "Customer Name: " Customer-Name. 10 DISPLAY "Customer Support: " CustomerSupportName. 11 DISPLAY Customer-Name " is supported by " CustomerSupportName. 12 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:

cobol
101 Customer-Name PIC A(25). *> Alphabetic variable with length 25 201 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.

Using MOVE and DISPLAY Statements

The MOVE and DISPLAY statements are used to assign values to variables and display them on the screen:

cobol
1MOVE "John Doe" TO Customer-Name. 2DISPLAY "Customer Name: " Customer-Name. 3DISPLAY "Customer Support: " CustomerSupportName. 4DISPLAY Customer-Name " is supported by " CustomerSupportName.
  • First we use MOVE to assign the value "John Doe" to the variable Customer-Name.
  • Then we use the DISPLAY statement to output the values of Customer-Name and CustomerSupportName.
  • Finally, we use the DISPLAY statement to combine the values of Customer-Name and CustomerSupportName with a string, creating a more complex and comprehensive output.

When the above code is executed, the output will be:

Plain text
1Customer Name: John Doe 2Customer Support: Alice Smith 3John Doe is supported by Alice Smith

This demonstrates how you can manipulate and display variable values in COBOL programs.

Notice, that there are spaces after the name 'John Doe', which is due to the fixed length of the Customer-Name variable. When the value is shorter than the defined length, COBOL pads it with spaces.

Why It Matters

Understanding how to define and manipulate variables is crucial for any programming language, and COBOL is no exception. By mastering these concepts:

  • You'll be able to create and manage data effectively, which is essential for building any program.
  • You'll learn to provide more detailed and useful output, which is vital for user interaction and debugging.

Grasping these concepts is essential for progressing to more advanced topics like arithmetic operations and complex data structures. Ready to take a deeper dive into COBOL's capabilities? Let's head to the practice section and get started!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.