Anda di halaman 1dari 42

COBOL (COMMON BUSINESS

ORIENTED LANGUAGE)
Overview
COBOL Fundamentals
DAY2
EDITING PICTURE
CLAUSE
Edit symbol Meaning


Z Zero suppression

* Check protection

, Comma insertion

- Minus sign insertion

+ Plus or minus sign insertion
Edited picture symbols
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
ZZ999 12 012

***99 12 ***12

$9999 123 $0123

-9999 -1234 -1234

+9999 -1234 -1234
Edit symbol Value Edited value
Edited picture symbols (Example)
999CR -12 012CR

999CR 12 012CR

999.9 12.34 012.3

99,999 1234 01,234

99B99 1234 12 34
Edit symbol Value Edited value
Edited picture symbols (Example)
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 PIC X(3).

10 POLICY-NO 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).


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 PIC A(15).
10 MIDDLE-NAME PIC A(10).
10 LAST-NAME PIC A(10).
05 NAME REDEFINES STUD-NAME.
REDEFINES Clause
REDEFINES CLAUSE example
************************************
YEAR FOR ENTERED DATE IS : 2005
MONTH FOR ENTERED DATE IS : 01
DAY FOR ENTERED DATE IS : 01
************************************
Output SPOOL
WS00-YEAR2 redefines WS00-YEAR1.
It is the same 8 bytes of
information which WS00-YEAR2
provides in the Year, Month & Day
format in its sub-items.
Any change in WS00-YEAR1 changes
value of WS00-YEAR2 and vice-versa.
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


01 STUD-DETAILS.
05 REG-NO PIC 9(5).
05 S-F-NAME PIC X(15).
05 S-M-NAME PIC X(12).
05 S-L-NAME PIC X(8).

66 STUD-NAME RENAMES S-F-NAME THRU S-L-NAME.
RENAMES Clause
The elementary
items getting
renamed should be
contiguous.
Must be coded only with
level number 66.

RENAMES clause .. example
*********************************
WS-REN VALUE IS : 341234
*********************************
Output SPOOL
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.
Data movement verb.

Arithmetic Verbs.

Input / Output Verbs.

Sequence control verbs.



Procedure Division VERBS
ADD

SUBTRACT

MULTIPLY

DIVIDE

COMPUTE
Arithmetic VERBS
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 VERB
ADD Examples
ADD Cash TO Total.
Before 3 1000
After

ADD Cash, 20 TO Total, Wage.
Before 3 1000 100
After

ADD Cash, Total GIVING Result.
Before 3 1000 0015
After

ADD Males TO Females GIVING TotalStudents.
Before 1500 0625 1234
After

3 1003



3 1023 123



3 1000 1003


1500 0625 2125

Before

WS00-IN1 123
WS00-OUT1 0000
After

WS00-IN1 123
WS00-OUT1 0123
Before

WS00-IN1 123
WS00-OUT2 0000
After

WS00-IN1 123
WS00-OUT2 0223
Before

WS00-IN1 123
WS00-IN2 456
WS00-OUT2 0000
After

WS00-IN1 123
WS00-IN2 456
WS00-OUT2 0579
ADD
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 VERB
SUBTRACT Examples
SUBTRACT Tax FROM GrossPay, Total.
Before 120 4000 9120
After

SUBTRACT Tax, 80 FROM Total.
Before 100 480
After

SUBTRACT Tax FROM GrossPay GIVING NetPay.
Before 750 1000 0012
After

120 3880 9000



100 300




750 1000
0250
Before

WS00-IN1 111
WS00-OUT1 1111
After

WS00-IN1 111
WS00-OUT1 1000
Before

WS00-IN1 111
WS00-OUT2 2222
After

WS00-IN1 111
WS00-OUT2 2000
Before

WS00-IN1 111
WS00-IN2 555
WS00-OUT3 6666
WS00-OUT4 9666
After

WS00-IN1 111
WS00-IN2 555
WS00-OUT3 6666
WS00-OUT4 6000
SUBTRACT
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 VERB
MULTIPLY and DIVIDE Examples
MULTIPLY Subs BY Members GIVING TotalSubs
ON SIZE ERROR DISPLAY "TotalSubs too small"
END-MULTIPLY.
Subs Members TotalSubs
Before 15.50 100 0123.45
After


