Upload
api-3838727
View
960
Download
0
Embed Size (px)
Citation preview
COBOL (COMMON BUSINESS ORIENTED
LANGUAGE)
Overview
COBOL Fundamentals
DAY3
SEQUENCE CONTROL verbs
GO TO
IF . . . THEN . . .
PERFORM
EVALUATE
STOP RUN
GO TO Verb
Syntax-1
GO TO paragraph-name.
Example
GO TO 400-READ-PARA.
GO TO . .
Syntax-2
GO TO paragraph-name-1 [paragraph-name-2
]Example
GO TO 500-INSERT-PARA, 600-UPDATE-PARA, 700-DELETE-PARA DEPENDING ON TRANS-CODE.
IF statement
Syntax-1
IF condition [ THEN ] {statement-1, NEXT SENTENCE}
[ELSE {statement-2, NEXT SENTENCE}]
[ END-IF ].
Examples
(1) IF MARKS >= 80 THEN MOVE ‘A’ TO GRADEELSE MOVE ‘B’ TO GRADE
END-IF.
(2) IF NOT OK-BALANCE THEN MOVE 2 TO BALANCE-CODE ELSE NEXT-SENTENCE END-IF
Relation Conditions
Identifier
Literal
ArithmeticExpression
Identifier
Literal
ArithmeticExpression
IS
NOT GREATER THAN
NOT >
NOT LESS THAN
NOT <
NOT EQUAL TO
NOT =
GREATER THAN OR EQUAL TO
>=
LESS THAN OR EQUAL TO
<=
IF statement
Syntax-2 ( Nested IF )
IF condition-1 [ THEN ] statement-1ELSE
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.
Sign condition
Syntax
ZERO
NEGATIVE
POSITIVE
]NOT[ IS Expression Arithmetic
Example
IF DISCRIMINANT IS NEGATIVE THEN DISPLAY “The roots are imaginary”.
Class condition
Syntax
Identifier IS [NOT]
NUMERIC
ALPHABETIC
ALPHABETIC - LOWER
ALPHABETIC - UPPER
UserDefinedClassName
Example
IF REGNO IS NOT NUMERIC
THEN DISPLAY “Records will not be sorted”.
Compound Condition
Syntax
Condition-1 { AND, OR } Condition-2
Examples
(1) IF PERCENT > 80 AND TOTAL > 480
THEN MOVE ‘A’ TO GRADE.
(2) IF ROW-NUMBER > 24 OR COLUMN > 80
THEN DISPLAY “Page Error ! “.
Defining Condition Names.
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.
We can use Condition Names for a group as well as an elementary item.
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.
88 ConditionName VALUE
VALUES
Literal
LowValue THROUGH
THRU HighValue
Condition Names
Are essentially boolean variables.
Are always associated with data names called condition variables.
Is defined in the DATA DIVISION with levelnumber 88.
Syntax
88 condition-name {VALUE IS, VALUES ARE } literal-1 [ { THRU, THROUGH } literal-2 ].
Condition-Names .. example
01 MARITAL-STATUS PIC 9.
88 SINGLE VALUE IS ZERO. 88 MARRIED VALUE IS 1. 88 WIDOWED VALUE IS 2. 88 DIVORCED VALUE IS 3. 88 ONCE-MARRIED VALUES ARE 1, 2, 3. 88 VALID-STATUS VALUES ARE 0 THRU 3.
PROCEDURE DIVISION Statements.DISPLAY ‘ENTER MARTIAL STATUS.:’.ACCEPT MARITAL-STATUS.IF SINGLE SUBTRACT 125 FROM DEDUCTIONS.IF ONCE-MARRIED ADD 300 TO SPECIAL-PAY.IF MARRIED PERFORM B000-MARRIAGE-GIFT.
Condition Names
Martial-status = 0
Martial-status = 2
Martial-status = 1
Before
WS00-MARKS 000
WS00-DISP
After
WS00-MARKS 050
WS00-DISP NOT CLEARED COMPRE
Before
WS00-MARKS 000
WS00-DISP
After
WS00-MARKS 081
WS00-DISP PASSED COMPRE
JCL000100 //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
001000 081
001100 /*
The PERFORM Verb
Iteration constructs are used when we need to repeat the same instructions over and over again in our programs.
Other programming languages have a variety of iteration / looping constructs (e.g. WHILE, FOR, REPEAT). Each of these in turn facilitate the creation of different ‘types’ of iteration structure.
In COBOL we have ‘PERFORM’ verb which is used to create these looping constructs. The PERFORM has several variations each of which simulates different looping constructs of other programming languages.
Paragraphs - Revisited
A PARAGRAPH comprises of one or more sentences.
The paragraph-name indicates the start of a paragraph. The next paragraph or section name or the end of the program text terminates the paragraph.
Paragraph names are either user defined or language enforced. They are followed by a full stop.
B0000-PERF-PARA.
PROGRAM-ID.
PERFORM Verb - variations
Simple PERFORM
In-line PERFORM
Nested PERFORM
PERFORM . . . THRU
PERFORM . . . UNTIL
PERFORM . . . TIMES
PERFORM . . . VARYING
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.
PERFORM Verb – Simple PERFORM example
****************************************
WE ARE INSIDE B000-LAST-PARA
WE ARE INSIDE B001-FIRST-PARA
WE ARE INSIDE B002-MIDDLE-PARA
****************************************
Output SPOOL
PERFORM Verb - In-line PERFORM
Syntax
PERFORM imperative-statements.
Example
PERFORM MOVE NUM-1 TO MAXIF NUM-2 > MAX THEN MOVE NUM-2 TO
MAXDISPLAY “Maximum is ” MAX.
END-PERFORM
INLINE PERFORM PROGRAM
JCL FOR THE INLINE PERFORM PROGRAM
When SYSIN data satisfies the condition WS-STRING = ‘KARINA’ the scope of the INLINE PERFORM gets
terminated
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.
. . . . . . . . . .
PERFORM Verb – Nested PERFORM
****************************************
WE ARE INSIDE B000-LAST-PARA
WE ARE INSIDE B001-FIRST-PARA
WE ARE INSIDE B002-MIDDLE-PARA
****************************************
Output SPOOL
PERFORM Verb – PERFORM … THRU …
Syntax
PERFORM Paragraph-Name-1 [ { THRU, THROUGH }
Paragraph-Name-2 ].
Example
PERFORM 300-READ-PARA THRU 600-UPDATE-PARA.
PERFORM … THRU … - example
****************************
WE ARE INSIDE B000-DISP-PARA
WE ARE INSIDE B001-DISP-PARA
WE ARE INSIDE B002-DISP-PARA
****************************
Output SPOOL
PERFORM Verb – PERFORM .. UNTIL ..
Syntax
PERFORM Paragraph-Name-1 [ { THRU, THROUGH }
Paragraph-Name-2 ] UNTIL condition.
Example
PERFORM 300-READ-PARA UNTIL EOF = ‘N’.
PERFORM Verb – PERFORM . . UNTIL .. WITH TEST AFTER OPTION
Syntax
PERFORM Paragraph-Name-1 [ { THRU, THROUGH }
Paragraph-Name-2 ]
[WITH TEST {BEFORE, AFTER}]
UNTIL condition.
ExamplePERFORM 300-PROCESS-PARA WITH TEST AFTER
UNTIL VALUE NOT = 0.
PERFORM Verb …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.
PERFORM Verb – PERFORM . . UNTIL .. WITH TEST BEFORE
****************************
****************************Output SPOOL
PERFORM Verb – PERFORM . . UNTIL .. WITH TEST AFTER
****************************
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
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
10 Times!! Why?
PERFORM Verb – PERFORM .. TIMES
Syntax
PERFORM Paragraph-Name-1 [ { THRU, THROUGH }
Paragraph-Name-2 ] { integer, identifier } TIMES.
Example
PERFORM 500-PROCESS-PARA THRU 800-END-PARA 8 TIMES.
PERFORM Verb – PERFORM .. TIMES …… Example
****************************
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
****************************
Output SPOOL
PERFORM Verb - PERFORM . . . VARYING
Syntax
PERFORM Paragraph-Name-1 [ { THRU, THROUGH }Paragraph-Name-2 ] VARYING identifier-1 FROM{identifier-2, integer-1} BY { identifier-3, integer-2 }
UNTIL condition.
Example
PERFORM 500-WRITE-PARA
VARYING I FROM 1 BY 1
UNTIL I > 5.
PERFORM Verb - PERFORM . . . VARYING
****************************
HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
****************************
Output SPOOL
PERFORM ...VARYING Syntax
PERFORM-ENDlock StatementB
Condition2 UNTIL Literal
6Identifier BY
4
5
FROM IndexName3
4Identifier AFTER
Condition1 UNTIL Literal
3Identifier BY
2
2
FROM IndexName1
Identifer1 VARYING
AFTER
BEFORE TEST WITH EndProc
THROUGH
THRU 1stProc PERFORM
Literal
IndexName
Identifier
Literal
IndexName
Identifier
PERFORM .. VARYING Example
PERFORM .. VARYING Example
Gives the IX1th occurrence of the array
PERFORM .. VARYING Example
OUTPUT SPOOL
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
EVALUATE subject-1 [ ALSO subject-2 ] . . .
{ { WHEN object-1 [ ALSO object-2 ] . . . } . . . }
imperative-statement-1 } . . .
WHEN subject = { identifier, expression, TRUE, FALSE }
and object = { condition, TRUE, FALSE }.
The Evaluate
EVALUATE
Identifier
Literal
CondExpression
ArithExpression
TRUE
FALSE
WHEN
ANY
Condition
TRUE
FALSE
NOT
Identifier
Literal
ArithExpression
THRU
THROUGH
Identifier
Literal
ArithExpression
StatementBlock
WHEN OTHER StatementBlock
END - EVALUATE
EVALUATE Verb .. example
There are two valid ranges
which the logic checks for –
1) Marks > 79
2) Marks > 64 & <= 79
*************************************
YOU HAVE CLEARED EXAM WITH A GRADE
*************************************Output SPOOL
STOP RUN statement
Syntax : STOP RUN.
Instructs the computer to terminate the program.
Closes all the files that were opened for file operations.
The STOP RUN is usually the last statement in the main paragraph.
COBOL DAY 4
Overview
Array
An array is a Linear data structure and is a collection of homogenous data items that can be referred by a single data name.
The data items contained in an array are called its elements.
The elements of an array are internally stored in contiguous memory locations.
The elements of an array may be elementary or group items.
An array can have dimension up to 7 in COBOL-85.
ARRAY – Defining an Array
Similar to any other data item an array is defined in the DATA DIVISION.
To specify the repeated occurrence of data items with the same format, the OCCURS clause is used.
The OCCURS clause specifies the maximum number of elements that can be stored in the array.
The OCCURS clause can be used only with level numbers 02 to 49.
ARRAY – Single dimensional Array
Array Declaration
ARRAY – Multi dimensional Array
Array Declaration
For Output spool display
Output Spool of the
Job
Defining an Array – Using Indexed clause
INDEXED ARRAY
Defining an Array – Using Indexed clause
Output Spool of the
Job
Declaring Tables – An examplePROJECT EMP# EMPNAME
BANKS 11111 SRINI
BANKS 22222 JYOTI
MKTNG 33333 VINAY
MKTNG 44444 SARMA
Declaring Tables – An example
Declaring Tables – An example
DISPLAY '**************************************************'
DISPLAY 'WS00-ORG : ' WS00-ORG
DISPLAY 'WS00-IBU FIRST OCCUR : ' WS00-IBU(1)
DISPLAY 'WS00-PROJ FIRST OCCUR : ' WS00-PROJ(1,1)
DISPLAY 'WS00-EMP : ' WS00-EMP(1,1)
DISPLAY 'WS00-EMPNAME : ' WS00-EMPNAME(1,1)
DISPLAY '**************************************************'
PROJECT EMP# EMPNAME
BANKS 11111 SRINI
BANKS 22222 JYOTI
MKTNG 33333 VINAY
MKTNG 44444 SARMA
Accessing the elements of an array
Can be done using the data-name that is lowest in the hierarchy with subscript .
The subscript is a positive integer
The subscript must be enclosed within a pair of parenthesis.
The highest value that the subscript can take is the integer value specified in the OCCURS clause.
The elements of an array can be used for arithmetic and logical operations similar to any ordinary data-items.
SET statement
Is used to modify the index value.
Syntax-1
SET index-1 , . . . TO { index-2, identifier-2, integer-1 }.
Syntax-2
SET index-1, . . . { UP BY, DOWN BY } { integer, identifier }.
SUBSCRIPT Vs INDEX
Subscript Index
(1) Is a WORKING-STORAGESECTION variable defined by the user.
Is a special subscript created
and maintained by the operating System.
(2) Represents an occurrence of array element
Represents a displacement from
the address of the first element
(3) To modify the value of a
subscript, the MOVE or
Arithmetic verbs are used.
To modify the value of an index,
the SET verb is used.
SEARCH Statement
Syntax
SEARCH table-name [ VARYING index ]
[ AT END imperative statement-1 ]
{ WHEN condition-1 { statement-2, NEXT SENTENCE }} . . .
[ END-SEARCH ].
Searching in a table
Looking forward to locate the presence of ‘Z’ in the given
string/array
Searching in a table
JCL
Output Spool
SEARCH ALL
Syntax
SEARCH ALL Table-Name [AT END imperative-statement-1]
WHEN { identifier-1 { IS EQUAL TO, IS = } {identifier-2,
literal-1, arithmetic-expression-1 }
imperative-statement-2.
SEARCH ALL
Limitations of SEARCH ALL compared SEARCH
The condition following the word WHEN can test only for equality.
If the condition following the word WHEN is a compound condition then it can use only AND but not OR.
Multiple WHENs can not be used.
The VARYING option can not be used.
The item defined with OCCURS clause with its index must appear to the left of equal to sign.
SEARCH vs. SEARCH ALL SEARCH SEARCH ALL
(1) Table entries need not be in Table entries must be sequence. any sequence.
(2) Requires SET statement Does not need a SET prior to prior to SEARCH statement. SEARCH ALL statement.
(3) Can include any relational Can only have a single = condition with WHEN clause. condition with WHEN clause. (4) May include multiple WHEN May include only one WHEN
clause. clause.(5) Linear Search Binary Search
STRING HANDLING VERBS
Inspect
String
Unstring
INSPECT statement
The INSPECT statement can be used to
1. Count the number of occurrences of a given character in a field.
2. Replace specific occurrences of a given character with another character.
INSPECT statement
Syntax
INSPECT identifier-1 TALLYING { counter-1 FOR { {ALL, LEADING } , CHARACTERS , { char-1, literal-1 } } [ { BEFORE, AFTER } INITIAL { delimiter-4, literal-2 } ] }. . .
INSPECT – How does it work
a) The INSPECT scans the Source String from left to right counting and/or replacing characters under the control of the TALLYING, REPLACING or CONVERTING phrases.
b) The behavior of the INSPECT is modified by using the LEADING, FIRST, BEFORE and AFTER phrases.
c) An ALL, LEADING, CHARACTERS, FIRST or CONVERTING phrase may only be followed by one BEFORE and one AFTER phrase.
INSPECT – Modifying phrases
1) LEADINGThe LEADING phrase causes counting/replacement of all
Compare$il characters from the first valid one encountered to the first invalid one.
2) FIRSTThe FIRST phrase causes only the first valid character to be
replaced.
3) BEFOREThe BEFORE phrase designates as valid those characters to
the left of the delimiter associated with it.
4) AFTERThe AFTER phrase designates as valid those characters to
the right of the delimiter associated with it.
INSPECT – Example (finding occurrences)
INSPECT statement for finding the number of times a given character comes in a given name
Display messages to come in sysout
INSPECT – Example (finding occurrences)
JCL for executing program INSPEC
Name & Character passed to program by SYSIN in JCL
OUTPUT SPOOL
STRING statement
The STRING statement may be used to combine several fields to form one concise field. This process is called concatenation.
Example:
Suppose the structure of NAME field is
01 NAME. 05 F-NAME PIC A(10). 05 M-NAME PIC A(10). 05 L-NAME PIC A(10).
Let us say that the value of NAME field is THOMASALVAEDISON. We may wish to print the name with only a single blank between each component as THOMAS ALVA EDISON. We can use the STRING statement.
STRING statementSyntax
STRING
{ { identifier-1, literal } DELIMITED BY { identifier-2, literal-2, SIZE }} { { identifier-3, literal } DELIMITED BY { identifier-4, literal-2, SIZE }} INTO identifier-5
END-STRING.
ExampleSTRING
F-NAME DELIMITED BY ‘ ‘‘ ‘ DELIMITED BY SIZE M-NAME DELIMITED BY SIZE‘ ‘ DELIMITED BY SIZEL-NAME DELIMITED BY SIZE
INTO NAME.
STRING statement
Rules for using the STRING statement
1) The DELIMITED BY clause is required. It can indicate a) SIZE : The entire sending field is transmitted.
b) Literal : The transfer of data is terminated when the specified literal is encountered; the literal is not moved.
c) Identifier : The transfer of data is terminated when the contents of the identifier is encountered.
2) The receiving field must be an elementary data item with no editing symbols is JUSTIFIED RGHT.
3) All literals must be described as nonnumeric.
STRING statement
Rules for using the STRING statement
4) The identifier specified with the POINTER clause must be an elementary numeric item.
5) The STRING statement moves data left to right just like alphabetic fields are moved, but a STRING does not pad with low ordered, unlike an alphanumeric MOVE.
STRING statement
OVERFLOW Option
STRING . . . [ ON OVERFLOW imperative-statement ].
POINTER Option
STRING . . . [ WITH POINTER identifier ] [ ON OVERFLOW . . . ].
STRING - Example
Display in the spool
STRING statement
STRING - Example
JCL for executing program STRNG
Display in the Output spool
UNSTRING statement
The UNSTRING statement may be used to convert keyed data to a more compact form for storing it on disk.
For example, a program may include a statement that causes the following to be displaced on a screen.
ENTER NAME: LAST, FIRST, MIDDLE INITIAL
: USE COMMAS TO SEPARATE ENTRIES
Since each name has a variable number of characters, there is no way of knowing how large each individual last name and first name is. With the UNSTRING statement, we can instruct the computer to separate the NAME-IN into its components and store them without the commas.
UNSTRING statement
Syntax
UNSTRING identifier-1 [ DELIMITED BY [ ALL ] { identifier-2, literal-1 } [ OR [ ALL] { identifier-3, literal-2 }] . . .]
INTO identifier-4 . . . [ END-UNSTRING ].
UNSTRING statement
Rules for using the unstring statement
1. The sending field must be nonnumeric. The receiving fields numeric or nonnumeric.
2. Each literal must be nonnumeric.
3. The WITH POINTER and ON OVERFLOW clauses can be used in the same way as with the STRING statement.
UNSTRING - Example
UNSTRING - Example
JCL for executing program
UNSTRNG
Display in the Output spool
Some Common programming problems
• DATA EXCEPTIONPerforming arithmetic operation or comparison on afield containing BLANKS or non-numeric data
• DIVIDE EXCEPTIONAttempting to divide by ZERO
• ADDRESSING ERRORWhen we have Invalid value placed in a sub-script or index.This might lead to addressing beyond the table boundaries.
• OPERATION ERRORAttempting to access file before opening it.
Some common abend codes are –S0C4 : Protection exception (Unable to convert virtual to real address)S0C7 : Data Exception (Bad data in decimal field)S322 : Job/Program exceeded time limit. Program is loopingS222 : Job was cancelled by operator or JES