how to use GO TO in COBOL

14.1k Views Asked by At

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.

5

There are 5 best solutions below

13
On BEST ANSWER

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.

7
On

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
3
On

OMFSM! It is not 1974, why are you writing Cobol like that? This:

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

Has a number of problems:

  • It uses periods to delimit scope, that is nearly three decades deprecated.
  • It isn't using ELSE
  • It is trying to use GO TO.

May I suggest the following as the way to approach it since 1985:

If FIRST < SECOND
  Move FIRST to WS
Else
  Move SECOND to WS
End-IF
Move WS to RESULT

But really, the code should simply read:

If FIRST < SECOND
  Move FIRST to RESULT
Else
  Move SECOND to RESULT
End-If

Unless the intermediate result is needed in WS. Cobol 66 and 74 used GOTO and periods because they lacked modern control structures. I realize you are a 'newbie', but you should suggest to whoever is teaching you that they really need to upgrade their skills.

0
On

A new non-Answer has brought this silly question to light.

ELSE in the IF is the 100% obvious answer. It is deeply odd that GO TO has to be used whereas ELSE may not be used.

Another way, surprised it didn't come up:

MOVE SECOND TO WS
IF  FIRST LESS THAN SECOND
   MOVE FIRST TO WS
END-IF
MOVE WS TO RESULT

No ELSE, no GO TO. Extra MOVE executed when FIRST is less than second, though.

To include a GO TO is simple, but stupid. Add a GO TO. A GO TO has to go somewhere (unless using ALTER ... TO PROCEED TO ..., which everyone hopes you weren't), so make a label at the point you want it to arrive, and add the name of that label to the GO TO.

A label is a user-defined word. If referenced (as in this case) it must be unique within a SECTION, if you use SECTIONs, which you don't need to, otherwise unique within the program and whether referenced or not it may not be the same name as something else (like a data-definition or the internal name of a file).

A label is a procedure-name. A procedure-name should terminate with a period/full-stop and the procedure itself should also terminate with a period/full-stop.

What about the MOVE FUNCTION MIN ( ... ) ... as a solution?

Well, it works. If other staff at your site are not used to it, you will not be thanked for using it (without prior discussion, anyway).

What does it do? Well, in Enterprise COBOL, the compiler generates an extra little area, copies the first argument to that area, tests against the second argument, copies the copy of the first argument, or the second argument, whichever is relevant, to the result.

Vs the ELSE, that is an extra storage area defined, an extra instruction for addressability of that, and an extra Assembler move (MVC) plus the lack of ready recognition.

Better for programmers new to COBOL, used to a multitude of functions in other languages? Not really, as they will be soundly beaten if they don't write programs that can be understood (at 2am) by the rest of the staff.

IF FUNCTION MIN(VAR1 VAR2 VAR3 VAR4 VAR5) = 17

It's another downside of FUNCTION. You see, you can do that. Then, at 2am, when the program has crashed 32 lines later, after VAR1 and VAR3 have been changed, are you going to be able to find the result of that IF in the core dump? Maybe, maybe not. Depends if any other functions, and of what type, have been used. At 2am, you don't want that. Not at all.

On the upside, it is less typing. For those who type, rather than use the editor.

1
On

In our shop, we'd use the example provided by Bill Woodger. However, we do use periods as scope-terminators. COBOL should be structured and use the KISS principle, just like any other language. This:

MOVE SECOND TO WS.
IF FIRST LESS THAN SECOND
    MOVE FIRST TO WS.
MOVE WS TO RESULT.

Note that this only works if we are assured that SECOND and FIRST have numeric values, or that WS is a PIC X() string, not a numeric PIC 9(). This is by far the easiest to read. No ELSE or GO TO required.