+ All Categories
Home > Documents > Branching Statements(COBOL)

Branching Statements(COBOL)

Date post: 26-Oct-2014
Category:
Upload: praveen-kumar
View: 61 times
Download: 2 times
Share this document with a friend
Popular Tags:
15
BRANCHING STATEMENT CONTENT 1. PERFORM STATEMENT 1.1 SIMPLE PERFORM STATEMENT 1.2 PERFROM…TIMES STATEMENT 1.3 IN-LINE PERFORM STATEMENT 1.4 PERFORM….UNTIL STATEMENT 1.5 PERFORM….VARYING.... UNTIL STATEMENT 1.6 PERFORM… VARYING…UNTIL…AFTER….UNTIL STATEMENT 2. GO TO STATEMENT 2.1 SIMPLE GO TO STATEMENT 2.2 GO TO ….. DEPENDING ON STATEMENT 3. COMPARISON AMONG PERFORM AND GO TO STATEMENT 4. THROUGH / THRU KEYWORD. 5. WITH TEST BEFORE / AFTER 1. PERFORM STATEMENT 1.1 SIMPLE PERFORM STATEMENT Syntax: PERFORM procedure-name-1. Working: While executing simple PERFORM statement, control transferred to the first statement of the specified procedure/ paragraph. After the execution of the paragraph, control return back to the statement following the PERFORM statement in the main paragraph.
Transcript
Page 1: Branching Statements(COBOL)

BRANCHING STATEMENT

CONTENT1. PERFORM STATEMENT

1.1 SIMPLE PERFORM STATEMENT1.2 PERFROM…TIMES STATEMENT1.3 IN-LINE PERFORM STATEMENT1.4 PERFORM….UNTIL STATEMENT1.5 PERFORM….VARYING.... UNTIL STATEMENT1.6 PERFORM… VARYING…UNTIL…AFTER….UNTIL STATEMENT

2. GO TO STATEMENT2.1 SIMPLE GO TO STATEMENT2.2 GO TO ….. DEPENDING ON STATEMENT

3. COMPARISON AMONG PERFORM AND GO TO STATEMENT 4. THROUGH / THRU KEYWORD.

5. WITH TEST BEFORE / AFTER

1. PERFORM STATEMENT

1.1 SIMPLE PERFORM STATEMENT

Syntax: PERFORM procedure-name-1.Working: While executing simple PERFORM statement, control transferred to the first statement of the specified procedure/ paragraph. After the execution of the paragraph, control return back to the statement following the PERFORM statement in the main paragraph.

Page 2: Branching Statements(COBOL)

1.2 PERFORM ….TIMES STATEMENT

Syntax: PERFORM procedure-name-1 {integer-1/ identifier-1} TIMES.

Working: While executing the PERFORM statement, control transferred to the first statement of the specified procedure/ paragraph. That procedure/ paragraph will execute integer-1 (using current value of identifier-1) times. After the execution, control return back to the statement following the PERFORM statement in the main paragraph. Identifier-1 should have a positive integral value. If identifier-1 value is negative the paragraph will not execute and control goes to the next

statement in sequence. If the value of integer is zero then paragraph will not execute. If integer value is one, then

paragraph will execute once.

1.3 IN-LINE PERFORM STATEMENT

Syntax: PERFORM UNTIL condition.

For eg:PERFORM UNTIL FIELD>10 ADD 1 TO …… MOVE…… COMPUTE…… …………………END PERFORM.

No paragraph name follows PERFORM.

Loop through this code until FIELD >10

Terminates the scope of the loop

When a paragraph name is omitted, the statements to be executed are coded in the same paragraph as the PERFORM statement. Such a structure is called an in-line PERFORM statement. An in-line PERFORM is terminated with END_PERFORM. No period s may be used between PEROFORM and END-PERFORM within an

in-line PERFORM statement. Lengthy in-line PERFORM statement can be very difficult to understand and maintain. We can use in-line PERFORM anywhere you use PERFORM

Page 3: Branching Statements(COBOL)

1.4 PERFORM …UNTIL STATEMENT

Syntax: PERFORM procedure-name-1 UNTIL condition.

Working: While executing simple PERFORM statement, control transferred to the first statement of the specified procedure/ paragraph. That procedure/ paragraph will execute until condition becomes true. 1) First control will check the condition; if the condition is false, control transferred to

specified paragraph/ procedure.2) After the execution of the paragraph control will return back to the main paragraph again

it will check the condition.3) If the condition is false again control transferred to the specified paragraph.4) This process will continue until condition becomes true.5) If the condition is true, control will not pass to specified paragraph and it will not execute

and the statement following the PERFORM statement in the main paragraph will execute. It is useful to change the subscript value in the one dimensional table. If we want to execute some paragraphs we can use this perform …until statement.

Page 4: Branching Statements(COBOL)

1.5 PERFORM….VARYING.....UNTIL STATEMENT

Syntax: PERFORM procedure-name-1 VARYING identifier-1 FROM initial-value BY increment-factor/ decrement-factor UNTIL condition. Here we are combining the initialization and increment / decrement factor of the identifier

in one line. For incrementing, increment factor value should be positive and for decrementing,

