cobolgo-to-definition

how to use GO TO in COBOL


I have the following code snippet in one of my COBOL program.

IF  FIRST < SECOND
   MOVE FIRST TO WS
END-IF.
MOVE SECOND TO WS.
MOVE WS TO RESULT.

I need to use GO TO inside the IF block to jump to the last statement (MOVE WS TO RESULT).

IF  FIRST < SECOND
   MOVE FIRST TO WS
   GO TO <last line.(MOVE WS to RESULT)>
END-IF.
MOVE SECOND TO WS.
MOVE WS TO RESULT.

in other word, i need to skip "MOVE SECOND TO WS.".
what is the simplest way to jump to a specific line in cobol? I read somewhere that this is possible by defining a PARAGRAPH, but don't know how to define it.

It might seems very simple but I'm newbie to COBOL programming.

Thanks.

----------------* UPDATE *----------

based on @lawerence solution, is this correct?

IF  FIRST < SECOND
     MOVE FIRST TO WS
     GO TO C10-END.
  END-IF.

  MOVE SECOND TO WS.

C10-END.
MOVE WS TO RESULT.

i just moved back the last statement to be in first level.


Solution

  • jon_darkstar is right when it comes to improving the logic, however if you want to see how GO TO works here goes:

      IF  FIRST < SECOND
         MOVE FIRST TO WS
         GO TO C10-RESULT.
      END-IF.
    
      MOVE SECOND TO WS.
    
    C10-RESULT.
      MOVE WS TO RESULT.
    

    C10-RESULT. starts a paragraph and has to be a unique name in your code SECTION. By convention it should also start with the same prefix as the enclosing section. Therefore this example assumes that your code SECTION is something like C00-MAIN-PROCESS SECTION.