Showing posts with label COBOL perform. Show all posts
Showing posts with label COBOL perform. Show all posts

Tuesday, 12 August 2014

COBOL EXIT Statement: EXIT, EXIT PROGRAM, GOBACK, and STOP RUN

A called COBOL program should return to its caller. A main batch program should end the run unit. A paragraph used by PERFORM THRU may only need a named end point. Those are three different jobs, and using EXIT, EXIT PROGRAM, GOBACK, or STOP RUN in the wrong place can leave the next developer staring at confusing control flow.

COBOL EXIT statement diagram comparing EXIT, EXIT PROGRAM, GOBACK, and STOP RUN
Pick the right ending point.

What does EXIT mean in COBOL?

The plain EXIT. statement is mainly a common end point. IBM treats the simple form like CONTINUE, so control can pass through it when no active PERFORM range is returning from that point. It is not the same as ending a program.

The confusion comes from the word itself. In daily language, exit means leave. In COBOL, EXIT., EXIT PROGRAM, EXIT PARAGRAPH, and EXIT SECTION each have their own rules.

EXIT, EXIT PROGRAM, GOBACK, and STOP RUN

Statement Main use What happens
EXIT. Named end point Acts like a no-operation point in the procedure flow.
EXIT PROGRAM Called program return Returns control to the statement after the active CALL.
GOBACK Main or called program ending Returns from a subprogram like EXIT PROGRAM; in a main program it ends like STOP RUN.
STOP RUN End the run unit Terminates the run unit and closes files for programs in that run unit.

Plain EXIT example

Older COBOL often uses an exit paragraph at the end of a performed range. The paragraph gives PERFORM paragraph-a THRU paragraph-exit a clear final point.

PROCEDURE DIVISION.
PERFORM 2000-VALIDATE-ORDER
THRU 2000-VALIDATE-EXIT
GOBACK.
2000-VALIDATE-ORDER.
IF ORDER-NUMBER = SPACES
MOVE "Y" TO WS-ERROR-SW
END-IF.
2000-VALIDATE-EXIT.
EXIT.

The EXIT. paragraph does not return to a caller by itself. The active PERFORM controls the return. If some other path falls into that paragraph without an active performed range, control can pass to the next paragraph.

EXIT PROGRAM example

Use EXIT PROGRAM in a called program when it has finished and should return to the caller. This is common in reusable validation, formatting, and lookup modules.

IDENTIFICATION DIVISION.
PROGRAM-ID. VALDCUST.
PROCEDURE DIVISION USING LK-CUSTOMER-REC LK-RETURN-CODE.
IF LK-CUSTOMER-ID = SPACES
MOVE 8 TO LK-RETURN-CODE
EXIT PROGRAM
END-IF
MOVE 0 TO LK-RETURN-CODE
EXIT PROGRAM.

When a CALL is active, EXIT PROGRAM returns to the statement after the call. In a main program with no active call, it should not be used as the normal ending statement.

GOBACK example

GOBACK is often used as a clean final statement because it works naturally in both main programs and called programs. In a called program, it returns to the caller. In a main program, it returns control to the system or caller of the main program.

PROCEDURE DIVISION.
PERFORM 1000-OPEN-FILES
PERFORM 2000-PROCESS-FILE
PERFORM 9000-CLOSE-FILES
GOBACK.

Many shops prefer GOBACK at the end of batch programs because it avoids changing the statement when the same program structure is reused as a subprogram.

STOP RUN example

STOP RUN ends the COBOL run unit. That makes it a poor choice inside a called utility program, because it can end more than the current program.

PROCEDURE DIVISION.
IF WS-FATAL-ERROR
MOVE 12 TO RETURN-CODE
STOP RUN
END-IF.

Use STOP RUN only when the program really should terminate the run unit. For normal subprogram return, use EXIT PROGRAM or GOBACK.

EXIT PARAGRAPH and EXIT SECTION

Enterprise COBOL also supports procedure forms such as EXIT PARAGRAPH and EXIT SECTION. These statements leave the current paragraph or section without running the remaining statements in that paragraph or section. They are useful for clear early-exit logic when the coding standard permits them.

3000-CHECK-INPUT.
IF INPUT-STATUS NOT = "00"
MOVE 8 TO RETURN-CODE
EXIT PARAGRAPH
END-IF
PERFORM 3100-VALIDATE-FIELDS.

Do not hide business logic behind too many early exits. One or two clear exits can make code easier to read. A paragraph full of exit paths becomes hard to test.

Common mistakes

Using EXIT when EXIT PROGRAM is needed

EXIT. does not end a called program. If the program must return to the caller, use EXIT PROGRAM or GOBACK.

Using STOP RUN in a subprogram

A subprogram should usually return to its caller. STOP RUN can end the run unit, so it may close files and stop processing outside the subprogram.

Depending on PERFORM THRU everywhere

PERFORM THRU and exit paragraphs appear in many older systems, but they can make control flow harder to follow. For new code, a single performed paragraph or inline PERFORM is often easier to maintain.

Practical coding checklist

  • Use EXIT. only as a clear common end point.
  • Use EXIT PROGRAM when a called program must return to the active CALL.
  • Use GOBACK when the program ending should work in both main and called contexts.
  • Use STOP RUN only when the whole run unit should end.
  • Keep end points easy to scan in the Procedure Division.

Related Mainframe Forum guides

For related control-flow topics, read COBOL PERFORM statement, COBOL STOP RUN statement, COBOL CONTINUE and NEXT SENTENCE, COBOL CALL statement, and COBOL ending and reentering.

