You are on page 1of 84

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 GRADE


ELSE MOVE ‘B’ TO GRADE
END-IF.

(2) IF NOT OK-BALANCE THEN MOVE 2 TO BALANCE-


CODE
ELSE NEXT-SENTENCE
END-IF
Relation
Conditions

[ NOT] GREATER THAN 


 
[ NOT] > 
 

[ NOT] LESS THAN 
[ NOT] < 
 Identifier     Identifier 
  [ NOT] EQUAL TO   
 Literal  IS    Literal 
 ArithmeticExpression  [ NOT] =   ArithmeticExpression 
     
 GREATER THAN OR EQUAL TO 
 >= 
 
 LESS THAN OR EQUAL TO 
 <= 
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.
Sign condition

 Syntax
POSITIVE 
 
Arithmetic Expression IS [ NOT] NEGATIVE
ZERO 
 

Example

IF DISCRIMINANT IS NEGATIVE THEN


DISPLAY “The roots are imaginary”.
Class condition

 Syntax  NUMERIC 
 ALPHABETIC 
 
Identifier IS [NOT]  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.

 Literal 
 VALUE   
88 ConditionName     THROUGH  
 VALUES   LowValue  THRU  HighValue 
   

 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.
Condition Names

 Are essentially boolean variables.

 Are always associated with data names called


condition variables.

 Is defined in the DATA DIVISION with level


number 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.
Condition 88 WIDOWED VALUE IS 2.
Names 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.:’. Martial-status = 0
ACCEPT MARITAL-STATUS.
IF SINGLE
SUBTRACT 125 FROM DEDUCTIONS.
IF ONCE-MARRIED Martial-status = 2
ADD 300 TO SPECIAL-PAY.
IF MARRIED
PERFORM B000-MARRIAGE-GIFT. Martial-status = 1
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

WS00-MARKS 000 WS00-MARKS 081


WS00-DISP WS00-DISP PASSED COMPRE
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
Output SPOOL WE ARE INSIDE B001-FIRST-PARA
WE ARE INSIDE B002-MIDDLE-PARA
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
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
Output SPOOL WE ARE INSIDE B001-FIRST-PARA
WE ARE INSIDE B002-MIDDLE-PARA
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
Output SPOOL WE ARE INSIDE B001-DISP-PARA
WE ARE INSIDE B002-DISP-PARA
****************************
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.
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

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
WE ARE INSIDE B000-PERF-PARA
****************************
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

Output SPOOL HELLO GUEST. WELCOME TO E&R TRAINING


HELLO GUEST. WELCOME TO E&R TRAINING
HELLO GUEST. WELCOME TO E&R TRAINING
****************************
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
Output SPOOL HELLO GUEST. WISH YOU ALL THE BEST
HELLO GUEST. WISH YOU ALL THE BEST
****************************
PERFORM ...VARYING Syntax

 THRU    BEFORE 
PERFORM 1stProc   EndProc WITH TEST  

 THROUGH  
 AFTER 
Identifier 2 
Identifer1   
VARYING   FROM IndexName2
IndexName1  Literal 
 
Identifier3
BY   UNTIL Condition1
Literal 
 Identifier 5  
 Identifier4    
AFTER   FROM IndexName 4  
IndexName3  Literal 
   
 
 Identifier6 
 BY   UNTIL Condition2 
 Literal  
[StatementBlock END - PERFORM ]
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

Identifier 
 
Literal
 
CondExpression 
EVALUATE  
ArithExpression 
TRUE 
 
FALSE 
    
    
    
    
 ANY   
 Condition   
    
WHEN TRUE   StatementBlock 
 FALSE   
    
  Identifier  Identifier   
    THRU     
 [ NOT] Literal    Literal   
ArithExpression   THROUGH  ArithExpression 

 
     
 

[ 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

*************************************

Output SPOOL YOU HAVE CLEARED EXAM WITH A GRADE


*************************************
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

Output
Spool of the
Job

Array Declaration

For Output spool


display
Defining an Array – Using Indexed
clause

INDEXED ARRAY
Defining an Array – Using Indexed
clause

Output
Spool of the
Job
PROJECT EMP# EMPNAME
Declaring Tables –BANKS
An example
11111 SRINI
BANKS 22222 JYOTI
MKTNG 33333 VINAY
MKTNG 44444 SARMA
Declaring Tables – An example
PROJECT EMP# EMPNAM
Declaring Tables – BANKS
An example
11111 E
SRINI
BANKS 22222 JYOTI
MKTNG 33333 VINAY
MKTNG 44444 SARMA

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 '**************************************************'
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- Is a special subscript
STORAGESECTION variable created
defined by the user.
and maintained by the
(2) Represents an Represents
operating a displacement
System.
occurrence of array from
element
the address of the first
(3) To modify the value of To modify the value of an
element
a index,
subscript, the MOVE or the SET verb is used.
Arithmetic verbs are 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.

(12)May include multiple WHEN May include only one WHEN


clause. clause.
(13)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

• LEADING
The LEADING phrase causes
counting/replacement of all Compare$il
characters from the first valid one encountered to
the first invalid one.

• FIRST
The FIRST phrase causes only the first valid
character to be replaced.

• BEFORE
The BEFORE phrase designates as valid those
characters to the left of the delimiter associated
with it.

AFTER
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


STRING statement
Syntax

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.

Example
STRING
F-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
M-NAME DELIMITED BY SIZE
‘ ‘ DELIMITED BY SIZE
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.
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

STRING
statement

Display in the
spool
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,


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 EXCEPTION
Performing arithmetic operation or comparison on a
field containing BLANKS or non-numeric data

• DIVIDE EXCEPTION
Attempting to divide by ZERO

• ADDRESSING ERROR
When we have Invalid value placed in a sub-script or index.
This might lead to addressing beyond the table boundaries.

• OPERATION ERROR
Attempting 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 looping

You might also like