2
   identification division.
   program-id.      quick.
   environment division.
   data division.
   working-storage section.
   01 temp-val1 pic 9 value 1.
   01 temp-val2 pic 9 value 2.
   procedure division.
    mainline.
           perform encrypt.
           stop run.
    encrypt.
           if (temp-val1 = 1)
             display "1"
             display "2".
           end_if.
           if (temp-val2 = 2)
             display "2"
             display "1".
           end_if.
           display "outside the ifs".

The output of the program is simply:
1
2

But I would expect:

1
2
2
1
outside the ifs

What am I doing wrong? Have I misunderstood the sentence structure for the statements inside the first if, or the placement of the periods?

NickAbbey
  • 1,201
  • 2
  • 10
  • 17
  • You are mixing up COBOL coding styles. This is resulting in unexpected transfer of control. Check out [COBOL Transfer of Control](http://www3.sympatico.ca/bredam/TransferControl.html). That said, I would have expected the compiler to have thrown an error or at least a warning on your `end-if.` – NealB Apr 18 '13 at 17:06
  • OK, not a compiler bug once luke has spotted the problem. Serves me right for looking at the nicer examples, rather than the code in the question :-) – Bill Woodger Apr 19 '13 at 13:55

3 Answers3

4

There are 2 problems with the code. One is the period just before the end-if.

The other is that end_if is not the same as end-if. Only end-if is used in Cobol. The end_if is being taken as a paragraph name. Therefore, it does not (and should not) produce a compiler warning message.

McCee
  • 1,549
  • 10
  • 19
luke
  • 56
  • 1
  • Sharp eyes... That would make two paragraphs having the same name (END_IF). I don't think this is "illegal", just unconventional. I also believe the compiler should throw an error/warning if there been a reference to one of these occurances (eg. PERFORM END_IF) because the name could not be uniquely resolved. – NealB Apr 18 '13 at 20:51
  • This is the correct answer, as it explains (mostly) why there was no error reported by the compiler either. The end_if as a paragraph causes the PERFORM of encrypt to end having just done the one IF. The unreferenced duplicate paragraph names presumably are "valid", although since neither are in a SECTION they could never be referenced (they would both need to be, I think, in SECTIONs to be truly valid if referenced). Fixed-format COBOL gives some extra possibilities for diagnosing a problem, like " should begin in Area A" :-) – Bill Woodger Apr 19 '13 at 12:56
2

Trial and error proves out that the issue is a mix of syntax and period placement errors.

This did the trick to provide the expected output.

   identification division.
   program-id.      quick.
   environment division.
   data division.
   working-storage section.
   01 temp-val1 pic 9 value 1.
   01 temp-val2 pic 9 value 2.
   procedure division.
    mainline.
           perform encrypt.
           stop run.
    encrypt.
           IF (temp-val1 = 1)
             display "1"
             display "2"
           END-IF
           if (temp-val2 = 2)
             display "2"
             display "1"
           END-IF
           display "outside the ifs".

The example code in the openCobol Programmer's guide helped me resolve the issue. http://opencobol.add1tocobol.com/OpenCOBOL%20Programmers%20Guide.pdf

NickAbbey
  • 1,201
  • 2
  • 10
  • 17
1

To spell it out bluntly

    display "2".
   end_if.

the '.' after "2" ends the if statement, then you have another end_if and . which Open-Cobol is probably taking as end of procedure - probably not the best interpretation.

As Nick said **Do not mix coding styles.

personnaly I would put one '.' on a line by itself at the end of each procedure

encrypt.
           IF (temp-val1 = 1)
             display "1"
             display "2"
           END-IF
           if (temp-val2 = 2)
             display "2"
             display "1"
           END-IF
           display "outside the ifs"

           .
Bruce Martin
  • 10,358
  • 1
  • 27
  • 38
  • Looks good. You'd hope for a compile error for the, by that time, "stray" end-ifs. It is not only the second IF which is not done, but the DISPLAY which follows as well, so I think you're correct about it wandering away from the paragraph. – Bill Woodger Apr 18 '13 at 10:32
  • Yes, sorry to be unclear. That's what I meant by a "compile error". The fact that there is no "compile error" points to a "compil*er* error" if the version is up-to-date. If an "old" version of the compiler, could well have been fixed. I do not know of any Cobol which would successfully compile that. There should be the same syntax-error noted by the compiler twice. – Bill Woodger Apr 18 '13 at 12:45
  • I like this style of placing the single period at the end of a paragraph for clarity's sake. Thanks for the tip! – NickAbbey Apr 19 '13 at 16:57