Javatpoint Logo
Javatpoint Logo

COBOL - Loop Statements

There 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 THRU
  • PERFORM UNTIL
  • PERFORM TIMES
  • PERFORM VARYING

PERFORM THRU

Perform 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:

  • Inline Perform
  • Outline Perform

Let's see the difference between them:

Inline Outline
INLINE executes a series of statements or a block of statements between the PERFORM and the END-PERFORM. OUTLINE Perform is used to execute a series of statements or blocks of statements coded in a separate section or paragraph that are not coded in along with the PERFORM statement.
This requires no separate PARAGRAPH or SECTION that needs to be coded, which are to be executed. It requires a separate PARAGRAPH or SECTION that needs to be coded for statements that are to be executed.
In this, scope terminator (END-PERFORM) is mandatory. In this, scope terminator (END-PERFORM) is not required.
Syntax: - Following is the syntax for INLINE PERFORM:
PERFORM
DISPLAY 'HELLO WORLD'
END-PERFORM.
Syntax:- Following is the syntax for OUTLINE PERFORM:
PERFORM PARAGRAPH1 THRU PARAGRAPH2

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:

COBOL Loop Statements

PERFORM UNTIL

A 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:

COBOL Loop Statements

PERFORM TIMES

PERFORM 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:

COBOL Loop Statements

PERFORM VARYING

A 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:

COBOL Loop Statements

GO TO Statement

In 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:

COBOL Loop Statements





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA