| Q. What is a Variable? What are Literals? |
A Computer Program takes Data as Input, performs processing on the Input Data, and produces and Output. You would like to store the Input Data and Output Results in Computer Memory, so that you can retrieve it for later purposes. Let's take a look at how Computer Memory looks like. Just like on the street people live in houses, Computer Memory is organised as a series of Cells. These cells do not house people, instead they house Data. You can visualize a picture of computer memory like the one below: Suppose you have stored the number 2, at some Memory Location in the Computer Storage. Next time, you want to retrieve the contents of this cell. How to go about it? In Computer Memory, how do you refer to a particular Cell or Memory Location? In the real world, houses on a street have different names. In a similar fashion, what you have to do is, you need to assign a name to the Computer Memory Location or House, say MY-NUMBER. And then, you can access the contents of this Memory Location, using the name MY-NUMBER. Next time, you simply have to say, "DISPLAY the contents of MY-NUMBER", and you'll get the Output=2. I am going to tell you a little more about the data(contents) stored inside a Computer Memory Location. If the data stored in a Computer Memory Location can change, for example if the value in MY-NUMBER can be changed(modified) to 3, such a Memory Location is called Variable. The word Variable is due to the fact, that the contents of such a Cell can vary or change. On the other hand, a Literal means constant data. Together, Variables and Literals are Computer Storage Areas, where data is kept, and identified a unique name. |
| Q. What are the rules for naming Variables? |
Every Variable(Computer Storage Area) must have a name. It's a good idea to assign relevant and meaningful names to variables. For example, PRINCIPAL, NUMBER-OF-YEARS, RATE-OF-INTEREST are self-explanatory names. Whitespaces are not allowed. If a Variable name has multiple words, separate them by Hyphens, like AREA-OF-CIRCLE. |
| Q. What is declaration of Variables? What is DATA DIVSION? |
You can't directly store data in a Variable(Mainframe Computer Storage Area). First, you must declare or announce the Variable. Declaration actually causes the Mainframe Computer, to keep aside Storage Space for your Data. During declaration, you must specify exactly how many Bytes of Space you need, to store your Data – One Byte, Two Bytes, Three Bytes how much? Depending on your space requirements, the Mainframe Computer honours your request, and exclusively reserves(books) Storage Space for you. Now, you can go ahead and store some data in it. Remember that, the Mainframe Computer is like a miser. It does not give away any Bytes for free. Even if you want a single byte of Memory Space, you must first ask the Mainframe Computer for it. Telling the Mainframe Computer, how much space you need to store Data, is called Declaration. DATA DIVISION contains the declaration(list) of all the Variables(Computer Storage Areas), you want to use in the COBOL Program. |
| Q. How to declare Variables in the DATA DIVISION? |
Declaring a Variable in COBOL is very easy. In COBOL, you code the data-name of the Variable, followed by Data-type. Suppose, I want to calculate Simple Interest on Rs. 1000, for 5 years, at 20 percent Interest. The assumption is, to store one character you need 1 Byte. To store the Input Data-Items on the Mainframe Computer, I shall need three Variables – PRINCIPAL(4 Bytes large), NUMBER-OF-YEARS(1-Byte) and RATE-OF-INTEREST(2-Bytes). How to declare these variables in COBOL? You always code the Data-Name followed by Data-Type.
|
| Q. What are data-types? What are the basic data-types in COBOL? |
Data-type indicates whether a variable can hold Alphabetic Characters such as 'A','B','C',... etc. or numeric data such as 123, –500, 6159, ... etc. The data that you can store in a COBOL-Variable falls into one of these classes :
Alpha-numeric data consists of Alphabetic character and numbers. For example, 'HELLO 123 @$','INDIA IS THE 3RD LARGEST ECONOMY' are alpha-numeric strings. In COBOL, the Symbol X implies Alpha-numeric. Numeric Data refers to numbers which are used in Arithmetic-computation. 123, 3.14159, –642.70 are examples of Numeric Data. In COBOL, the Symbol 9 implies Numeric. Alphabetic Data refers to Non-Numeric Data. 'HELLO HOW DO YOU DO', 'NOT BAD!' are examples of pure Alphabetic-Strings. In COBOL, the Symbol A implies Alphabetic. |
| Q. What is PICTURE Clause? |
PICTURE Clause specifies the Data-Type and Size(in Bytes) of a Variable. PICTURE Clause is coded after the Data-Name. You may code PICTURE or simply PIC.
PRINCIPAL Variable is defined as PIC 99999. 9 means PRINCIPAL Variable can hold Numeric Data. As it is a PIC 99999, five times, PRINCIPAL Variable occupies 5-Bytes of Storage space. Generally, you can store one character in a Byte. So, in 5-Bytes of Storage Space, you can store a number upto Five-Digits large. NUMBER-OF-YEARS Variable is defined as PIC 9. NUMBER-OF-YEARS occupies 1-Byte of Storage-Space. In 1-Byte Space, you can store a Single-Digit Number. RATE-OF-INTEREST Variable is defined as PIC 99, which suggests it is 2-Bytes big and numeric type. In 2-Bytes Space, you store a number upto . FIRST-NAME Variable is specified as PIC XXXXXX. X stands for Alpha-numeric, so FIRST-NAME can hold alpha-numeric Data. Further, as its PIC XXXXXX(Six Times), you can store a alphanumeric Textual-word upto Six Characters large in it. Size or Length of FIRST-NAME is Six. LAST-NAME Variable is specified as PIC XXXXXXXXXX. This means, you can store an alpha-numeric string upto ten characters long in the LAST-NAME Variable. You can code PIC XXXXXXXXXX, in short as PIC X(10). Similarly, you may code PIC 99999 as PIC 9(05) in short.
 |
