1.19k likes | 2.13k Views
COBOL Programming. DAY 2. Agenda for Day2. Data Movement verbs. Sequence Control verbs. Types of conditions. REDEFINES, RENAMES, USAGE clauses. Design and development of sample programs. MOVE VERB.
E N D
COBOL Programming DAY 2
Agenda for Day2 • Data Movement verbs. • Sequence Control verbs. • Types of conditions. • REDEFINES, RENAMES, USAGE clauses. • Design and development of sample programs.
MOVE VERB • TheMOVEcopies data from the source identifier or literal to one or more destination identifiers. • The source and destination identifiers can be group or elementary data items. • When the destination item is alphanumeric or alphabetic (PIC X or A) data is copied into the destination area fromleft to rightwith space filling or truncation on the right. • 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 zeroorspace filled.
DATA movement verbs… (1) MOVE (2) MOVE . . . CORRESPONDING ( CORR ) (3) MOVE . . . OF . . . TO . . . OF
Before WS00-OUT1 ‘BEST’ WS00-OUT2 1234 Before WS00-OUT1 ‘ ’ WS00-OUT2 0 Before WS00-OUT3 0786 After WS00-OUT3 2345 Before WS00-OUT4 ‘PAYAL PAREKH’ After WS00-OUT4 ‘SHUTI DEY’
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.
Before WS00-OUT1 0000 WS00-OUT2 000000 Before WS00-OUT1 3456 WS00-OUT2 345678 Before WS00-OUT3 000000 After WS00-OUT3 123456 Before WS00-OUT4 00000000 After WS00-OUT4 12345678
MOVE .. example **************************** WS00-OUT1 : HARAYANA WS00-OUT2 : HARAYANA **************************** Output SPOOL
JUSTIFIED RIGHT clause Is used to change the default type movement of alphabetic and alphanumeric data. Example 01 NAME PIC X(10) JUSTIFIED RIGHT. MOVE “KAJOL” TO NAME. Contents of NAME field is bbbbbKAJOL
JUSTIFIED RIGHT clause .. example ********************************************* WS00-OUT1 : ABCDEFGHIJKLMNOPQRSTUVWXYZ WS00-OUT2 : ABCDEFGHIJKLMNOPQRSTUVWXYZ ********************************************* Output SPOOL
MOVE CORRESPONDING • Facilitates movement of value of sub-item of a group item to a similar named sub-item of another group item SyntaxMOVE { CORRESPONDING, CORR } identifier-1 TO identifier-2 where identifier-1 and identifier-2 are group items.
MOVE CORRESPONDING .. example **************************** WS00-GR2 : NISHANT 00000 **************************** Output SPOOL
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 Certain combinations of sending and receiving data types are not permitted. Receiving field Sending field
SEQUENCE CONTROL verbs • GOTO • IF . . . THEN . . . • PERFORM • EVALUATE • STOP RUN
GO TO Verb • Syntax-1 GO TO Paragraph Name. • ExampleGO TO 400-READ-PARA.
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
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.
IF statement -Implied Operands • ExampleIF TIME < 2 AND TIME > 1 THEN MOVE “SLOW” TO SPEED END-IF. Is equivalent toIF TIME < 2 AND > 1 THEN MOVE “SLOW” TO SPEED. • Note: The following statement is invalid. IF TOT-1 OR TOT-2 = 7 THEN DISPLAY “ The Sum is 7.”.
Classification of Conditions • Relational condition • Sign condition • Class condition • Compound condition • Condition-name
Sign condition • Syntax • ExampleIF DISCRIMINANT IS NEGATIVE THEN DISPLAY “The roots are imaginary”.
Class condition • Syntax • 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 ! “.
Condition Names • Are essentially boolean variables. • Are always associated with data names called condition variables. • Is defined in the DATA DIVISION with levelnumber 88. • Syntax88 condition-name {VALUE IS, VALUES ARE } literal-1 [ { THRU, THROUGH } literal-2 ].
Condition-Names .. example Condition Variable 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. Condition Names PROCEDURE DIVISION Statements. IF SINGLE SUBTRACT 125 FROM DEDUCTIONS. IF ONCE-MARRIED ADD 300 TO SPECIAL-PAY. IF MARRIED PERFORM B000-MARRIAGE-GIFT.
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 identifythe values which make the Condition Name TRUE.
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 001000 081 001100 /* 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
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.
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 Scope of P0000-PROCESS-RECORD is delimited by the occurrence the paragraph name D0000-PRODUCE-OUTPUT.
PERFORM Verb - variations • Simple PERFORM • In-line PERFORM • Nested PERFORM • PERFORM . . . THRU • PERFORM . . . UNTIL • PERFORM . . . TIMES • PERFORM . . . VARYING
PERFORM Verb - Simple PERFORM • SyntaxPERFORM Paragraph-Name. • ExamplePERFORM 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 • SyntaxPERFORM imperative-statements. • ExamplePERFORM MOVE NUM-1 TO MAX IF NUM-2 > MAX THEN MOVE NUM-2 TO MAX DISPLAY “Maximum is ” MAX. END-PERFORM Lets see an example ..
When SYSIN data satisfies the condition WS-STRING = ‘KARINA’ the scope of the INLINE PERFORM gets terminated
PERFORM Verb – Nested PERFORM • SyntaxParagraph-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 … • SyntaxPERFORM Paragraph-Name-1 [ { THRU, THROUGH } Paragraph-Name-2 ]. • ExamplePERFORM 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 .. • SyntaxPERFORM 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 . . 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 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
PERFORM Verb – PERFORM .. TIMES • SyntaxPERFORM Paragraph-Name-1 [ { THRU, THROUGH } Paragraph-Name-2 ] { integer, identifier } TIMES. • Example PERFORM 500-PROCESS-PARA THRU 800-END-PARA 8 TIMES.