Sie sind auf Seite 1von 42

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 Blank insertion when the value is
ZERO 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 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).
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 PIC A(15).
10 MIDDLE-NAME PIC A(10).
10 LAST-NAME PIC A(10).
05 NAME REDEFINES STUD-NAME.
REDEFINES CLAUSE … WS00-YEAR2
example redefines WS00-YEAR1.
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 : 2005

Output SPOOL 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. contiguous.
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.

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

ADD Cash TO Total.


Before 3 1000
After 3 1003

ADD Cash, 20 TO Total, Wage.


Before 3 1000 100
After
3 1023 123
ADD Cash, Total GIVING Result.
Before 3 1000 0015
After
3 1000 1003
ADD Males TO Females GIVING TotalStudents.
Before 1500 0625 1234
After
1500 0625
2125
ADD

Before After

WS00-IN1 123 WS00-IN1 123


WS00-OUT1 0000 WS00-OUT1 0123

Before

WS00-IN1 123
WS00-OUT2 0000

After

WS00-IN1 123
WS00-OUT2 0223

Before After

WS00-IN1 123 WS00-IN1 123


WS00-IN2 456 WS00-IN2 456
WS00-OUT2 0000 WS00-OUT2 0579
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

SUBTRACT Tax FROM GrossPay, Total.


Before 120 4000 9120
After
120 3880 9000

SUBTRACT Tax, 80 FROM Total.


Before 100 480
After 100 300

SUBTRACT Tax FROM GrossPay GIVING NetPay.


Before 750 1000 0012
After
750 1000
0250
SUBTRACT

Before After

WS00-IN1 111 WS00-IN1 111


WS00-OUT1 1111 WS00-OUT1 1000

Before

WS00-IN1 111
WS00-OUT2 2222

After

WS00-IN1 111
WS00-OUT2 2000

Before After

WS00-IN1 111 WS00-IN1 111


WS00-IN2 555 WS00-IN2 555
WS00-OUT3 6666 WS00-OUT3 6666
WS00-OUT4 9666 WS00-OUT4 6000
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 Members TotalSubs
Before 15.50 100 0123.45
After
15.50 100 1550.00

MULTIPLY 10 BY Magnitude, Size.


Before 355 125
After 3550 1250

DIVIDE Total BY Members GIVING Average ROUNDED.


Before 9234.55 100 1234.56
After
9234.55 100
92.35
MULTIPLY

Before

WS00-IN1 100
WS00-OUT1 1111

After

WS00-IN1 100
WS00-OUT1 1000

Before After

WS00-IN2 10 WS00-IN2 10
WS00-OUT2 0222 WS00-OUT2 0222
WS00-OUT3 0666 WS00-OUT3 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 100
WS00-OUT1 1000

After

WS00-IN1 100
WS00-OUT1 0010

Before After

WS00-IN2 10 WS00-IN2 10
WS00-OUT2 2000 WS00-OUT2 0008
WS00-OUT3 00 WS00-OUT3 16
The
COMPUTE

COMPUTE { Identifier [ ROUNDED ]} ... = ArithmeticExpression


  ON SIZE ERROR  
  StatementBlock END - COMPUTE 
  NOT ON SIZE ERROR  

Precedence Rules.
1. ** = POWER NN
2. * = MULTIPLY x
/ = DIVIDE ÷
3. + = ADD +
- = SUBTRACT -

Compute IrishPrice = SterlingPrice / Rate * 100.


Before 1000.50 156.25 87
After 179.59 156.25
87
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 123.2 123.3


PIC 9(3). 123.25 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.
The ON SIZE ERROR
option
Receiving Field Actual Result SIZE ERROR
PIC 9(3)V9. 245.96
Yes
PIC 9(3)V9. 1245.9
Yes
PIC 9(3). 124
No
PIC 9(3). 1246
Yes
PIC 9(3)V9 Not Rounded 124.45
Yes
PIC 9(3)V9 Rounded 124.45
No
PIC 9(3)V9 Rounded 3124.45
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 12.34
WS00-IN2 12.34
WS00-IN3

After

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

Before After

WS00-IN1 12.34 WS00-IN1 12.34


WS00-IN2 12.34 WS00-IN2 12.34
WS00-IN4 WS00-IN4 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 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 After

WS00-IN1 1234 WS00-IN1 1234


WS00-IN2 1234 WS00-IN2 1234
WS00-IN4 246 WS00-IN4 246
WS00-ERROR-IN4 N WS00-ERROR-IN4 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

Receiving field
Alphabe Alphanu Edited Numeri Numeri Edited
tic meric Alpha c c non numeri
numer integer c
ic

Alphabetic Y Y Y N N N
Sending field

Alphanumeri Y Y Y Y Y Y
c

Edited Y Y Y N N N
Alphanumeri
c
Numeric N Y Y Y Y Y

Numeric non N N N Y Y Y
integer

Edited N Y Y Y Y Y

Das könnte Ihnen auch gefallen