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.
In this lesson, we will focus on these key aspects:
DISPLAY
statement to show variable values on the screen.Here’s a glimpse of what your code will look like:
cobol1IDENTIFICATION 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.
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.
In the code snippet, you see the declaration of alphabetic variables in the Working-Storage Section:
cobol101 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).
:
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'.
:
Customer-Name
, this variable also has a level number and a length of 25 characters.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.
The MOVE
and DISPLAY
statements are used to assign values to variables and display them on the screen:
cobol1MOVE "John Doe" TO Customer-Name. 2DISPLAY "Customer Name: " Customer-Name. 3DISPLAY "Customer Support: " CustomerSupportName. 4DISPLAY Customer-Name " is supported by " CustomerSupportName.
Customer-Name
.Customer-Name
and CustomerSupportName
.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 text1Customer 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.
Understanding how to define and manipulate variables is crucial for any programming language, and COBOL is no exception. By mastering the Data Division and learning to work with simple variables:
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!