What’s new at MAINFRAMES 360
(Updates May '11)...
- We are a cosmopolitan community of 400 members, from around the globe.
- Working on writing and publishing articles on CICS.
- Special article on "How do I learn Mainframe Programming"

Friday, December 25, 2009

Decision Making in COBOL


Q. How does the IF Condition in COBOL work? What’s the syntax for it..?
The IF-condition is used for decision-making in COBOL. The basic IF-condition has following structure -

IF <test-condition> THEN
   Statement-1
   Statement-2
   ...
END-IF
Statement-X

The entire IF END-IF construct along with all its statements, Statement-1, Statement-2 constitute a single unit, a BLOCK. Upon entry into the IF Block, the test-condition is checked. The statements inside the IF Block constitute the body of the IF Block. Thus, the test-condition guards entry into the IF Body.

The <test-condition> expression is evaluated. If the test-condition expression equals = true, then the Statement-1, Statement-2,... are executed, i.e. the statements in the body of the IF-Block are executed. Once, the IF-Block has been completed, the next successive statement following the IF-Block, say Statement-X will be executed.

Image34.png[3]

When test-condition expression equals = false, the control jumps directly to Statement-X, after the IF-Block.

Example:
01  AIR-FARE PIC 9(5).
01  BAGGAGE-WEIGHT PIC 9(5).
01  MAXIMUM-ALLOWED PIC 9(5) VALUE 20.
01  DIFFERENCE PIC 9(5).
01  EXCESS-CHARGE PIC 9(5).
...
...
MOVE 1000 TO AIR-FARE
IF BAGGAGE-WEIGHT > MAXIMUM-ALLOWED THEN
   COMPUTE DIFFERENCE = BAGGAGE-WEIGHT – MAXIMUM-ALLOWED
   COMPUTE EXCESS-CHARGE = DIFFERENCE * 300;
   COMPUTE AIR-FARE = AIR-FARE + EXCESS-CHARGE
END-IF
DISPLAY ‘AIR-FARE : ‘ AIR-FARE

Explanation:
In a flight, the maximum weight of luggage allowed = 20 kgs. The passengers have to pay a Total fare = Basic Fare + Any extra charges for excess baggage above 20 kgs. How to code this logic in COBOL?

First, the MOVE statement stores the value 1000 in AIR-FARE variable. Now, if the baggage-weight exceeds 20 kgs., then you got to pay extra for the excess baggage. The IF condition checks, whether the weight is greater than 20.

Say, the baggage-weight = 24 kgs. Then, baggage-weight > 20, so difference = 24 – 20 = 4 kgs. The excess-charge would be 300 multiplied 4 times = Rs. 1200. So, the air-fare = 1000 + 1200 = 2200. Finally, the DISPLAY statement displays AIR-FARE=2200.

Say, the baggage-weight = 18 kgs. Then, baggage-weight < 20, so the test-condition fails. Thus, the control directly to the DISPLAY statement after the IF-block, and the Air-Fare is displayed as Rs. 1000. 

Image35[1]
Image36[1]

Upon running the above COBOL Program, you get the following output - 

Image37[1]
Q. How do you use the IF-THEN ELSE construct in COBOL?
The IF-THEN ELSE construct in COBOL, has the following syntax :

IF <test-condition> THEN
   Statement-1
   Statement-2
   ...
ELSE
   Statement-3
   Statement-4
   ...
END-IF
Statement-X

The IF-THEN ELSE construct is simple – when the test-condition = true, the IF-part is performed. When the test-condition = false, the ELSE-part is performed. Thus, the IF part and ELSE part are mutually exclusive. Either one of them is performed, based on the truth or falsity of the condition.

The statement-1/statement-2 could be a simple COBOL Statement, or it could be another IF Else block. One IF-ELSE Block can sit right inside another IF or ELSE. This is called as Nested Conditional.

Example:
Let’s study the working of a deeply nested conditional. The data in the program used are :

Image41[1] 

The logic used to calculate the RATE-OF-TAX is as follows -

  Salary upto 20k Salary>20k
Salaried Employee 08 percent 10 percent
Self-employed Professional 20 percent 20 percent

This decision-table is used to determine the rate of tax. You can implement this logic COBOL Code as follows -

Image42[1] 

The data presented tells you, the Salary is 18000. The program starts with SET SALARIED TO TRUE, which turns on the SALARIED switch. Thus, the control goes to the next statement, IF SALARIED. When this condition is evaluated(as SALARIED switch is turned on), it returns true. So, control enters into the the IF Block. The control reaches IF SALARY > 2000. Salary equals 18k, so
(SALARY > 20000) = false, the control goes to the ELSE part. The statement MOVE 08 to Rate is performed, which stores the value 08 in Rate. The Inner IF Block terminates. The control jumps to the DISPLAY statement, that displays the RATE = 08 percent.
 
Q. What are the Relational/Comparision Conditions?
To compare two quantities, which is larger one, which is smaller amongst them, are the two quantities equal in magnitude, you would use Relational Conditions. The different relational operators available in COBOL, are tabulated below :

Relational Operator Symbol
IS LESS THAN <
IS EQUAL TO =
IS GREATER THAN >
IS LESS THAN OR EQUAL TO <=
IS GREATER THAN OR EQUAL TO >=

A relational condition would have the following form:

data-item-1 relational-condition data-item-2

The data-items could be variables or literal constant values. Moreover, in relational conditions you can use the complete relational operator spelled out in words, or as an alternative the symbolic representation for it. So, for example you may write 2 < 3, or you may as well write 2 IS LESS THAN 3.

What happens, when you compare two data-items, which are non-numeric. In this case, the result of comparision depends on the collating sequence.
 
Q. Well in COBOL, how do you combine 2 or more conditions? Like, you wanna check if AGE >= 20 and AGE =< 35(Age lies in the range 20 to 35)?
In COBOL, you can write a simple condition or a Complex Condition. Two or more simple conditions connected by Logical Operators give a Complex condition. For example, you can use the Logical operator AND to combine two conditions. For example, to check whether Age is in the range 20-35, you write

IF (AGE >= 20) AND (AGE <= 35)
  <condition-1>     <condition-2>

The Logical Operators in COBOL, are briefly discussed below :
1. IF BALANCE IS NEGATIVE AND DAYS-OVERDUE > 30
     
PERFORM 1100-SEND-OVERDUE-NOTICE

In the above example, the PERFORM statement is executed, only when both the conditions balance is less than 0, as well as, DAYS-OVERDUE exceeds 30 are satisfied. The AND operator returns true, only if both the conditions are true.

2. IF PHONE-NUMBER IS NOT NUMERIC OR NAME-IS-MISSING
      PERFORM INVALID-DATA

In the above example, the PERFORM Statement is executed, when either the phone-number field is non-numeric or the name field is missing. Thus, the OR Operator returns true, even if one of the conditions is true. If both the conditions are true, well and good, but even if one of them is true, OR returns true.

3. IF WEIGHT-1 NOT = WEIGHT-2
      PERFORM INEQUAL-WEIGHTS

In the above example, the PERFORM Statement is executed, when the first weight does NOT equal the second weight. The NOT Operator is used to negate any condition.
Q. What are Class Conditions in COBOL?
Class Conditions help determine, what type of data is present in a variable. Thus, it checks the contents stored in a variable, is it alphabetic, numeric or alpha-numeric... Suppose, you want to check, whether the name is alphabetic, or age of the person is numeric, then you should use Class Conditions. The general format for Class conditions is given below -

Image43[1]

Data is Alphabetic implies that it contains only characters from A-Z and/or blanks. Alphabetic data cannot contain numerals(0,1,2,...,9). On the other, when you say that data is numeric in COBOL, it can contain digits from 0-9, with/without a sign.

ALPHABETIC-LOWER and ALPHABETIC-UPPER are the same as alphabetic, except that they also take the case into account; whether it is lower-case or upper-case. So, if the variable WS-TEXT contains ‘HELLO’, and you test IF WS-TEXT IS ALPHABETIC-UPPER, it returns true.
Q. Hey pal, how do you check if the data input in a COBOL Program is alphanumeric? Alphabetic is cool, numeric is awesome, but alphanumeric?
Let’s assume, you want to check, if the ADDRESS-FIELD is alpha-numeric. To do this, take it one at a time, (i) First check, if ADDRESS-FIELD is alphabetic. (ii)Next check, if ADDRESS-FIELD is numeric.

IF ADDRESS-FIELD IS NOT ALPHABETIC THEN
   IF ADDRESS-FIELD IS NOT NUMERIC THEN
      PERFORM INVALID-ADDRESS
   END-IF
END-IF
Q. What are Condition-name conditions?
Suppose the variable WS-GENDER stores the values ‘M’ for Male or ‘F’ for female. You may want to associate the values ‘M’ or ‘F’ with some meaningful names such as MALE or FEMALE. To do this, you use 88-Level Condition names. Consider the following :

--1----+----2----+----3----+----4----+----5----+----6----+----7--
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
01  GENDER                            PIC X.                    
    88 MALE                           VALUE 'M'.                
    88 FEMALE                         VALUE 'F'.                
PROCEDURE DIVISION.                                             
    SET MALE TO TRUE                                            
    IF MALE THEN                                                
       DISPLAY 'GENDER IS MALE'                                 
    ELSE                                                        
       DISPLAY 'GENDER IS FEMALE'                               
    END-IF                                                      
    STOP RUN.
                                                   


In the above code snippet, I have assigned the values ‘M’ and ‘F’, the symbolic names MALE and FEMALE respectively using 88-level entries. 88-Level entries act as flags. To turn the flag on, you use the SET Statement. SET MALE TO TRUE, turns MALE flag on, and ‘M’ is stored in GENDER variable. To check if the gender is male, you may write IF GENDER = ‘M’ or you could use the condition name -  IF MALE; both are equivalent.
Q. What are Sign-conditions?
Sign conditions are used to find if a numeric quantity is positive(>0) or negative(<0). Thus, they are used to check for the sign of a numeric variable.

The general syntax for Sign condition is given below -

Image44[1] 
The quantity which is being test could be stored in a Variable, or it could also be the result of an expression. A variable is POSITIVE, if it is greater than > 0. On the other hand, the variable is NEGATIVE, if it is less than 0.

Example:
IF BALANCE IS NEGATIVE
   PEFORM SEND-CREDIT-OVERLIMIT-NOTICE
END-IF

Saturday, December 19, 2009

COBOL Tutorial – USAGE Clause


Q. What is USAGE Clause? Could you cite an example, where one would employee USAGE Clause?

USAGE Clause signifies, how the computer internally stores or represents DATA. Memory space calculation is done by the computer, on looking at the USAGE Clause. Thus, USAGE Clause answers the question, How much storage space does a data-item occupy? 1 byte, 2bytes, 4 bytes,… well how much? Depending upon the type of data – whether alphabetic or numeric, you should choose a USAGE(internal representation) that suits your purpose.

USAGE

Purpose
DISPLAY When you want to work with characters, manipulate them, work on textual string data
COMPUTATIONAL More appropriate, when you want to perform arithmetic computations, specially on numbers – integers and fractional numbers

When there is no explicit USAGE clause, it defaults to USAGE IS DISPLAY.

Q. How do computers internally store data?

Computers store data in Binary digits 0 and 1. Thus, all the data you enter into the computer is represented as 0’s and 1’s. The computer is said to understand and speak binary language.

Text data such as ‘HELLO’, ‘I HOPE YOU ARE IN THE PINK OF HEALTH’,’QUASAR CHUNAWALA’ are stored using a collating sequence(ASCII or EBCDIC). The collating sequence assigns a unique code number to each character. For example, A is assigned ASCII code 65, B = 66, C = 67, and so on. Now, when you type A on the computer, the computer interprets it as ASCII Code 65, which gets stored as 0100 0001.

Image45[1]

Numeric data can be held as Text Digits(ASCII Digits), or as pure Binary numbers, or as Binary Coded Decimal(BCD), or as Real Numbers(IEEE Format).

Q. What is the USAGE IS DISPLAY Clause?

USAGE IS DISPLAY is mainly used with Text Data such as ‘HELLO’,’HOW ARE YOU’ and numeric data, which is not used in computation such as Street no. ‘2530’, phone numbers like ’17654094509’.

Text-data(strings) are made up of several characters. For example, the text-string ‘HELLO’ is composed of the characters ‘H’,’E’,’L’,’L’ and ‘O’. When you apply USAGE IS DISPLAY, the text-data is stored as ASCII/EBCDIC Characters.

Generally, when Text-data is stored on PC, the collating sequence used is ASCII. On Mainframes, Text-Data is stored in EBCDIC Format.

Example:

01 WS-TEXT PIC X(5) VALUE ‘HELLO’ USAGE IS DISPLAY. 

Here, ‘H’, ‘E’, ‘L’, ‘L’ and ‘O’ are stored ASCII/EBCDIC Characters. A picture of how they look is shown below :

Image46[2] 

Each ASCII/EBCDIC Character requires 1 byte. So, the text-data ‘HELLO’ occupies 5 bytes of storage.

Q. Is there any problem, if I store numeric data, which is used in computations with a USAGE IS DISPLAY?


01 WS-VAR PIC 9 VALUE 2. 

Since no USAGE clause is explicitly specified, it defaults to USAGE IS DISPLAY.

When numeric data is specified with USAGE IS DISPLAY, these text-digits of the number are stored as ASCII/EBCDIC digits. Thus, the Text-digit 2 would be stored in computer memory as shown below -

Image47[1] 

Each text-digit is stored in ASCII/EBCDIC Form, so it will occupy 1 byte. Hence, if you store 12345.67 with USAGE IS DISPLAY, it will occupy 7 bytes.

Imagine a scenario, where you want to read and process a data-file containing 1 million phone-numbers, each phone-number being a PIC 9(10). Each phone number occupies 10 bytes of storage.

For processing 1 million phone numbers,

1 MILLION x(multiplied by) 10 bytes for each phone number = 10 million bytes.

This is quite computationally expensive in terms of storage space, time and bandwidth. Reading and processing a file as large as 10 million bytes(10 MB) takes time. This is why,
USAGE IS DISPLAY is not good choice for storing numeric data.

USAGE is DISPLAY, is more suited to working on a character-by-character level on Text data-items. When you want to perform character-level manipulations, use DISPLAY.

Q. What is the USAGE IS COMP/COMPUTATIONAL/BINARY Clause?

USAGE IS COMPUTATIONAL is more appropriate when you are dealing with integer numbers. USAGE IS COMPUTATIONAL indicates that the data-item shall be stored as pure binary numbers.

Example:
01  WS-NUMBER  PIC 9(04) VALUE 1327 USAGE IS COMPUTATIONAL.

USAGE IS COMPUTATIONAL indicates that 1327 is stored in pure binary form. A picture of how 1327 is stored, is given below :

Image49[1]  

The pure binary equivalent of 1,327 occupies 2 bytes.

The memory space calculation is done by the computer, as follows:

Integer Number Range PICTURE Clause Storage Space

-32,768 to +32,767

S9(01) to S9(04) 2 Bytes
-2 billion to +2 billion S9(05) to S9(09) 4 Bytes
-1.8 x 10^19 to +1.8 x 10^19 S(09) to S9(18) 8 Bytes

For numbers upto 32,767, 2 bytes of memory space is reserved. What if, you wanted to store an even larger integer number like 50,000? The pure binary equivalent of such numbers would then not fit into 16 bits(2 bytes). A 2-bytes bucket would not be large enough to accommodate it. They would spill over. They would need a big tumbler(say 4 bytes capacity). So, automatically 4 bytes of memory space would be reserved. What if, you store a mammoth number like a hundred billion? A hundred billion would have a picture clause S9(11), so it would occupy 8 bytes.

As you may observe on the table, based on the ranges, we can draw a correlation between the PICTURE Clause of the data-item and the Storage space.

Consider the following simple example, in which the variables WS-VAR-1 and WS-VAR-2 are declared as COMPUTATIONAL. As the USAGE IS COMPUTATIONAL, the numbers 10 and -12 are stored internally in binary format.

USAGE-1[2] 

Upon the addition Of these two integers, 10 and -12, the COBOL Code shows the following output. 10 added to –12 gives a sum of –2. Note, that sign is stored as part of the rightmost digit. K stands for minus(-ve) 2. So, 1K means –12 and 0K means –2.

USAGE-2[2] 

Q. What is the USAGE IS COMP-1/COMP-2 Clause?

When you want to work with fractional numbers, numbers containing a decimal point, you must employ USAGE IS COMP-1. USAGE IS COMP-1 is used to store floating point numbers(real numbers). COMP-1 occupies 4 bytes of memory space. Floating point(real numbers) are divided into 2 parts : mantissa part and exponent. For example, 123.456 can be represented as mantissa = 123456 e -3. The mantissa part is occupies 3 bytes. The exponent part occupies 1 byte.

On the other hand, if you have an extremely huge number like a googol, 1 e +100, you would need more space. Moreover, you a number 23 e -50 is a extremely tiny number(quite close to zero, but not zero), to store it require lot of precision and accuracy. For such numbers, you would use COMP-2. COMP-2 is used for real fractional numbers which are extremely large, and very small. COMP-2 occupies 8 bytes of memory space.

Q. What is the USAGE IS COMP-3 Clause?

As most references quote, COMP-3 is used mainly for packed decimal values(BCD Format). I would say, on most computer systems, running COBOL Applications, COMP-3 is the general standard. The reason behind this is, COMP-3 packs, two digits in 1 single byte. This is really cool, and it saves a lot of space.

Let me show ya, how COMP-3 achieves this remarkable feat. In the decimal number system, any digit can take a value ranging from 0-9, there are in all 10 possibilities. Thus, you can represent any digit with 4 bits(16 possibilities, 4 bits = 1 nibble). So, in COMP-3 format, each digit occupies only 4 bits. You must find the binary representation for each digit separately. For example, the number

  4    5    9    8    1    2 .  5    6   would be stored as
0100 0101 1001 1000 0001 0010 0101 0110.

One can infer, in COMP-3 format, two digits are packed into a single byte. So, 459812.56, a number, which is composed of 8 digits would take only 4 bytes of computer storage. There’s a general formula for memory space calculation, in COMP-3 format -
S9(4) COMP-3 would occupy 2 bytes.
S9(6) COMP-3 would occupy 3 bytes.
S9(7) COMP-3 would occupy 4 bytes.
S9(n) COMP-3 would occupy (n+1)/2 bytes.

Saturday, December 12, 2009

VSAM Tutorial 04 – Loading Records in VSAM KSDS


Q. How do you load data into VSAM KSDS Dataset?
Storing data in a VSAM KSDS file, is known as Loading. Initially, to begin with the KSDS File is empty, it doesn’t contain any records. Based on, the Control Interval Size, that you’d have specified, while creating the KSDS File, the MVS Operating System books and reserves empty Control Intervals of that size in the KSDS Cluster. These are yet to be filled up,populated with data-records.

To actually load data-records into a VSAM KSDS file, you use IBM Supplied Utility IDCAMS. Instruct the IDCAMs utility, what’s the task you would like to perform by providing an appropriate control statement(as instream data). The IDCAMS utility picks up the Control Commands, and acts accordingly. So, the control command used to Load Data records in VSAM KSDS File is REPRO Command. In this tutorial, effectively, you are going to REPRO in the data records from a plain Sequential file into a KSDS Dataset. At the same time you’ll also learn, how to unload the contents KSDS Dataset back to a plain sequential file.

Image28[2] 
Q. Hey, tell me what’s the syntax for the REPRO Command? How do I write a Batch Job/JCL with IDCAMS and REPRO Command?
The REPRO Command in it’s simplest form looks like this -

REPRO INFILE(input-ddname)
      OUTFILE(output-ddname)

In the above syntax, the input-ddname is DD Card for the Input Dataset, which happens to be the Physical Sequential(PS) Dataset, the source which currently holds the data-records. The output-ddname is the DD Card for the Output Dataset, which should be the destination VSAM KSDS File. So, let me quickly put together an example, that’ll show how to load records in a dataset. I am going to create a KSDS Dataset, to store the data of Employees working in an Organisation. I would like to store their ENo, EName, Salary, JDate(Joining Date). Assume that, you’ve stored the following Employees Data in a sequential file -

PS SEQUENTIAL FILE – AGY0157.SEQ.EMPLOYEE

Image30[1]

As you can observe, all the data-records have a length = 23. Moreover, the Employee ID, is 2 digits. This can be used to uniquely identify each employee. Hence, this will acts as a Key in the VSAM KSDS Cluster. Note that all the Input Data Records are increasing(ascending) order of the Key(EMPLOYEE ID). If the records are out of order, it will throw an error. When you load data records in KSDS Dataset, it should always be Sequential Load.

Given below is the definition of the VSAM KSDS File : AGY0157.KSDS.EMPLOYEE, which will be used to store the Employee Records -

Image31[1]

Notice, that the name of the VSAM KSDS Cluster is AGY0157.KSDS.EMPLOYEE. It has RECORDSIZE 23, and the Key values begin from column 0(offset) and are 2 columns wide. The word INDEXED is used to create KSDS.

To load or REPRO IN the Employee Data from PS Sequential File, AGY0157.SEQ.EMPLOYEE to VSAM KSDS File AGY0157.KSDS.EMPLOYEE, you would use the REPRO Command of the IDCAMS utility as follows :

Image32[2] 

Upon submitting this Job, the records are loaded successfully, and this can be verified by seeing the JOB SYSOUT Dataset.

Image33[1]

You can see in bold that the NUMBER OF RECORDS PROCESSED WAS 6. This implies all the 6 records were successfully loaded into the VSAM KSDS File with Maximum Condition Code 0.
Related Posts Plugin for WordPress, Blogger...

Note :

Protected by Copyscape Online Copyright Protection
© Copyright – Quasar Chunawalla, 2010.
Note : The copyrights of all the material, text and pictures posted in this website belong to the author. Any instance of lifting the material from this website, shall be considered as an act of plagiarism. For any clarifications, please drop me a line at
 
back to top