Chapter 4
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

Chapter 4 PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 4. Using PERFORM, GO TO, and IF to Control Programs. 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.

Download Presentation

Chapter 4

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 4

Chapter 4

Using PERFORM, GO TO, and IF to Control Programs


Using go to to control a program

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.


Using go to to control a program1

Using GO TO to control a program

PROCEDURE DIVISION.

PROGRAM-BEGIN.

PERFORM SHALL-WE-CONTINUE.

IF YES-OR-NO = "N“

GO TO PROGRAM-DONE.

PERFORMMAIN-LOGIC.

PROGRAM-DONE.

GOBACK.

MAIN-LOGIC.

DISPLAY "This is the main logic.".


Using go to to control a program2

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.


Using go to to control a program3

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.


Using perform repetitively

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.


A processing loop

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 conditionis 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.


A processing loop1

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.


A processing loop2

A Processing Loop


A processing loop3

A Processing Loop


Using peform to control a processing l oop

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.


Using peform to control a processing l oop1

Using PEFORM to control a processing loop


Using peform to control a processing l oop2

Using PEFORM to control a processing loop

  • 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.


Using peform varying until

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.


Using peform varying until1

Using PEFORM, VARYING, UNTIL


Using peform varying until2

Using PEFORM, VARYING, UNTIL

1. How many times will DISPLAY-HELLO be performed in the following example?

PERFORM DISPLAY-HELLO 10 TIMES.

DISPLAY-HELLO.

DISPLAY "hello".


Using peform varying until3

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".


Using peform varying until4

Using PEFORM, VARYING, UNTIL

3. In each of the previous examples, identify the the processing loop and the control for the processing loop?


  • Login