ESCAPE TOP in Subprogram is a goto to a loop in main program?

Hello all!

Here’s a sample which is narrowed down from a code of our production system.

define data local
1 #i1 (I1)
end-define
for #i1 = 1 to 5
perform sub-esc
display #i1
end-for
define subroutine sub-esc
callnat 'ESCTOP-N' #i1
end-subroutine
end

While ESCTOP-N is:

define data parameter 
1 #i1 (I1)            
end-define            
if #i1 = 3            
escape top            
end-if                
end

Output:
#I1

1
2
4
5

I’m shocked. I would expect a NAT1324…
So ESCAPE TOP has similar behaviour as REINPUT.

regards

Matthias

From the manual:

Function
The ESCAPE statement is used to interrupt the linear flow of execution of a processing loop or a routine.

With the keywords TOP, BOTTOM and ROUTINE you indicate where processing is to continue when the ESCAPE statement is encountered.

An ESCAPE TOP/BOTTOM statement, when encountered for processing, will internally refer to the innermost active processing loop. The ESCAPE statement need not be physically placed within the processing loop.

If an ESCAPE TOP/BOTTOM statement is placed in a routine (subroutine, subprogram, function, or a program invoked with FETCH RETURN), the routine(s) entered during execution of the processing loop will be terminated automatically.

So, yes, the ESCAPE TOP works just as shown by your output.

Yes, it is similar to how a REINPUT works. For example, if you have an INPUT statement within a FOR loop and after the INPUT you invoke a callnat or perform, a REINPUT in the subroutine or subprogram will return to the INPUT statement. HOWEVER, if the FOR loop has been completed and you then do a callnat/perform within which you try a REINPUT you should get an error message.

1 Like