0

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.

mhshams
  • 16,384
  • 17
  • 55
  • 65
  • I've no idea about COBOL, but does it allow statements to be labelled? – pavium May 04 '11 at 05:14
  • The indentation can be important depending on the compiler. `IF` should line up with the `END-IF` and `MOVE WS TO RESULT` should also line up with the `END-IF` – Lawrence Woodman May 04 '11 at 06:48
  • 3
    Quote "I need to use GO TO" -- for the last thirty years COBOL programmers have been advised to avoid using GO TO at all costs. The only reason its still implemented is there is so much forty year old code still out there. Nobody needs to use GO TO, nobody should use GO TO and anybody coding up GO TOs in the 21st centuary should consider a career change. – James Anderson May 09 '11 at 10:41
  • @James Anderson--concur. How old is the COBOL dialect you're using @mohammad shamsi that it doesn't support IF ELSE? – Onorio Catenacci May 10 '11 at 13:29
  • 2
    The problem is not that the compiler lacks support for IF/ELSE, it clearly has that. The problem is the period at the end of "GO TO C10-END." -- the period is a nuclear weapon type scope terminator -- it terminats ALL active scopes. Remove the period and the updated code will work fine, as would an ELSE. – Joe Zitzelberger May 10 '11 at 17:23

5 Answers5

4

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
jon_darkstar
  • 16,398
  • 7
  • 29
  • 37
  • @jon_darstar: because of some reasons i CANNOT use ELSE and i have to handle it with GOTO. Tnx btw. – mhshams May 04 '11 at 06:21
  • 3
    love to hear them if you got time – jon_darkstar May 04 '11 at 11:07
  • 1
    +1 for using Else/End-If... and -0, but I wish it could be -1 for saying it looks miserable. Remove your periods and clean your logic and: "If FIRST < SECOND Move FIRST to RESULT else Move SECOND to RESULT End-If" looks amazingly modern, almost not any different than "if (FIRST < SECOND) RESULT = FIRST else RESULT = SECOND;" – Joe Zitzelberger May 05 '11 at 03:56
  • yes i recognized that, unless theres something important we dont see, the `WS` is extraneous. i wasnt trying to overhaul his code, its not mine to change. just wanted to point out `IF/ELSE`. i dont mean to shit on COBOL, just sharing my first impression. i maintain its not a language id be particularly excited to learn. btw - i havent got a clue what the periods mean. your code does look marginally better, but isnt there something much simpler we've all been overlooking? check my edit – jon_darkstar May 05 '11 at 04:30
  • Function Min works great if FIRST and SECOND are numeric items. It is an excellent solution. – Joe Zitzelberger May 05 '11 at 05:12
  • glad to hear it. the reference i looked up seemed to suggest that a few other data types were ok too, but i dont doubt it is somewhat more restrictive than `<` comparison. anyway - hows the syntax? – jon_darkstar May 05 '11 at 05:18
  • Syntax is perfect. Some compiler vendors might support non-numerics...I'm not really sure. – Joe Zitzelberger May 05 '11 at 06:01
2

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.

Joe Zitzelberger
  • 4,238
  • 2
  • 28
  • 42
  • 2
    how about `Move Function Min(FIRST, SECOND) to RESULT` ? – jon_darkstar May 05 '11 at 04:37
  • @jon_darkstar -- function min() is really what this code is doing, you are totally correct, but is that part of the standard or just part of Enterprise Cobol? I'm not certain. – Joe Zitzelberger Nov 19 '16 at 01:43
  • @JoshStodola - I frequently see horror show homework questions on here, and I'm not going to give the lazy professors a pass. If they can't upgrade their textbooks and teach modern Cobol, there are two things that will happen 1) their students will not learn Cobol as it is used today and 2) their students will not get jobs. The sad thing is, the student DOES NOT KNOW that they are being taught horribly dated approaches, someone must tell them. – Joe Zitzelberger Nov 19 '16 at 01:47
1

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.

Lawrence Woodman
  • 1,424
  • 9
  • 13
  • so you probably want to change that stuff between the `` to `C10-RESULT`? – jon_darkstar May 04 '11 at 05:38
  • np........... =D im glad you posted since i kinda sidestepped his question and i have zero interest in looking up COBOL syntax any more than i already did – jon_darkstar May 04 '11 at 05:46
  • @lawrence: if i understand it correctly, last statement (MOVE WS TO RESULT) is belong to C10-RESULT Paragraph. can we have a empty paragraph? (let's say we don't want to put last statement under a certain paragraph) – mhshams May 04 '11 at 06:26
  • @mohammad Yes, `MOVE WS TO RESULT.` belongs to paragraph `C10-RESULT`. Empty paragraphs are not recommended and indeed not always supported. What people normally do is have a final paragraph like: `C99-EXIT.` followed by `EXIT.`, assuming that you are running each section using `PERFORM`. – Lawrence Woodman May 04 '11 at 06:37
  • @lawrence: i don't really want to jump to end of section (exit) i just need to jump over the statement that i mentioned (move second to ws). so i think an empty paragraph is the best or a paragraph with an dummy statement like (DISPLAY '' or something like this) – mhshams May 04 '11 at 06:49
  • @mohammad if you don't want to jump to the end of the section, then it follows that you won't need an empty paragraph as there will be further statements to execute and hence the paragraph will not be empty. – Lawrence Woodman May 04 '11 at 07:12
  • @lawrence i need to jump but not to end of section, i need to bypass few statements only (int the above sample only one statement). actually i need something like LABEL in C or Assembly language. – mhshams May 04 '11 at 07:23
  • @mohammad for your purposes you can think of a paragraph as a label, that can be jumped to within a section. So to bypass a few statements, just start a paragraph after the statements that you want to bypass. In fact the only difference between a standard label and a paragraph is that you can only `GO TO` a paragraph in the current `SECTION`. – Lawrence Woodman May 04 '11 at 07:28
  • -1 for writing ancient Cobol. You have 5 extraneous periods and an extraneous label. And SECTIONS? Seriously? Like ANY code needs to do load module overlays in a day of ubiquitous virtual memory? – Joe Zitzelberger May 05 '11 at 03:51
  • @Joe I was writing it in the same style as the OP and assuming he was dealing with legacy code. – Lawrence Woodman May 05 '11 at 05:33
  • 1
    Could lose the period after 'GO TO C10-RESULT.' The if is closed by the explicit "END IF". – James Anderson May 09 '11 at 10:37
  • 2
    This bit of code really belongs on [The Daily WTF](http://thedailywtf.com/). It makes my heart sink to see anyone write stuff like this while taking themselves seriously. – NealB May 10 '11 at 20:44
  • Your first full-stop/period is incorrect. SECTIONs in the PROCEDURE DIVISION are optional. *referenced* paragraphs have to be unique within a SECTION or within a program without SECTIONs in the PROCEDURE DIVISION. Nope, you may have found some local convention, but there are many, many, such local conventions for procedure names. – Bill Woodger Sep 21 '14 at 00:16
0

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.

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
-1

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.

Sean Elgin
  • 19
  • 1
  • This is not a good example. Using if-statements without end-if scope-terminator makes it much harder to read. – Mnemonics Dec 22 '21 at 10:28