External references

IBM documents the Enterprise COBOL EXIT statement, EXIT PROGRAM format, GOBACK behavior, and STOP RUN behavior.

FAQ

Does EXIT end a COBOL program?

No. Plain EXIT. is mainly a common end point. Use EXIT PROGRAM, GOBACK, or STOP RUN when program ending behavior is required.

What is the difference between EXIT and EXIT PROGRAM?

EXIT. marks a procedure point and acts like no operation. EXIT PROGRAM returns from a called program to the statement after the active CALL.

Should I use GOBACK or STOP RUN?

Use GOBACK when the program should return cleanly from either a main or called context. Use STOP RUN only when the run unit should end.

Is EXIT PARAGRAPH the same as EXIT PROGRAM?

No. EXIT PARAGRAPH leaves the current paragraph. EXIT PROGRAM leaves the called program and returns to the caller.

Monday, 28 July 2014

COBOL Basic PERFORM Statement | BASIC PERFORM Statement | COBOL PERFORM Example. [PERFORM in COBOL]

PERFORM in COBOL Tutorial.


PERFORM in COBOL or BASIC PERFORM statement in COBOL is primarily used to implements loops in the COBOL programs. In this session, you'll learn the basics of the BASIC PERFORM statement in COBOL. 


You'll also learn how to use PERFORM Statement in COBOL programs with the help of an example. Let's get started with today's COBOL Basic PERFORM tutorial. 


Agenda.

  • Introduction.
  • PERFORM in COBOL.
  • COBOL PERFORM Statement Variants.
  • COBOL BASIC PERFORM Statement. 
  • COBOL BASIC PERFORM Syntax
  • COBOL BASIC PERFORM Examples. 
  • Conclusion

Introduction. 

COBOL is a procedural language and it's one of the oldest programming language which is still in use. COBOL is primarily used in large-scale enterprise applications such as banking, healthcare, the aviation industry, etc. 


In general, COBOL program logic is group into different paragraphs or sections. COBOL PERFORM statement executes the logic specified in these paragraphs. In nutshell, PERFORM in COBOL is used to control the program flow. 


COBOL PERFORM Statements are used for looping, iteration, or sequencing.


PERFORM in COBOL. 

As you know, that perform statement control the flow and execution of logic specified in paragraphs or sections. These are generally used for looping, iteration, or sequencing in COBOL programs. COBOL PERFORM verb is similar to DO...WHILE or FOR...loops of other programming languages such as VB, JAVA, or Python.

In simple terms, the COBOL PERFORM statement is used to transfer control implicitly or explicitly within the paragraph, section, or procedure.

PERFORM statements in COBOL are broadly divided into two categories. These categories are further subdivided into different variants. Let's look at each category one by one.
  • INLINE PERFORM Statement.
  • OUT-OF-LINE PERFORM Statement.

Now, let's talk about different variants of PERFORM Statements.

How many different types of performs are there in COBOL?


In COBOL, COBOL PERFORM statement has the following different types:
  • PERFORM ..... Paragraph name.
  • PERFORM ..... Times phrase.
  • PERFORM ..... Thru phrase.
  • PERFORM ..... Until phrase.
  • PERFORM ..... Varying phrase.


COBOL BASIC PERFORM Statement.

The basic PERFORM statement in COBOL is one of the simplistic variants of the COBOL PERFORM statements. The procedures (i.e. paragraph/sections) referenced in the basic PERFORM statement are executed once and then the control passes to the next executable statement following the PERFORM statement.

It is important to note that a COBOL PERFORM statement must not cause itself to be executed. Such a recursive PERFORM statement can cause unpredictable results.

Now, let's focus on the syntax of BASIC PERFORM Statements. 

COBOL BASIC PERFORM Syntax.

The following syntax is the syntax of the basic PERFORM Statement.  

COBOL PERFORM Statment Syntax. 


COBOL PERFORM Example. 

In the following example, B000-OPEN-FILES and C000-CALC-TAX para will be executed one after the other if there is no program failure. Also, the control will move from one statement to the next executable statement mentioned in the paragraph. 

  ENVIRONMENT DIVISION
       DATA DIVISION
       WORKING-STORAGE SECTION.       
      *
       PROCEDURE DIVISION
       A000-MAIN-LOGIC.
           PERFORM B000-OPEN-FILES.
           PERFORM C000-CALC-TAX. 
           STOP RUN.
      *
       B000-OPEN-FILES.
           ...
           ...
           ...

       C000-CALC-TAX.
           ...
           ...
           ...


COBOL PERFORM THRU Example.

In the following example, B100-INIT-PARA and B100-EXIT para will be executed one after the other if there is no program failure. Also, the control will move from one statement to the next executable statement mentioned in the paragraph. 


COBOL PERFORM Statement, BASIC PERFORM Statement
COBOL PERFORM THRU Example.


Youtube: PERFORM in COBOL Tutorial.

PERFORM in COBOL is linked with a youtube video tutorial. Please do visit our channel and consider subscribing to our channel. 


Conclusion. 

In this session, you learn the basics of the COBOL BASIC PERFORM Statement. PERFORM statement in COBOL is used to implement loops, sequences, or iteration in COBOL programs. You also learned the different variants of PERFORM Statements such as PERFORM with times, PERFORM with Until phrase, Perform with Varying Phrase, etc. 

►Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 
Mainframe Forum™

Created with Artisteer

New In-feed ads