REJECT and REPEAT issue

I am not sure any one has encountered this issue before…

I am able to compile the below code successfully but however when I execute the below code the loop is running proper till the #I <=2.When the value of #I reaches 3 the program abends with the error message
“NAT1324 Loop must be active if this statement is used”. I could understand reject with out read statement is illogical and the ESCAPE TOP would be right statement instead of REJECT in the repeat loop.
REPEAT UNTIL #I EQ 5
ADD 1 TO #I
WRITE #I
REJECT IF #I GT 2
END-REPEAT

I modified the code by including read work once statemet to make it sense .But still I am getting the same error message when the reject condition is satisfied though the code looks logically correct.
REPEAT UNTIL #I EQ 5
ADD 1 TO #I
WRITE #I
READ WORK FILE 01 ONCE #INFILE
REJECT IF #INFILE EQ ‘CHECK’
END-REPEAT

I believe the natural compiler should be bit modified to sense the REJECT statement inside a REPEAT loop and it should issue compile time error.But currently the code is working fine whenever reject condition is not statisfied and Issuing the error message @ run time whenever the condition becomes true.

The explanation is found in the text for message 1324 (as you say, the code sample is illogical):

REJECT does not apply to a REPEAT statement/loop.

Since the REJECT statement can be located in a subroutine outside of a loop to be executed from inside a suitable data access loop, the compiler doesn’t check for active loops at compile time. The code might be called from a PERFORM (or perhaps even a CALLNAT).

Hi Doug;

And even a FETCH RETURN.

Since there are so many newbies on this forum, I thought I would add something to the descriptions by Ralph and Doug. PLEASE, please, do not make it your practice to ACCEPT/REJECT or ESCAPE from loops in objects other than the object that contains the loop.

Debugging, and maintaining, such code is far more difficult than the alternative; which is to set a “flag” in the external object, and, upon return to the object with the loop, test the flag to ascertain if an action is required.

This can be especially important if you get involved in converting a mainframe Natural system to a PC Natural system using Dialogs (where it is best to pass all necessary “information” from object to object).

steve

Keeping your ACCEPT/REJECT logic inside the module with the data access loop also means that any called routines are more reusable. For example; the called routine could answer a simple question question such as ‘is this a valid deliverable US mailing address’ and what is to be done with the answer should be left to the initiating module - whether to accept or reject, process or not process.