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:
- Data Division: Understanding how to define and use variables in COBOL.
- Simple Variables: Learning to create and manipulate alphabetic variables.
- More on Display: Using the
DISPLAYstatement to show variable values on the screen.
Here’s a glimpse of what your code will look like:
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:
-
Let's examine the first line:
01 Customer-Name PIC A(25).:- The
01is the level number, which indicates that this is the highest level of the variable hierarchy – we will discuss this in upcoming courses. Customer-Nameis the name of the variable.PIC A(25)specifies that this is an alphabetic variable with a length of 25 characters.
- The
-
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.
- Similar to
Notice, that COBOL supports dash - and underscore _ characters in variable names.
