| Introduction
|
Aims
| The aim of this unit is give you an understanding of the different categories of data used in a COBOL program and to demonstrate how items of each category may be created and used.
|
Objectives
| By the end of this unit you should -
|
Prerequisites
| Introduction to COBOL. but more information on declaring data items in COBOL can be found in the units covering
|
Further reading
| More information on declaring data items in COBOL can be found in the units covering -
|
| Categories of COBOL data
|
Introduction
| There are three categories of data item used in COBOL programs:
|
Variables
| A data-name or identifier is the name used to identify the area of memory reserved for a variable. A variable is a named location in memory into which a program can put data, and from which it can retrieve data. Every variable used in a COBOL program must be described in the DATA DIVISION. In addition to the data-name, a variable declaration also defines the type of data to be stored in the variable. This is known as the variable's data type.
|
Variable Data types
| Some languages like Modula-2,Pascal or Ada are described as being strongly typed. In these languages there are a large number of different data types and the distinction between them is rigorously enforced by the compiler. For instance, the compiler will reject a statement that attempts to assign character value to an integer data item. In COBOL, there are really only three data types -
The distinction between these data types is a little blurred and only weakly enforced by the compiler. For instance, it is perfectly possible to assign a non-numeric value to a data item that has been declared to be numeric. The problem with this lax approach to data typing is that, since COBOL programs crash (halt unexpectedly) if they attempt to do computations on items that contain non-numeric data, it is up to the programmer to make sure this never happens. COBOL programmers must make sure that non-numeric data is never assigned to numeric items intended for use in calculations. Programmers who use strongly typed languages don't need this level of discipline because the compiler ensures that a variable of a particular types can only be assigned appropriate values.
|
Literals
| A literal is a data-item that consists only of the data-item value itself. It cannot be referred to by a name. By definition, literals are constant data-items. There are two types of literal -
|
String Literals
| String/Alphanumeric literals are enclosed in quotes and consist of alphanumeric characters. For example: "Michael Ryan", "-123", "123.45"
|
Numeric Literals
| Numeric literals may consist of numerals, the decimal point, and the plus or minus sign. Numeric literals are not enclosed in quotes. For example: 123, 123.45, -256, +2987
|
Figurative Constants |
Unlike most other programming languages COBOL does not provide a mechanism for creating user-defined constants but it does provide a set of special constants called Figurative Constants. A Figurative Constant may be used wherever it is legal to use a literal but unlike literals, when a Figurative Constant is assigned to a data-item it fills the whole item overwriting everything in it. The Figurative Constants are:
Figurative Constant Notes
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Using COBOLdata |
The animated program fragments below demonstrate how variables, literals and Figurative Constants may be created and used.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Declaring Data-Items in COBOL | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Introduction |
Because COBOL is not a typed language like Modula-2 or C it employs a different mechanism for describing the characteristics of the data-items in a program. Rather than using types, as these languages do, COBOL uses a kind of "declaration by example" strategy. The programmer provides the system with an example, or template, or PICture of the storage required for the data-item. In COBOL, a variable declaration consists of a line in the DATA DIVISION that contains the following items:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
COBOL picture clauses |
To create the required 'picture' the programmer uses a set of symbols. The most common symbols used in standard picture clauses are:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Picture clause notes |
Although the word PICTURE can be used when defining a picture clause it is normal to use the abbreviation PIC. Recurring symbols may be specified by using a 'repeat' factor inside brackets. For instance: PIC 9(6) is equivalent to PICTURE 999999 PIC 9(6)V99 is equivalent to PIC 999999V99 PICTURE X(10) is equivalent to PIC XXXXXXXXXX PIC S9(4)V9(4) is equivalent to PIC S9999V9999 PIC 9(18) is equivalent to PIC 999999999999999999 Numeric values can have a maximum of 18 (eighteen) digits. The limit on string values is usually system dependent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Group and Elementary data-items | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Introduction |
Although we stated above that each variable declaration consists of a level number, an identifying name and a picture clause, that definition only applies to elementary data-items. Group items are defined using only a level-number and an identifying name; no picture clause is required or allowed. Which begs the question - what is a group item and what is an elementary item? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Elementary items |
An "elementary item" is the name we use in COBOL to describe a data-item that has not been further subdivided. Other languages might describe these as ordinary variables. Elementary items must have a picture clause because they actually reserve the storage required for the item. The amount of storage reserved is specified by the item's picture clause. An elementary item declaration consists of;
A starting value may be assigned to a variable by means of an extension to the PICTURE clause called the VALUE clause. Some examples: 01 GrossPay PIC 9(5)V99 VALUE ZEROS. 01 NetPay PIC 9(5)V99 VALUE ZEROS. 01 CustomerName PIC X(20) VALUE SPACES. 01 CustDiscount PIC V99 VALUE .25. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Group items |
Sometimes when we are manipulating data it is convenient to treat a collection of elementary items as a single group. For instance, we may want to group the data-items YearofBirth, MonthofBirth, DayOfBirth under the group name - DateOfBirth. If we are recording information about students we may want to subdivide StudentName into FirstName, MiddleInitial and Surname. And we may want to use both these group items and the elementary items StudentId and CourseCode in a student record description. We can create groups like these in COBOL using group items. A "group item" is the term used in COBOL to describe a data-item - like DateOfBirth or StudentName - that has been further subdivided. In other languages group items might be described as "structures". A group item consists of subordinate items. The items subordinate to a group item may be elementary items or other group items. But ultimately every group item must be defined in terms of its subordinate elementary items. In a group item, the hierarchical relationship between the various subordinate items of the group is expressed using level numbers. The higher the level number, the lower the item is in the hierarchy. Where a group item is the highest item in a data hierarchy it is referred to as a "record" and uses the level number 01. Group items are declared using a level number and a data name only. A group item cannot have a picture clause because it does not actually reserve any storage. It is merely a name given to a collection of (ultimately) elementary items which do reserve the storage. Therefore, the size of a group item is the sum of the sizes of its subordinate elementary items. The type of a group item is always assumed to be PIC X because a group item may have several different data items and types subordinate to it and an X picture is the only one which could support such collections. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Level Numbers |
Level numbers are used to express data hierarchy. The higher the level number, the lower the item is in the hierarchy. At the lowest level the data is completely atomic. What is important in a structure defined with level numbers is the relationship of the level numbers to one another, not the actual level numbers used. For instance, the record descriptions shown below are equivalent.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Some observations on Record-A |
It is useful to examine Record-A above and to answer the following questions:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Level number notes |
The level numbers 01 through 49 are general level numbers but there are also special level numbers such as 66, 77 and 88.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Building a record structure |
In the animation below we show how level numbers can be used to define a record structure as hierarchy of data-items. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Copyright NoticeThese COBOL course materials are the copyright property of Michael Coughlan. All rights reserved. No part of these course materials may be reproduced in any form or by any means - graphic, electronic, mechanical, photocopying, printing, recording, taping or stored in an information storage and retrieval system - without the written permission of the author. (c) Michael Coughlan Last updated : March
1999 mailto:michael.coughlan@ul.ie | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||