270 likes | 481 Views
CHAPTER 9 Iteration: Beyond the Basic PERFORM. OBJECTIVES. To familiarize you with: 1. The simple PERFORM. 2. How PERFORM statements are used for iteration . 3. The various options available with the PERFORM statement. The Simple PERFORM Reviewed. The Basic Formats.
E N D
CHAPTER 9Iteration: Beyond the Basic PERFORM Structured COBOL Programming, Stern & Stern, 9th Edition
OBJECTIVES • To familiarize you with: 1. The simple PERFORM. 2. How PERFORM statements are used for iteration. 3. The various options available with the PERFORM statement. Structured COBOL Programming, Stern & Stern, 9th Edition
The Simple PERFORM Reviewed Structured COBOL Programming, Stern & Stern, 9th Edition
The Basic Formats • There are two formats of the basic PERFORM: 1. In-Line PERFORM PERFORM Statements to be executed END-PERFORM Structured COBOL Programming, Stern & Stern, 9th Edition
The Basic Formats 2. PERFORM Paragraph-Name PERFORM [paragraph-name-1]control returns to this statement • The PERFORM paragraph-name statement will: 1. Execute all instructions in the named paragraph. 2. Transfer control to the next instruction in sequence, after the PERFORM paragraph-name. Structured COBOL Programming, Stern & Stern, 9th Edition
The Basic FormatsPseudocode • Pseudocode for a PERFORM paragraph-name PERFORM Paragraph . . PARAGRAPH . . {Statements to be performed go here . Structured COBOL Programming, Stern & Stern, 9th Edition
Modularizing Programs Using PERFORM Statements • Version 1: Nonmodular Approach IF AMT1-IN < AMT2-IN ADD AMT1-IN TO TOTAL-1 ADD AMT2-IN TO TOTAL-2 ADD 1 TO OK-REC-CTR ELSE ADD AMT2-IN TO TOTAL-3 ADD 1 TO ERR-REC-CTR END-IF. Structured COBOL Programming, Stern & Stern, 9th Edition
Modularizing Programs Using PERFORM Statements Version 2: Modular ApproachIF AMT1-IN < AMT2-IN PERFORM 300-OK-RTN ELSE PERFORM 400-ERR-RTN END-IF. 300-OK-RTN. ADD AMT1-IN TO TOTAL-1 ADD AMT2-IN TO TOTAL-2 ADD 1 TO OK-REC-CTR. 400-ERR-RTN. ADD AMT2-IN TO TOTAL-3 ADD 1 TO ERR-REC-CTR. Structured COBOL Programming, Stern & Stern, 9th Edition
Executing a Group of Paragraphs with a Simple PERFORM PERFORM paragraph-name-1 {THROUGH} {THRU} paragraph-name-2 • The PERFORM paragraph-name executes all statements beginning at paragraph-name-1 until the end of paragraph-name-2 is reached. • Control is then transferred to the statement directly following the PERFORM. Structured COBOL Programming, Stern & Stern, 9th Edition
The Use and Misuse of GO TO Statements Format: GO TO paragraph-name-1 • Unlike the PERFORM, which returns control to the statement following the PERFORM, the GO TO permanently transfers control to another paragraph. • However, in well-designed, structured programs it is best to avoid the use of GO TO as much as possible. • Grounds for failing this class Structured COBOL Programming, Stern & Stern, 9th Edition
The EXIT Statement • EXIT is a COBOL reserved word that performs no operation. • It is used to allow execution to pass over other statements or to transfer control back to the statement following the original PERFORM. • It is used, when necessary, as an end point in a paragraph being performed. Structured COBOL Programming, Stern & Stern, 9th Edition
EXIT Statement PERFORM 500-VALIDATION500-VALIDATION. IF AMT NOT NUMERIC EXIT END-IF ADD 1 TO COUNT ADD AMT TO TOTAL IF AMT > 50 ADD 1 TO OVER-50-CLUB END-IF.510-NEXT-PARA. Structured COBOL Programming, Stern & Stern, 9th Edition
ITERATION USING OTHER TYPES OF PERFORMs Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM UNTIL . . . A Review The format of a PERFORM UNTIL ... is: • Format 2 PERFORM [paragraph-name-1] [{THROUGH} {THRU} paragraph- name-2] UNTIL condition-1 Structured COBOL Programming, Stern & Stern, 9th Edition
Coding a Counted Loop with a PERFORM • The loop should: 1. Be preceded by an instruction that initializes the field to be tested (e.g., MOVE 0 TO COUNTER1). 2. Include a PERFORM UNTIL ... that is executed repeatedly UNTIL the field to be tested reaches the desired value (e.g., PERFORM UNTIL COUNTER1 = 5 or PERFORM UNTIL COUNTER1 >= 5). Structured COBOL Programming, Stern & Stern, 9th Edition
Coding a Loop with a PERFORM 3. Include, as one of the instructions within the PERFORM UNTIL, a statement that increases (or decreases) the value of the field being tested (e.g., ADD 1 TO COUNTER1). • The contents of the counter is used to control the number of times that the loop is performed. Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM TIMES • Format PERFORM paragraph-name-1 [{THROUGH} {THRU} paragraph- name-2] {integer-} {identifier-1} TIMES Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM TIMES • When using the TIMES format -- PERFORM paragraph-name-1 identifier-1 TIMES: (1) the identifier must be specified in the DATA DIVISION; (2) it must have a numeric PICTURE clause; and (3) it must contain only integers or zeros. Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM TIMES PERFORM 340-CALCULATE 10 TIMESPERFORM 10 TIMES ADD 2 TO EVEN-NUMBER ADD EVEN-NUMBER TO EVEN-TOTALEND-PERFORM Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP • Use PERFORM TIMES rather than PERFORM UNTIL if you know in advance the specific number of times a paragraph is to be executed. • If the number of times a paragraph is to be executed is variable use a PERFORM UNTIL. Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM VARYING • The last format for a PERFORM statement is the most comprehensive: PERFORM paragraph-name-1 [{THROUGH} {THRU} paragraph- name-2] VARYING identifier-1 FROM {identifier-2}{integer-1} BY{identifier-3} {integer-2} UNTIL condition-1 Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM TIMES PERFROM VARYING EVEN-NUMBER FROM 2 BY 2 UNTIL EVEN-NUMBER > 20 ADD EVEN-NUMBER TO EVEN-TOTALEND-PERFORMWhat happens? EVEN-NUMBER is set to 2 then tested to see if greater than 20 if not loop is executed next EVEN-NUMBER is incremented by 2 then tested etcWhen will loop stop? Structured COBOL Programming, Stern & Stern, 9th Edition
NESTED PERFORMS PERFORM VARYING ROW FROM 1 BY 1 UNTIL ROW > 20 PERFORM VARYING COL FROM 1 BY 1 UNTIL COL > 10 ADD CELL TO TOTAL** END-PERFORM DISPLAY TOTAL MOVE 0 TO TOTALEND-PERFORM **: The above is for example purposes – it is not syntactically correct Structured COBOL Programming, Stern & Stern, 9th Edition
USING NESTED PERFORM VARYING Statements Rules for using a Nested PERFORM VARYING 1. The innermost PERFORM VARYING loop is executed first. 2. The next outer PERFORM VARYING loop in sequence is then executed. Structured COBOL Programming, Stern & Stern, 9th Edition
THE PERFORM WITH TEST AFTER OPTION (COBOL 85) • Format PERFORM paragraph-name-1 [WITH TEST {BEFORE} {AFTER}] UNTIL condition-1 • With COBOL 85, if an in-line PERFORM is used, no paragraph-name is coded, • Statements within the PERFORM end with an END-PERFORM scope terminator. Structured COBOL Programming, Stern & Stern, 9th Edition
THE PERFORM WITH TEST AFTER OPTION (COBOL 85) • Default is WITH TEST BEFORE • Pre Test loop • Loop may be executed 0 times • Ex: MOVE 10 TO m-var PERFORM 200-INIT UNTIL m-var >= 10 • WITH TEST AFTER • Post Test loop • Loop will always be executed at least 1 time Structured COBOL Programming, Stern & Stern, 9th Edition
Examples • Page 343 • Page 345 • Page 347 • Page 349 • Page 351 • Page 352 • Page 355 • Page 357 Structured COBOL Programming, Stern & Stern, 9th Edition