Cobol (Common Business Oriented Language)

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 38

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.

 Example
PERFORM 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

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 
  Identifier5  
 Identifier4    
 AFTER   FROM  IndexName 4  
 IndexName3   Literal 
   
 
 Identifier6 
 BY   UNTIL Condition2
 Literal  
StatementBlock END - PERFORM 

You might also like