Search code examples
if-statementcobolsentencegnucobol

Why is the second if statement in this Cobol code not being evaluated (OpenCOBOL)?


   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?


Solution

  • 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.