MULTIPLY 10 BY Magnitude, Size.
Before 355 125
After


DIVIDE Total BY Members GIVING Average ROUNDED.
Before 9234.55 100 1234.56
After
15.50 100 1550.00




3550 1250





9234.55 100 92.35
Before

WS00-IN1 100
WS00-OUT1 1111
After

WS00-IN1 100
WS00-OUT1 1000
Before

WS00-IN2 10
WS00-OUT2 0222
WS00-OUT3 0666
After

WS00-IN2 10
WS00-OUT2 0222
WS00-OUT3 2220
MULTIPLY
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 VERB
DIVIDE 201 BY 10 GIVING Quotient REMAINDER Remain.
Before
After
020 001
Before

WS00-IN1 100
WS00-OUT1 1000
After

WS00-IN1 100
WS00-OUT1 0010
Before

WS00-IN2 10
WS00-OUT2 2000
WS00-OUT3 00
After

WS00-IN2 10
WS00-OUT2 0008
WS00-OUT3 16
DIVIDE
Compute IrishPrice = SterlingPrice / Rate * 100.
Before 1000.50 156.25 87
After


179.59 156.25 87
The COMPUTE
{ } COMPUTE Identifier [ ROUNDED ] . . . = ArithmeticExpression

ON SIZE ERROR
NOT ON SIZE ERROR
StatementBlock END- COMPUTE


`
)

(
Precedence Rules.
1. ** = POWER N
N
2. * = MULTIPLY x
/ = DIVIDE
3. + = ADD +
- = SUBTRACT -
Computational Exceptions SIZE ERROR
On Size error

Divide by zero
The ROUNDED option
Receiving Field Actual Result Truncated Result Rounded Result
PIC 9(3)V9. 123.25
PIC 9(3). 123.25
123.2 123.3
123 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.
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).
Receiving Field Actual Result SIZE ERROR
PIC 9(3)V9. 245.96
PIC 9(3)V9. 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
The ON SIZE ERROR option
Yes
Yes
No
Yes
Yes
No
Yes
Before

WS00-IN1 12.34
WS00-IN2 12.34
WS00-IN3
After

WS00-IN1 12.34
WS00-IN2 12.34
WS00-IN3 24.6
Before

WS00-IN1 12.34
WS00-IN2 12.34
WS00-IN4
After

WS00-IN1 12.34
WS00-IN2 12.34
WS00-IN4 24.7
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.
ON SIZE ERROR option
Before

WS00-IN1 12.34
WS00-IN2 12.34
WS00-IN3 0
WS00-ERROR-IN3 N
After

WS00-IN1 12.34
WS00-IN2 12.34
WS00-IN3 00
WS00-ERROR-IN3 Y
Before

WS00-IN1 1234
WS00-IN2 1234
WS00-IN4 246
WS00-ERROR-IN4 N
After

WS00-IN1 1234
WS00-IN2 1234
WS00-IN4 246
WS00-ERROR-IN4 N
JUSTIFIED RIGHT clause .. example
*********************************************
WS00-OUT1 : ABCDEFGHIJKLMNOPQRSTUVWXYZ
WS00-OUT2 : ABCDEFGHIJKLMNOPQRSTUVWXYZ
*********************************************
Output SPOOL
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
MOVE CORRESPONDING .. example
****************************
WS00-GR2 : NISHANT 00000
****************************
Output SPOOL
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.
MOVE . . . OF . . . TO . . . OF
Alphabet
ic
Alphanu
meric
Edited
Alphan
umeric
Numeric Numeric
non
integer
Edited
numeric
Alphabetic Y Y Y N N N
Alphanumeric Y Y Y Y Y Y
Edited
Alphanumeric
Y Y Y N N N
Numeric N Y Y Y Y Y
Numeric non
integer
N N N Y Y Y
Edited numeric N Y Y Y Y Y
Receiving field
S
e
n
d
i
n
g

f
i
e
l
d

LEGAL MOVES

Anda mungkin juga menyukai