| Q. What are group and Elementary Data Items? |
COBOL provides the facility to provide a detailed-breakup of a Variable(Computer Storage Area). You can drill down a COBOL Storage-Area into smaller parts. Say for example, there's a COBOL Variable called EMPLOYEE-NAME, that stores the Employee's full-name. It is declared as follows -
But, I also know that the Employee's Full-Name consists of his First-Name, his Middle-Name and his Last-Name, all put together. Therefore, it is possible to represent EMPLOYEE-NAME as a composite item – broken down into simpler items EMPLOYEE-FNAME, EMPLOYEE-MNAME and EMPLOYEE-LNAME, which is represented in COBOL as follows -
Thus, I have supplied a detailed break-up of the EMPLOYEE-NAME, 30-Characters Field which consists of EMPLOYEE-FNAME, 10 Characters Field, EMPLOYEE-MNAME, 10 Characters Field and EMPLOYEE-LNAME, 10 Characters Field. EMPLOYEE-NAME is called a Group Data-Item. The sub-ordinate data-items under it – EMPLOYEE-FNAME, EMPLOYEE-MNAME and EMPLOYEE-LNAME are called Elementary or Simple Data-Items. A visual representation of the EMPLOYEE-NAME Area and its break-up is shown below. In a similar fashion, the Address of an Employee generally consists of a Street, a City and Pin-Code. Therefore, in the COBOL Program you may specify a detailed break-up of Address like this -
The Group Data-item Address is composed of Street, a 10-Byte Alphanumeric Field, City being a 10-byte alphanumeric field again and pin-code, a 6-digit numeric field. The Address Field has a sum-total size of 10 Bytes + 10 Bytes + 6 Bytes = 26 Bytes. Pictorially it may be represented as - Likewise, the phone-number of an Employee, would consist of Country- Code, City-Code and the Actual number. Look at, how I've coded EMPLOYEE-PHONE-NO Group-Item in COBOL.
The Full-Name of the Employee, the Address of the Employee and his Phone-No. together represent an Employee's Data. COBOL allows aggregation, putting together Data-items, under a head, a higher-level data-item. So, I have clubbed EMPLOYEE-NAME, EMPLOYEE-ADDRESS and EMPLOYEE-PHONE-NO under one roof, EMPLOYEE-DATA.
Take a look at the above picture. I'll just quickly run you through the EMPLOYEE-DATA Item's structure. EMPLOYEE-DATA is used to hold or store the data of an Employee. The Group Item EMPLOYEE-DATA is broken down into EMPLOYEE-NAME, EMPLOYEE-ADDRESS and EMPLOYEE-PHONE-NO. EMPLOYEE-NAME is a group-item in turn, consisting of EMPLOYEE-FNAME, EMPLOYEE-MNAME and EMPLOYEE-LNAME Elementary Items. EMPLOYEE-ADDRESS is a group-item internally made up of EMPLOYEE-STREET, EMPLOYEE-CITY and EMPLOYEE-PINCODE Elementary Items. The COUNTRY-CODE, CITY-CODE and LOCAL-NUMBER together constitute the Group-Item EMPLOYEE-PHONE-NO.
The EMPLOYEE-DATA Item can be represented with the help of a Inverted Hierarchical Tree-Like picture shown above. This is the structure of EMPLOYEE-DATA. In this manner, COBOL allows you to specify the format or structure of the data, by creating Group and Elementary Data-Items. |
| Q. What are Level-Numbers in COBOL? |
In the Indian Military Forces, General delegates his authority through Major General, who heads Lieutenant General, Brigadier, Colonel, and so on. There are different ranks in the Military. In a similar fashion, when you describe a Data-structure or format in COBOL, every item holds a rank in the structure. For example, with reference to the example of the EMPLOYEE-DATA Item, take a look at the structure. This tree-structure has several Levels. Each level can be numbered 01, 02, 03 and so on. At the top of the Hierarchy, is EMPLOYEE-DATA. As this is the root-level, or top-most level, this Level is called 01-Level. EMPLOYEE-DATA is said to be a 01—Level Data-Item. EMPLOYEE-NAME, EMPLOYEE-ADDRESS and EMPLOYEE-PHONE-NO are sub-ordinates of EMPLOYEE-DATA. So, they are said to be at Level 02. Similarly, EMPLOYEE-FNAME, EMPLOYEE-MNAME, EMPLOYEE-LNAME, EMPLOYEE-STREET, EMPLOYEE-CITY and so on... are at Level 03. In COBOL, you indicate where a Data-Item stands in the Hierarchy(Structure-Tree) by coding the Level-Number. You write the Level-Number, followed by the data-item name. For example, you should code 01 EMPLOYEE-DATA, 02 EMPLOYEE-NAME and 03 EMPLOYEE-MNAME. I have written the COBOL Code, for the above EMPLOYEE-DATA Tree Structure as follows -
01-Level Data-Items like EMPLOYEE-DATA, should always be coded in Area-A(Positions 8-11). On the other hand, lower-level data items like EMPLOYEE-NAME or EMPLOYEE-MNAME should be coded in Area-B(Positions 12-72). In the above example, EMPLOYEE-FNAME PIC X(10), EMPLOYEE-MNAME PIC X(10), and EMPLOYEE-LNAME PIC X(10) Fields together make up EMPLOYEE-NAME Field. The fact that EMPLOYEE-NAME Field is 30-Bytes large is understood here, you don't need to code or specify a new PICTURE Clause for it. The same goes for EMPLOYEE-ADDRESS Field which is 26-Bytes wide, or EMPLOYEE-PHONE-NO Field which is 14-Bytes in length. Summed up, EMPLOYEE-DATA Field turns out to be 30 Bytes + 26 Bytes + 14 Bytes = 70 Bytes. |