GO TO statements kill your application performance. It is not a part of the structural design. Avoid these mistakes in your program.
GO TO Statement
GO TO statement will not give control back. It just drops control at destiny. When you compare GOTO with Perform, then Perform is a 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.
Sample COBOL Program.
2020-TEST-SECTION SECTION. Move A To B. IF B > 50 GO TO 2020-EXIT END-IF. EXIT-2020. EXIT.
How to re-write the Code.
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.
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 the first line of the section. No need to write anything for paragraphs. A section can contain paragraphs.
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 a 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.