Search code examples
ibm-midrangecobol

How GO To flow behaves/work, if PARA has only EXIT statement


For example what will be the output?

  PROCEDURE DIVISION
    MAINPARA
        DISPLAY "MAIN".
        GO TO PARA1.
        DISPLAY "DISPLAY".
        STOP RUN.

    PARA1.
        EXIT.

    PARA2.
        DISPLAY "I AM IN PARA2"
    ...

Will it execute PARA2 or not?

I am not a COBOL programmer, and I need to understand code from a migration tool/process, AMXW COBOL. The target system is an IBM AS/400.


Solution

  • It will continue into PARA2 just as in your other question.

    EXIT does nothing, in any circumstance.

    If it were to do anything, it would be only when there was a PERFORM involved. If a PERFORM were involved, the programmer may think that EXIT has somewhere to return to. With a GO TO, where would EXIT go?

    Exit does nothing. In any circumstance.

    See here: https://www.ibm.com/developerworks/community/forums/html/topic?id=4f9b202f-fba2-4802-8d61-8e31aef22d0e&ps=25

    I am assuming that you have an IBM Midrage COBOL, as you keep tagging your COBOL questions with RPG. https://www.ibm.com/developerworks/community/forums/html/topic?id=4f9b202f-fba2-4802-8d61-8e31aef22d0e&ps=25

    The above link describes the situation with CONTINUE, but CONTINUE and EXIT in current IBM compilers have identical function (no code is generated) as is also explained there.

    So what do EXIT and CONTINUE do? They are both "No Operation" statements, but they must have some use?

    They are for us. We use them if we otherwise have no statement that we can use, or as a reminder to ourselves of something that we expect to happen.

    For instance, if site-standards do not allow the use of negation in an IF you may see this:

    IF A EQUAL TO B
        CONTINUE
    ELSE
        Do some stuff...
    END-IF 
    

    There must be a statement after the IF but you don't actually want to do anything. Use CONTINUE and the IF will now compile and function exactly as you want.

    You will also see things like this:

        PERFORM 300-PARA THRU 300-PARA-EXIT
    
        ...
    
    300-PARA.
    
        ...
    
        IF some-condition
            GO TO 300-PARA-EXIT
        END-IF
    
        ...
    
    300-PARA-EXUT.
         EXIT.
    

    Because of the PERFORM range (the THRU) the PERFORM will end with the last statement of 300-PARA-EXIT. To avoid compiler diagnostics, a paragraph must contain something. Something that does nothing is EXIT.

    Many people erroneously think that CONTINUE and EXIT actually do something. I have money which says they don't.

    On IBM compilers there have been some changes in EXIT over the years. It used to be that EXIT could only appear in a paragraph on its own. It still generated no code, but could not be used interchangeably with CONTINUE. Whoopee! No longer true. That's me being ironic.