Question

   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?

Était-ce utile?

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

Autres conseils

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

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"

           .
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top