2.1.
MOVE
This statement moves data values from literal to identifier, or identifier to identifier
It does not actually moves but copies.
Some major points to be noted are
• Receiving field should not be smaller than the sending field. If the receiving field is smaller than the sending field, then
truncation happens
• Alphabetic / alphanumeric data is left justified in receiving field. If the receiving field is 5 bytes long and the sending
field is 8 bytes, then only the first 5 bytes of the sending field will be moved to the receiving field.
• Numeric data is right justified in receiving field. If the receiving field is 5 bytes long and the sending field is 8 bytes ,
then only the last 5 bytes of the sending field will be moved to the receiving field.
Syntax:
MOVE Literal-1 [, Identifier-1]TO Identifier-2 [, Identifiers-3]. . .
Example :
MOVE 15 TO A.
15 will be copied to data name A.
MOVE A TO B, C, D.
Contents of data name A will be copied to data name B, C, D.
2.2. ADD
ADD verb is used to calculate the sum of two or more numeric data items. We will have to declare all the variables to
be added in the working storage section of the Data division. The logic for the addition needs to be performed in the
procedure division.
Syntax
ADD Identifier-1 TO Identifier-2
GIVING [ Identifier-3] [ ROUNDED ]
Example 1:
ADD A B TO D GIVING E
Before addition A=2, B=4, D=5, E=3
After addition A=2, B=4, D=5, E=11
Example 2:
If the values in A, B & C are 23.412 , 35.273 & 42.4
ADD A, B GIVING C ROUNDED.
After execution, the value of C will be 58.7 instead of 58.685.
2.3. SUBTRACT
SUBTRACT verb is used to subtract one, or the sum of two or more numbers from one or more numbers and to store
the result. We will have to declare all the variables to be added in the working storage section of the Data division. The
logic for the addition needs to be performed in the procedure division.
SUBTRACT 10 FROM A, B.
10 will be subtracted from both A and B.
SUBTRACT A, B FROM C.
A & B will be added and the result will be subtracted from C.
SUBTRACT A FROM B GIVING C ROUNDED.
If the values in A, B & C are 23.412, 35.273 & 75
After execution, the value of C will be 11.9 instead of 11.861.
2.4. MULTIPLY
MULTIPLY statement causes numeric items to be multiplied and sets the value of data items equal to the results.
Example:-
MULTIPLY A BY B GIVING C.
C would have product of A and B.
Eg:-
MULTIPLY 5 BY 6 GIVING C.
C will have a value of 30 which will be the product of 5 & 6.
2.5. DIVIDE
DIVIDE statement divides one numeric data item by another and the quotient and the remainder will be populated with
the values based on the division.
Example 1:-
If values of A is 15 consider the below DIVIDE statement
DIVIDE 5 INTO A GIVING B
will give after execution B = 3, where B is the quotient.
Example 2:-
If values of A is 16, consider the below DIVIDE statement
DIVIDE 5 INTO A GIVING B REMAINDER C
will give after execution B=3, C=1 where C is the remainder.
2.6. COMPUTE
All the computations performed by the other four arithmetic statements can be done by using only the COMPUTE
statement.
Example :
COMPUTE A = ( ( B + C ) / ( D – E ) ) * 100
2.7. STRING & UNSTRING
STRING
Two or more strings of characters can be combined to form one longer string with the help of the STRING statement.
Syntax:-
STRING FIELD-1, FIELD-2 INTO FIELD-3
The above statement will combine the 2 strings and give the output in the field-3
UNSTRING
This statement facilitates the splitting of one string to many substrings
eg:-
if STRING-1 is having value ‘A.K. KRISHNAMURTY’.
Consider the statement UNSTRING STRING-1 DELIMITED BY ‘.’ INTO FIELD-1, FIELD-2, FIELD-3.
Will store character ‘A’ in FIELD-1 character ‘K’ in FIELD-2 and ‘KRISHNAMURTY’ in FIELD-3. Since the delimiter is ‘.’
it will split the string whenever a delimiter is encountered. In this case we have delimiter ‘.’ after A and K. So it will sp lit
the entire string into 3 small strings as described above.
2.8. INSPECT
This statement enables the programmer to tally and /or replace the occurrences of a single character or group of
characters in a data field. The word tallying is used to count the number of characters in the string and store the value
in a variable.
Example 1:-
Consider ‘MY-STRING' which is having value 'CHANDRA BABU NAIDU' and other field ‘TALLY-COUNT’ where output
after the INSPECT statement execution will be stored
MY-STRING PIC X(20) VALUE ‘CHANDRA BABU NAIDU’
TALLY-COUNT PIC 9(2) VALUE 00.
INSPECT MY-STRING TALLYING TALLY-COUNT FOR ALL ‘A’ - will give the value of TALLY-COUNT as 4 since there
4 ‘A’ in the string ‘MY-STRING’.
Example 2:-
Consider ‘STRING-1' which is having value 'AABBCC'. In the below INSPECT statement we are going to replace all 'A'
by 'D'
INSPECT STRING-1 REPLACING ALL 'A' BY 'D'.
Now the value in STRING-1 will be 'DDBBCC' where all the 'A' are replaced with 'D'
Problem statement #2:-
Write a program to concatenate 3 strings into a single string. If the first string is having value ‘TATA’, second string is
having value ‘CONSULTANCY’ and third string is having value ‘SERVICES’, then the result should have the value
‘TATA CONSULTANCY SERVICES’ after the statement is executed