how to use GO TO in COBOL

mhshams picture mhshams · May 4, 2011 · Viewed 13.2k times · Source

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.

Answer

jon_darkstar picture jon_darkstar · May 4, 2011

GOTO can do what you're looking for, but IF/ELSE would be more direct. You want MOVE SECOND TO WS to run iff the IF block does not, correct?

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

I hope I got the syntax right, I have never used COBOL and just tried to work off your snippet and this example http://www.fluffycat.com/COBOL/If-and-End-If/. There probably will be situations in the future where you need GOTO, but it A) should be avoided when another control structure will work and B) I haven't the slightest idea how its done

to be honest, COBOL looks pretty miserable lol. ive never seen a language so verbose. good luck with everytihng


EDIT


Mostly for joe...

Cant this all be better done with a min function? I'm sure the syntax is wrong, but:

Move Function Min(FIRST, SECOND) to RESULT