3 GO TO Statements in COBOL absolutely to Avoid now

GO TO statements kill your application performance. It is not a part of structural design. Avoid these mistakes in your program.

GO TO will not give control back. It just drops control at destiny. When you compare GOTO with Perform, then Perform is much better option.

GO TO is first introduced in FORTRAN language in 1957.

The primary criticism is that code that uses goto statements is harder to understand than alternative constructions.

You can tell in interview, if we avoid GO TO statements, you can improve performance.

1. GO TO Example

Sample COBOL Program

2020-TEST-SECTION  SECTION.
Move A To B. IF B > 50
GO TO 2020-EXIT END-IF. EXIT-2020. EXIT.

You can Avoid GO TO

2020-TEST-SECTION  SECTION.
Move A To B. IF B > 50
CONTINUE END-IF. EXIT-2020. EXIT.

In the above example CONTINUE helps you to come out of your section.

2. GO TO Example

COBOL Sample Program

2021-TEST-SECTION SECTION.
Move A To B. IF B > 50
GO TO 2021-EXIT
END-IF. 
Move B To C. IF C > 50
GO TO 2021-EXIT END-IF. 
EXIT-2021. 
EXIT.

How to Avoid GO TO Statement

2021-TEST-SECTION SECTION.
Move A To B. IF B > 50
SET B IS TRUE
END-IF.
Move B To C.
IF C > 50
SET C IS TRUE
END-IF.
EXIT-2021.
EXIT.
*** Your Next Logic would be
IF B
PERFORM 2022-B-SECTION.
END-IF.
IF C
PERFORM
2023-C-SECTION.
END-IF.

Section and Paragraph both are different. The word SECTION you need to write in first line of section. No need to write anything for paragraph. A section can contain paragraphs.

3. GO TO Example

2023-TEST-SECTION SECTION.
Move A To B.
IF B > 50
GO TO 2024-TEST-SECTION
END-IF.
EXIT-2023.
EXIT.

The above way of using GO TO is crude method. Instead you can use code as below. The EXIT is the last statement in a section or Paragraph.

PERFORM 2023-TEST-SECTION
PERFORM 2024-TEST-SECTION
*** Sections start here.
2023-TEST-SECTION SECTION.
Move A To B. IF B > 50
CONTINUE 
END-IF. 
EXIT-2023.
EXIT.
Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.