decrement factor value should be negative.Working: steps are1. Compiler set the value of identifier-1 to initial-value.2. Compiler check the condition3. If the condition is false, compiler executes the specified paragraph.4. After the execution of the specified paragraph, control transfer to increment

part; here the identifier-1 adds increment-factor to its previous value.5. After increment control transfer to condition part, it will check the condition; if

it is false, then control transfer to specified paragraph. 6. Control transfer to the first statement of the paragraph; after the execution,

again the identifier-1 will increment by increment-factor. (it will continue until condition becomes true)

7. If the condition becomes true; statement following the perform statement will execute.

Page 5: Branching Statements(COBOL)

1.6 PERFORM… VARYING…UNTIL…AFTER….UNTIL STATEMENT

Syntax: PERFORM procedure-name-1 VARYING identifier-1 FROM initial-value-1 BY increment-factor-1/ decrement-factor-1 UNTIL condition-1 AFTER identifier-2 FROM initial-value-2 BY increment-factor-2/ decrement-factor-2 UNTIL condition-2. Here we are combining the initialization and increment / decrement factor of the identifier-1

and identifier-2 in one line. For incrementing, increment factor value should be positive and for decrementing,

decrement factor value should be negative. Here both the conditions are false, the specified paragraph will execute. It is a nested looping statement; so it is useful in multidimensional arrays.

Page 6: Branching Statements(COBOL)

Working: steps are1. Compiler set the value of identifier-1 to initial-value-1.2. Compiler checks the condition-1.3. If the condition-1 is false, control transfer to AFTER part, set identifier-2 to intial-value-2.4. After the initialization, control transfer to condition-2.5. If the condition-2 is false, control transfer to specified paragraph. 6. After that control transfer to increment part of identifier-2; ie. Identifier-2 adds increment-

factor-2 to its previous content.7. Control checks the condition-2;8. If it is false, control transfer to specified paragraph.9. If the condition-2 is false, control transfer to increment part of identifier-1. i.e identifier-1

adds increment-factor-1 to its previous content10. Control checks the condition-111. If the condition-1 is false, control transfer to AFTER part, set identifier-2 to intial-value-2.12. After the initialization, control transfer to condition-2.13. If the condition-2 is false, control transfer to specified paragraph. 14. After that control transfer to increment part of identifier-2; ie. Identifier-2 adds increment-

factor-2 to its previous content.15. Control checks the condition-2; it will continue until condition-1 becomes true.

2. GO TO STATEMENT2.1 SIMPLE GO TO STATEMENTSyntax:

GO TO procedure-name-1 [, procedure-name-2]….. Here control will transfer to the specified procedure unconditionally. In simple GO TO statement, control will transfer to the specified procedure. After the

execution of the procedure, control will transfer to the remaining paragraphs. Control will not return to the main paragraph.

Page 7: Branching Statements(COBOL)

2.2 GO TO ….. DEPENDING ON STATEMENT Syntax:GO TO procedure-name-1 [, procedure-name-2]….., procedure-name-n DEPENDING ON identifierWorking: depending on the value of identifier, control transfer to the procedure. For eg: if the identifier = 1, procedure-name-1 will execute,

if the identifier = 2, procedure-name-2 will execute,if the identifier = 3, procedure-name-3 will execute etc…..

Page 8: Branching Statements(COBOL)

3. COMPARISON AMONG PERFORM AND GO TO STATEMENT

GO TO STATEMENT PERFORM STATEMENTGO TO is always unconditional PERFORM can be conditional or unconditionalGO TO is a jump statement PERFORM is a looping statementIn GO TO statement, control will transfer to the specified paragraph and control will pass to the remaining paragraphs also

In PERFORM statement, control will transfer to the specified paragraph and control will not pass to the remaining paragraphs

Control will not return back to main paragraph

Control will return to the main paragraph.

In GO TO, procedure-name is mandatory In PERFORM, procedure-name is optionalIn GO TO, always the specified paragraph will execute.

In conditional PERFORM, the condition is false, the paragraph will execute. If it is true, it will not.

4. THROUGH / THRU KEYWORD If we want to execute a number of procedures continuously then we can use THROUGH /

THRU keyword.For eg: PERFORM P2 THRU P4.Control will transfer to P2, after that P3 and last P4 will execute. After the execution of P2, P3 and P4 control will return back to the main paragraph.

5. WITH TEST BEFORE / AFTER WITH TEST BEFORE: in this case first compiler will check the condition before the

execution of the paragraph. WITH TEST AFTER: in this case first compiler will execute the paragraph after that control

will transfer to condition part.

The modified Syntaxes are PERFORM procedure-name-1 [{THROUGH / THRU} procedure-name-2].

PERFORM procedure-name-1 [{THROUGH / THRU} procedure-name-2] {integer-1/ identifier-1} TIMES.

PERFORM procedure-name-1 [{THROUGH / THRU} procedure-name-2] [WITH TEST {AFTER / BEFORE}] UNTIL condition-1.

PERFORM procedure-name-1 [{THROUGH / THRU} procedure-name-2] [WITH TEST {AFTER / BEFORE}] VARYING identifier-1 FROM initial-value-1 BY increment-factor-1/ decrement-factor-1 UNTIL condition-1 AFTER identifier-2 FROM initial-value-2 BY increment-factor-2/ decrement-factor-2 UNTIL condition-2.


Recommended