COBOLDay 2
COBOLDay 2
DAY 2
Agenda for Day2
Data Movement verbs.
Types of conditions.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 2
Technologies Ltd Version 1.0
MOVE VERB
• The MOVE copies data from the source identifier or literal to one or more
destination identifiers.
• When data is MOVEd into an item the contents of the item are completely
replaced. If the source data is too small to fill the destination item entirely
the remaining area is zero or space filled.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 3
Technologies Ltd Version 1.0
MOVE verb…
Identifier
MOVE TO { Identifier} ...
Literal
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 4
Technologies Ltd Version 1.0
DATA movement verbs…
(1) MOVE
(3) MOVE . . . OF . . . TO . . . OF
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 5
Technologies Ltd Version 1.0
Before Before
Before
WS00-OUT3 0786
After
WS00-OUT3 2345
Before After
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 6
Technologies Ltd Version 1.0
MOVE to a numeric item
• When the destination item is numeric, or edited numeric, then data is aligned
along the decimal point with zero filling or truncation as necessary.
• When the decimal point is not explicitly specified in either the source or
destination items, the item is treated as if it had an assumed decimal point
immediately after its rightmost character.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 7
Technologies Ltd Version 1.0
Before Before
Before
WS00-OUT3 000000
After
WS00-OUT3 123456
Before After
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 8
Technologies Ltd Version 1.0
MOVE .. example
****************************
Example
bbbbbKAJOL
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 10
Technologies Ltd Version 1.0
JUSTIFIED RIGHT clause .. example
*********************************************
Syntax
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 12
Technologies Ltd Version 1.0
MOVE CORRESPONDING .. example
****************************
Output SPOOL WS00-GR2 : NISHANT 00000
****************************
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 13
Technologies Ltd Version 1.0
MOVE . . . OF . . . TO . . . OF
Example:
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 14
Technologies Ltd Version 1.0
LEGAL MOVES
Certain combinations of sending and receiving data types are not permitted.
Receiving field
Alphabetic Alphanu Edited Numeric Numeric Edited
meric Alphan non numeric
umeric integer
Alphabetic Y Y Y N N N
Sending field
Alphanumeric Y Y Y Y Y Y
Edited Y Y Y N N N
Alphanumeric
Numeric N Y Y Y Y Y
Numeric non N N N Y Y Y
integer
Edited N Y Y Y Y Y
numeric
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 15
Technologies Ltd Version 1.0
SEQUENCE CONTROL verbs
• GOTO
• IF . . . THEN . . .
• PERFORM
• EVALUATE
• STOP RUN
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 16
Technologies Ltd Version 1.0
GO TO Verb
• Syntax-1
GO TO Paragraph Name.
• Example
GO TO 400-READ-PARA.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 17
Technologies Ltd Version 1.0
GO TO . . . DEPENDING ON . . .
• Syntax-2
GO TO paragraph-name-1 [paragraph-name-2 ]
. . . DEPENDING ON identifier.
• Example
GO TO 500-INSERT-PARA, 600-UPDATE-PARA,
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 18
Technologies Ltd Version 1.0
IF statement
• Syntax-1
IF condition [ THEN ] {statement-1, NEXT SENTENCE}
[ELSE {statement-2, NEXT SENTENCE}]
[ END-IF ].
• Examples
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 19
Technologies Ltd Version 1.0
IF statement
• Syntax-2 ( Nested IF )
IF condition-1 [ THEN ] statement-1
ELSE
IF condition-2 [ THEN ] statement-2
ELSE statement-3
END-IF
END-IF.
• Example
IF ( Var1 < 10 ) THEN DISPLAY “Zero”
ELSE
IF Var2 = 14 THEN DISPLAY “First”
ELSE DISPLAY “Second”
END-IF
END-IF.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 20
Technologies Ltd Version 1.0
IF statement - Implied Operands
• Example
Is equivalent to
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 21
Technologies Ltd Version 1.0
Classification of Conditions
• Relational condition
• Sign condition
• Class condition
• Compound condition
• Condition-name
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 22
Technologies Ltd Version 1.0
Relational condition
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 23
Technologies Ltd Version 1.0
Sign condition
• Syntax
POSITIVE
Arithmetic Expression IS [ NOT] NEGATIVE
ZERO
• Example
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 24
Technologies Ltd Version 1.0
Class condition
• Syntax
NUMERIC
ALPHABETIC
Identifier IS [NOT] ALPHABETIC - LOWER
ALPHABETIC - UPPER
UserDefinedClassName
• Example
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 25
Technologies Ltd Version 1.0
Compound Condition
• Syntax
• Examples
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 26
Technologies Ltd Version 1.0
Condition Names
• Syntax
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 27
Technologies Ltd Version 1.0
Condition-Names .. example
Condition Variable
• Condition Names are defined using the special level number 88 in the DATA
DIVISION of a COBOL program.
• They are defined immediately after the definition of the data item with which
they are associated with.
• A condition name takes the value TRUE or FALSE depending on the value
of the data item with which it is associated. The VALUE clause of the
associated data item is used to identify the values which make the Condition
Name TRUE.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 29
Technologies Ltd Version 1.0
JCL
000100 //ER4857C JOB ,,NOTIFY=&SYSUID,CLASS=B
000500 //STEP1 EXEC PGM=COND88
000700 //STEPLIB DD DSN=OPERN.CICS3.LOADLIB,DISP=SHR
000800 //SYSIN DD *
000900 050
Before
001000 081
001100 /*
WS00-MARKS 000
WS00-DISP
After
WS00-MARKS 050
WS00-DISP NOT CLEARED COMPRE
Before After
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 31
Technologies Ltd Version 1.0
The PERFORM Verb
• Iteration constructs are used when we need to repeat the same instructions
over and over again in our programs.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 32
Technologies Ltd Version 1.0
Paragraphs - Revisited
• Paragraph names are either user defined or language enforced. They are
followed by a full stop.
– B0000-PERF-PARA.
– PROGRAM-ID.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 33
Technologies Ltd Version 1.0
Paragraph Example
P0000-PROCESS-RECORD.
DISPLAY StudentRecord
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ.
D0000-PRODUCE-OUTPUT.
DISPLAY “Here is a message”.
NOTE
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 34
Technologies Ltd Version 1.0
PERFORM Verb - variations
• Simple PERFORM
• In-line PERFORM
• Nested PERFORM
• PERFORM . . . THRU
• PERFORM . . . UNTIL
• PERFORM . . . TIMES
• PERFORM . . . VARYING
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 35
Technologies Ltd Version 1.0
PERFORM Verb - Simple PERFORM
• Syntax
PERFORM Paragraph-Name.
• Example
PERFORM 500-PROCESS-PARA.
• This is not iterative but instructs the computer to execute the chunk of code
inside the mentioned paragraph before reverting back to the sentence
following the PERFORM coded.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 36
Technologies Ltd Version 1.0
PERFORM Verb – Simple PERFORM example
****************************************
WE ARE INSIDE B000-LAST-PARA
Output SPOOL WE ARE INSIDE B001-FIRST-PARA
WE ARE INSIDE B002-MIDDLE-PARA
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 37
Technologies Ltd Version 1.0
****************************************
PERFORM Verb - In-line PERFORM
• Syntax
PERFORM imperative-statements.
• Example
PERFORM
MOVE NUM-1 TO MAX
IF NUM-2 > MAX THEN MOVE NUM-2 TO MAX
DISPLAY “Maximum is ” MAX.
END-PERFORM
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 38
Technologies Ltd Version 1.0
INLINE PERFORM PROGRAM
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 39
Technologies Ltd Version 1.0
JCL FOR THE INLINE
PERFORM PROGRAM
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 40
Technologies Ltd Version 1.0
When SYSIN data satisfies
the condition WS-STRING =
‘KARINA’ the scope of the
INLINE PERFORM gets
terminated
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 41
Technologies Ltd Version 1.0
PERFORM Verb – Nested PERFORM
• Syntax
Paragraph-Name-1.
PERFORM Paragraph-Name-2.
. . . . .
. . . . .
Paragraph-Name-2.
PERFORM Paragraph-Name-3.
. . . . .
. . . . .
Paragraph-Name-3.
MOVE A TO B.
. . . . .
. . . . .
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 42
Technologies Ltd Version 1.0
PERFORM Verb – Nested PERFORM
****************************************
WE ARE INSIDE B000-LAST-PARA
Output SPOOL WE ARE INSIDE B001-FIRST-PARA
WE ARE INSIDE B002-MIDDLE-PARA
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 43
Technologies Ltd Version 1.0
****************************************
PERFORM Verb – PERFORM … THRU …
• Syntax
Paragraph-Name-2 ].
• Example
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 44
Technologies Ltd Version 1.0
PERFORM … THRU … - example
****************************
WE ARE INSIDE B000-DISP-PARA
Output SPOOL WE ARE INSIDE B001-DISP-PARA
WE ARE INSIDE B002-DISP-PARA
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 45
Technologies Ltd ****************************
Version 1.0
PERFORM Verb – PERFORM .. UNTIL ..
• Syntax
• Example
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 46
Technologies Ltd Version 1.0
PERFORM Verb –
PERFORM . . UNTIL .. WITH TEST AFTER OPTION
• Syntax
Paragraph-Name-2 ]
[WITH TEST {BEFORE, AFTER}]
UNTIL condition.
• Example
PERFORM 300-PROCESS-PARA WITH TEST AFTER
UNTIL VALUE NOT = 0.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 47
Technologies Ltd Version 1.0
PERFORM . . UNTIL .. WITH TEST AFTER OPTION
• This format is used where the WHILE or REPEAT constructs are used in
other languages.
• If the WITH TEST BEFORE phrase is used the PERFORM behaves like a
WHILE loop and the condition is tested before the loop body is entered.
• If the WITH TEST AFTER phrase is used the PERFORM behaves like a
REPEAT loop and the condition is tested after the loop body is entered.
• The WITH TEST BEFORE phrase is the default and so is rarely explicitly
stated.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 48
Technologies Ltd Version 1.0
PERFORM Verb –
PERFORM . . UNTIL .. WITH TEST BEFORE
****************************
Output SPOOL
****************************
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 49
Technologies Ltd Version 1.0
PERFORM Verb –
PERFORM . . UNTIL .. WITH TEST AFTER
10 Times!! Why?
****************************
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
Output SPOOL WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
WE ARE INSIDE B000-PERF-PARA
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 50 WE ARE INSIDE B000-PERF-PARA
Technologies Ltd Version 1.0
****************************
PERFORM Verb – PERFORM .. TIMES
• Syntax
• Example
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 51
Technologies Ltd Version 1.0
PERFORM Verb – PERFORM .. TIMES …… Example
****************************
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
• Example
PERFORM 500-WRITE-PARA
VARYING I FROM 1 BY 1
UNTIL I > 5.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 53
Technologies Ltd Version 1.0
PERFORM Verb - PERFORM . . . VARYING
****************************
HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
Output SPOOL HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
****************************
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 54
Technologies Ltd Version 1.0
EXIT statement
Syntax
EXIT.
Note:
It is recommended to avoid using EXIT similar to GO TO since it is against the
idea of structured programming.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 55
Technologies Ltd Version 1.0
EVALUATE Verb
• The EVALUATE verb provides a very powerful construct to carry out DATA
validation. It is similar to the SWITCH statement in C programs.
It assists us in implementing decision table logic.
• Syntax
imperative-statement-1 } . . .
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 56
Technologies Ltd Version 1.0
The Evaluate
Identifier
Literal
CondExpression
EVALUATE
ArithExpression
TRUE
FALSE
ANY
Condition
WHEN TRUE StatementBlock
FALSE
Identifier Identifier
THRU
[ NOT] Literal
THROUGH Literal
ArithExpression ArithExpression
[ WHEN OTHER StatementBlock]
END - EVALUATE
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 57
Technologies Ltd Version 1.0
EVALUATE Verb .. example
1) Marks > 79
2) Marks > 64 & <= 79
*************************************
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 58
Technologies Ltd Version 1.0
STOP RUN statement
• Closes all the files that were opened for file operations.
• The STOP RUN is usually the last statement in the main paragraph.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 59
Technologies Ltd Version 1.0
REDEFINES Clause
• Facilitates two or more data-names to point to the same memory location
Syntax
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.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 60
Technologies Ltd Version 1.0
REDEFINES Clause
Must not be used for data-items defined in level numbers 66 and 88.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 61
Technologies Ltd Version 1.0
REDEFINES CLAUSE … example
************************************
YEAR FOR ENTERED DATE IS : 2005
Syntax
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 63
Technologies Ltd Version 1.0
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).
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 64
Technologies Ltd Version 1.0
RENAMES clause .. example
*********************************
Output SPOOL WS-REN VALUE IS : 341234
*********************************
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 65
Technologies Ltd Version 1.0
USAGE Clause
• Syntax
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 66
Technologies Ltd Version 1.0
USAGE Clause
• USAGE is DISPLAY
– Each character of the data is represented in one byte
• USAGE IS COMPUTATIONAL
– The data item is represented as pure binary
• USAGE IS COMP-1
– The data item is represented as a single precision floating point number
(similar to real or float).
• USAGE IS COMP-2
– The data item is represented internally as Double precision floating
number (similar to Long or Double).
• USAGE IS COMP-3
– In decimal form but 1 digit takes half a byte (nibble). The sign is stored as
right most half a byte character.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 67
Technologies Ltd Version 1.0
USAGE Clause
Usage clause when declared for a group item, ensures that all the
sub-items of the group item default to the same USAGE clause as the
group item’s.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 68
Technologies Ltd Version 1.0
USAGE Clause .. example
B0002-DISPLAY-FIELDS.
B0001-POPULATE-FIELDS.
DISPLAY '******************************************'
MOVE 99999 TO WS00-COMP-FORM
DISPLAY '* COMP DISPLAY IS : ' WS00-COMP-FORM
MOVE 99999 TO WS00-COMP1-FORM
DISPLAY '* COMP1 DISPLAY IS : ' WS00-COMP1-FORM
MOVE 99999 TO WS00-COMP2-FORM
DISPLAY '* COMP2 DISPLAY IS : ' WS00-COMP2-FORM
MOVE 99999 TO WS00-COMP3-FORM
DISPLAY '* COMP3 DISPLAY IS : ' WS00-COMP3-FORM
DISPLAY '******************************************'
******************************************
* COMP DISPLAY IS : 99999
* COMP1 DISPLAY IS : .99999000E 05
Output SPOOL
* COMP2 DISPLAY IS : .99999000000000000E 05
* COMP3 DISPLAY IS : 99999
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 69
******************************************
Technologies Ltd Version 1.0
Review
Data Movement verbs. (MOVE)
Types of conditions.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 70
Technologies Ltd Version 1.0
Review questions
• What is wrong with the following code
(1) IF A EQUALS B
MOVE 1 TO A
END –IF
This should be ; IF A IS EQUAL TO B
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 71
Technologies Ltd Version 1.0
Review questions ..
• How many times will the paragraph named
100-PARA be executed by the following PERFORM STATEMENT
PERFORM 100-PARA VARYING X
FROM 1 BY 1 UNTIL X=10 9 TIMES
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 72
Technologies Ltd Version 1.0
Review questions
• State true or false
– The justified clause can be used for any data type False
True
– The data item at the level 49 will always have a picture
clause
True
– In a move statement, though the sending field is one, the
receiving fields may be more than one.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 73
Technologies Ltd Version 1.0
Summary
• Data Movement verbs.
• Types of conditions.
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 74
Technologies Ltd Version 1.0
Thank You!
ER/CORP/CRS/LA01/003
Copyright © 2005, Infosys 75
Technologies Ltd Version 1.0