COBOL - Loop StatementsThere are some set of statements in a program that needs to be executed repeatedly, such as reading each record of a file up to its end. The statements in the program are running in a series until or unless if any statement executed in the flow alters the execution sequence. For iterative programming, PERFORM statements are used in the COBOL. These loop statements are:
PERFORM THRUPerform Thru performs a series of paragraphs by giving in the sequence the first and last names of the paragraph. After performing the last paragraph, the control will return. Based on how the statements coded under it, PERFORM is mainly classified into two types:
Let's see the difference between them:
Example:Let's see an example for PERFORM THRU statement: Output: When you compile and execute the above program, it will display the following output: PERFORM UNTILA block of statements or a paragraph/section will be executed in the PERFORM UNTIL statement until the specified condition becomes true. The default condition is ' WITH TEST BEFORE, 'and it specifies that the condition is tested before the execution of statements in a paragraph. Syntax:Following is the syntax for PERFORM UNTIL: Example:Let's see an example for PERFORM UNTIL statement: Output: When you compile and execute the above program, it will display the following output: PERFORM TIMESPERFORM TIMES is mainly used to execute the block of statements or paragraphs/sections repetitively with the number of times specified. Syntax:Following is the syntax for PERFORM TIMES: Example:Let's see an example of PERFORM TIMES: Output: When you compile and execute the above program, it will display the following output: PERFORM VARYINGA block of a statement or a paragraph/section will be performed in PERFORM VARYING until the condition becomes true in the UNTIL phrase. Syntax:Following is the syntax for PERFORM VARYING: Example:Let's see an example of PERFORM VARYING: Output: When you compile and execute the above program, it will display the following output: GO TO StatementIn a program, the GO TO statement changes the execution flow. In this statement, control transfers only in the forward direction. It is used for exiting a program. The various types of GO TO statements are as follows: Unconditional GO TO Following is the syntax for Unconditional GO TO: Conditional GO TO Following is the syntax for Conditional GO TO: Here, if x = 1, then the control will be transferred to the first paragraph, and if x = 2, then the control will transfer to the second paragraph, and so on. Example:Let's see an example for GO To statement: Output: When you compile and execute the above program, it will display the following output: Next TopicCOBOL String Handling |