Download presentation
Presentation is loading. Please wait.
Published byOsborn Mosley Modified over 9 years ago
1
Chapter 4 Using PERFORM, GO TO, and IF to Control Programs
2
Using GO TO to control a program You can force the program to jump to the beginning of any paragraph with a GO TO. – E.g. GO TO paragraph-name. A GO TO is like a PERFORM in that the program jumps to a new paragraph. However, when that paragraph is completed, the PERFORM returns to the line at which the PERFORM was requested, but the GO TO does not. When a GO TO reaches the end of the paragraph to which it has jumped, it moves into the next paragraph.
3
Using GO TO to control a program PROCEDURE DIVISION. PROGRAM-BEGIN. PERFORM SHALL-WE-CONTINUE. IF YES-OR-NO = "N“ GO TO PROGRAM-DONE. PERFORM MAIN-LOGIC. PROGRAM-DONE. GOBACK. MAIN-LOGIC. DISPLAY "This is the main logic.".
4
Using GO TO to control a program Sample program 5.2 It is certainly legitimate to use a GO TO at the bottom of a paragraph to jump back to the top of the paragraph in order to execute the paragraph again under some condition, although some would dispute even that use. After you've worked with modifying real code, you will find out why GO TO should be discouraged.
5
Using GO TO to control a program It is very confusing to be following a paragraph of logic, and find a GO TO to another paragraph somewhere else in the program. Because a GO TO does not bounce back, you have no way of knowing whether the rest of the current paragraph is ever executed or the programmer just skipped everything else for some reason. One danger of GO TO verbs is the likelihood that the programmer skipped some code for no reason at all (other than carelessness), instead of having some reason to skip the code.
6
Using PERFORM repetitively When the PERFORM verb is used to perform something a number of times, the COBOL compiler takes care of setting things so that a PERFORM is requested over and over until the number of times is exhausted. – E.g. PERFORM A-PARAGRAPH 10 TIMES. The PERFORM...TIMES verb is flexible, and the number of times to perform something can be a variable itself. – E.g. PERFORM A-PARAGRAPH HOW-MANY TIMES.
7
A Processing Loop A computer is designed to do things over and over, but if it does the same thing endlessly, the computer is limited to a single job. In practice, a processing loop is brought to an end by some condition. The condition is set up to be tested at the beginning of each pass through the processing loop or at the last step in the loop. The condition is used to determine whether the processing loop should end or continue.
8
A Processing Loop A processing loop is one or more paragraphs that are executed over and over. Processing loops (which are almost always controlled by some condition and should be called controlled processing loops) are sometimes simply called loops. The condition that controls the processing loop usually is called the processing loop control, or simply the loop control.
9
A Processing Loop
11
Using PEFORM to control a processing loop The PERFORM UNTIL sentence is a repetitive request to perform a paragraph, with a built-in IF test in the UNTIL. The PERFORM verb is requested over and over until the condition tests true. – E.g. PERFORM CALCULATE-AND-DISPLAY UNTIL THE-MULTIPLIER > 12.
12
Using PEFORM to control a processing loop
13
A PERFORM UNTIL sentence tests the condition before the perform is executed. The requested paragraph is performed over and over until the condition tests true.
14
Using PEFORM, VARYING, UNTIL Syntax: PERFORM a paragraph VARYING a variable FROM a value BY a value UNTIL a condition. e.g. PERFORM CALCULATE-AND-DISPLAY VARYING THE-MULTIPLIER FROM 1 BY 1 UNTIL THE-MULTIPLIER > 12.
15
Using PEFORM, VARYING, UNTIL
16
1. How many times will DISPLAY-HELLO be performed in the following example? PERFORM DISPLAY-HELLO 10 TIMES. DISPLAY-HELLO. DISPLAY "hello".
17
Using PEFORM, VARYING, UNTIL 2. If THE-COUNT is defined as a numeric variable, how many times will DISPLAY-HELLO be performed in the following example? PERFORM DISPLAY-HELLO VARYING THE-COUNT FROM 1 BY 1 UNTIL THE-COUNT > 5. DISPLAY-HELLO. DISPLAY "hello".
18
Using PEFORM, VARYING, UNTIL 3. In each of the previous examples, identify the the processing loop and the control for the processing loop?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.