COBOL (COMMON BUSINESS ORIENTED LANGUAGE)
Overview
COBOL Fundamentals DAY2
EDITING PICTURE CLAUSE
Edited picture symbols Edit symbol
Meaning
Z
Zero suppression
*
Check protection
,
Comma insertion
-
Minus sign insertion
+
Plus or minus sign insertion
Edited picture symbols
Edit symbol
Meaning
$
Dollar sign insertion
CR
Credit symbol
DB
Debit symbol
B
Blank insertion
/
Slash insertion
.
Decimal point insertion
BLANK WHEN ZERO
Blank insertion when the value is zero
Edited picture symbols (Example) Edit symbol
Value
Edited value
ZZ999
12
012
***99
12
***12
$9999
123
$0123
-9999
-1234
-1234
+9999
-1234
-1234
Edited picture symbols (Example) Edit symbol
Value
Edited value
999CR
-12
01 2CR
999CR
12
012CR
999.9
12.34
012.3
99,999
1234
01,234
99B99
1234
12 34
Editing Picture Clause Example
EDITING PICTURE CLAUSE EXAMPLE
REDIFINES AND RENAMES
Overview
What is Redifines ?. •
Facilitates two or more data-names to point to the same memory location.
01 WS-POLICY-INFO. 05 POLICY-ID. 10 POLICY-LOC 10 POLICY-NO
PIC X(3). PIC X(8).
10 POLICY-NO-TYP REDIFINES POLICY-NO 15 POLICY-TYP PIC X(3). 15 POLICY-NO-T PIC X(5). 10 POLICY-CDE PIC X(2). 05 POLICY-EXP-DTE PIC X(10). 05 POLICY-HLD-NM PIC X(25).
REDEFINES Clause Rules governing REDEFINES clause Multiple REDEFINES is allowed for a data-item. REDEFINES clause must not be used for 01 level in FILE SECTION. Must not be used for data-items defined in level numbers 66 and 88. The item to be redefined should not have an OCCURS clause. Any change in REDEFINED item reflects on the value of the REDEFINING item and vice-versa.
REDEFINES Clause Syntax data-name-1 REDEFINES data-name-2.
Example 01 STUD-DETAILS. 05 STUD-NAME. 10 FIRST-NAME 10 MIDDLE-NAME 10 LAST-NAME 05 NAME REDEFINES
PIC A(15). PIC A(10). PIC A(10). STUD-NAME.
redefines WS00-YEAR1. REDEFINES CLAUSE … WS00-YEAR2 example It is the same 8 bytes of information which WS00-YEAR2 provides in the Year, Month & Day format in it’s sub-items. Any change in WS00-YEAR1 changes value of WS00-YEAR2 and vice-versa.
************************************ YEAR FOR ENTERED DATE IS
Output SPOOL
: 2005
MONTH FOR ENTERED DATE IS : 01 DAY FOR ENTERED DATE IS
: 01
************************************
RENAMES Clause •
Facilitates re-grouping of elementary data items in a record. After the renames enforcement the elementary items would belong to the original (renamed) group item as well as the new (renaming) group item.
Syntax 66 data-name-1 RENAMES data-name-2 THRU data-name-3.
Rules to be followed while using RENAMES RENAMES must be used after the description of the fields required. Must be coded only with level number 66. Data-names 2 and 3 should not have level numbers 01 and OCCURS Clause. The elementary items getting renamed should be contiguous.
RENAMES Clause Example
The elementary items getting renamed should be
01 STUD-DETAILS. 05 REG-NO 05 S-F-NAME 05 S-M-NAME 05 S-L-NAME
contiguous. PIC 9(5). PIC X(15). PIC X(12). PIC X(8).
66 STUD-NAME RENAMES S-F-NAME THRU S-L-NAME. Must be coded only with level number 66.
RENAMES clause .. example
WS-REN would be picking up the value of the sub-items from WS-IN12 to WS-22 (spreading across WS-IN1 & WS-IN2 values).
Note that WS-IN11 is left out.
*********************************
Output SPOOL
WS-REN VALUE IS : 341234 *********************************
Procedure Division VERBS
Data movement verb.
Arithmetic Verbs.
Input / Output Verbs.
Sequence control verbs.
Arithmetic VERBS ADD SUBTRACT MULTIPLY DIVIDE COMPUTE
ADD VERB The ADD statement sums two or more numeric operands and stores the result.
Syntax-1 ADD { identifier-1, literal-1 } [ , identifier-2, literal-2 ] . . . TO identifier-3 [ , identifier-4 ] . . .
Syntax-2 ADD { identifier-1, literal-1 } { identifier-2, literal-2 } [ identifier-3, literal-3 ] GIVING identifier-4 . . .
ADD Examples Before After
Before After
Before After
ADD Cash TO Total. 3 1000 3
1003
ADD Cash, 20 TO Total, Wage. 3 1000 100 3
1023
123
ADD Cash, Total GIVING Result. 3 1000 0015 3
1000
1003
ADD Males TO Females GIVING TotalStudents. Before 1500 0625 1234 After
1500 2125
0625
ADD Before
After
WS00-IN1 WS00-OUT1
123 0000
WS00-IN1 WS00-OUT1
123 0123
Before WS00-IN1 WS00-OUT2
123 0000
After WS00-IN1 WS00-OUT2 Before WS00-IN1 WS00-IN2 WS00-OUT2
After 123 456 0000
WS00-IN1 WS00-IN2 WS00-OUT2
123 456 0579
123 0223
SUBTRACT VERB The SUBTRACT statement subtracts one numeric item, or the sum of two or more numeric items, from one or more numeric items and stores the result.
Syntax SUBTRACT { identifier-1, literal-1 } [ identifier-2, literal-2 ] . . . FROM identifier-3 [ , identifier-4 ] [ , GIVING identifier-5 [ , identifier-6 ] . . . ]
SUBTRACT Examples
Before After
Before After
Before After
SUBTRACT Tax FROM GrossPay, Total. 120 4000 9120 120
3880
9000
SUBTRACT Tax, 80 FROM Total. 100 480 100
300
SUBTRACT Tax FROM GrossPay GIVING NetPay. 750 1000 0012 750 0250
1000
SUBTRACT Before
After
WS00-IN1 WS00-OUT1
111 1111
WS00-IN1 WS00-OUT1
111 1000
Before WS00-IN1 WS00-OUT2
111 2222
After WS00-IN1 WS00-OUT2
Before WS00-IN1 WS00-IN2 WS00-OUT3 WS00-OUT4
After 111 555 6666 9666
WS00-IN1 WS00-IN2 WS00-OUT3 WS00-OUT4
111 555 6666 6000
111 2000
MULTIPLY VERB The MULTIPLY statement multiplies numeric items and sets the values of data items equal to the results
Syntax MULTIPLY { identifier-1, literal-1 } BY identifier-2 [ identifier-3 ] . . . [ , GIVING identifier-4 [ , identifier-5 ] . . . ]
MULTIPLY and DIVIDE Examples MULTIPLY Subs BY Members GIVING TotalSubs ON SIZE ERROR DISPLAY "TotalSubs too small" END-MULTIPLY. Subs Before After
Before After
Before After
15.50 15.50
Members
TotalSubs
100
0123.45
100
1550.00
MULTIPLY 10 BY Magnitude, Size. 355 125 3550
1250
DIVIDE Total BY Members GIVING Average ROUNDED. 9234.55 100 1234.56 9234.55 92.35
100
MULTIPLY
Before WS00-IN1 WS00-OUT1
100 1111
After WS00-IN1 WS00-OUT1
Before WS00-IN2 WS00-OUT2 WS00-OUT3
100 1000
After 10 0222 0666
WS00-IN2 WS00-OUT2 WS00-OUT3
10 0222 2220
DIVIDE VERB The DIVIDE statement divides one numeric data item into or by other(s) and sets the values of data items equal to the quotient and remainder. Syntax-1 DIVIDE { identifier-1, literal-1 } INTO identifier-2 [ , identifier-2 ] . . . [ GIVING identifier-4 [ , identifier-5 ] . . . ] .. Syntax-2 DIVIDE { identifier-1, literal-1 } BY { identifier-2, literal-2 } GIVING identifier-3 [ , identifier-4 ]. Syntax-3 DIVIDE { identifier-1, literal-1 } { INTO , BY } { identifier-2, literal-2 } GIVING identifier-3 REMAINDER identifier-4.
DIVIDE 201 BY 10 GIVING Quotient REMAINDER Remain. Before After
020
001
DIVIDE
Before WS00-IN1 WS00-OUT1
100 1000
After WS00-IN1 WS00-OUT1
Before WS00-IN2 WS00-OUT2 WS00-OUT3
100 0010
After 10 2000 00
WS00-IN2 WS00-OUT2 WS00-OUT3
10 0008 16
The COMPUTE COMPUTE { Identifier [ ROUNDED ]} ... = ArithmeticExpression ON SIZE ERROR StatementBlock END - COMPUTE NOT ON SIZE ERROR
Precedence Rules.
Before After
1.
**
=
POWER
NN
2.
* /
= =
MULTIPLY DIVIDE
x ÷
3.
+ -
= =
ADD SUBTRACT
+ -
Compute IrishPrice = SterlingPrice / Rate * 100. 1000.50 156.25 87 179.59 87
156.25
Computational Exceptions – SIZE ERROR
On Size error
Divide by zero
The ROUNDED option Receiving Field
Actual Result Truncated Result
PIC 9(3)V9.
123.25
123.2
PIC 9(3).
123.25
123
Rounded Result
123.3 123
The ROUNDED option takes effect when, after decimal point alignment, the result calculated must be truncated on the right hand side.
The option adds 1 to the receiving item when the leftmost truncated digit has an absolute value of 5 or greater.
The ON SIZE ERROR option Receiving Field PIC 9(3)V9. PIC 9(3)V9.
Actual Result 245.96 1245.9
PIC 9(3).
124
PIC 9(3).
1246
PIC 9(3)V9 Not Rounded
124.45
PIC 9(3)V9 Rounded
124.45
PIC 9(3)V9 Rounded
3124.45
SIZE ERROR
Yes Yes No Yes Yes No Yes
A size error condition exists when, after decimal point alignment, the result is truncated on either the left or the right hand side.
If an arithmetic statement has a rounded phrase then a size error only occurs if there is truncation on the left hand side (most significant digits).
Before WS00-IN1 WS00-IN2 WS00-IN3
12.34 12.34
After WS00-IN1 WS00-IN2 WS00-IN3
Before WS00-IN1 WS00-IN2 WS00-IN4
12.34 12.34 24.6
After 12.34 12.34
WS00-IN1 WS00-IN2 WS00-IN4
12.34 12.34 24.7
‘ON SIZE ERROR’ option Syntax Arithmetic statement [ON SIZE ERROR imperative statement . . .]
Examples (1) ADD NUM-1, NUM-2, NUM-3 TO NUM-4 ON SIZE ERROR PERFORM 900-EXIT-PARA. (2) DIVIDE NUM-1 BY NUM-2 ON SIZE ERROR PERFORM 800-ERROR-PARA.
Before WS00-IN1 WS00-IN2 WS00-IN3 WS00-ERROR-IN3
12.34 12.34 0 N
After WS00-IN1 WS00-IN2 WS00-IN3 WS00-ERROR-IN3
Before WS00-IN1 WS00-IN2 WS00-IN4 WS00-ERROR-IN4
12.34 12.34 00 Y
After 1234 1234 246 N
WS00-IN1 WS00-IN2 WS00-IN4 WS00-ERROR-IN4
1234 1234 246 N
JUSTIFIED RIGHT clause .. example
*********************************************
Output SPOOL
WS00-OUT1 : ABCDEFGHIJKLMNOPQRSTUVWXYZ WS00-OUT2 :
ABCDEFGHIJKLMNOPQRSTUVWXYZ
*********************************************
MOVE CORRESPONDING •
Facilitates movement of value of sub-item of a group item to a similar named sub-item of another group item
Syntax MOVE { CORRESPONDING, CORR } identifier-1 TO identifier-2 where identifier-1 and identifier-2 are group items.
MOVE CORRESPONDING .. example
Output SPOOL
**************************** WS00-GR2 : NISHANT
00000
****************************
MOVE . . . OF . . . TO . . . OF Facilitates the movement of a particular field of a record to a particular field of another record. (in other words it facilitates movement of value of a individual/group item of one group item to an individual/group item of another group item). Example: MOVE NAME OF STUD-REC TO WS-NAME OF WS-STUD-REC.
LEGAL MOVES
Sending field
Receiving field Alphabe tic
Alphanu meric
Edited Alpha numer ic
Numeri c
Numeri c non integer
Edited numeri c
Alphabetic
Y
Y
Y
N
N
N
Alphanumeri c
Y
Y
Y
Y
Y
Y
Edited Alphanumeri c
Y
Y
Y
N
N
N
Numeric
N
Y
Y
Y
Y
Y
Numeric non integer
N
N
N
Y
Y
Y
Edited
N
Y
Y
Y
Y
Y