Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Use of -exit- within -foreach- or -forvalues-

From   "Ben Jann" <>
To   <>
Subject   st: Use of -exit- within -foreach- or -forvalues-
Date   Mon, 30 Oct 2006 22:17:45 +0100

Hi, I always thought that using -exit- within a -program- 
would terminate the program no matter what. However, I 
now used -exit- within a -foreach- loop and -exit- did 
not seem to have any effect. Here is an example:

. prog test1
  1.         local num 1 2 3
  2.         foreach n of local num {
  3.                 if "`n'"=="2" {
  4.                         di "`n' => exit"
  5.                         exit
  6.                 }
  7.                 di "`n'"
  8.                 mac shift
  9.         }
 10.         di "not to be reached"
 11. end

. test1
2 => exit
not to be reached

Neither the -foreach- loop, nor the program is 
terminated. This is not what I expected and I cannot 
find anything about it in the documentation. 
Furthermore, note that -exit- works as expected within 
a -while- loop (this is documented in [P] while):

. prog test2
  1.         local num 1 2 3
  2.         tokenize "`num'"
  3.         while "`1'"!="" {
  4.                 if "`1'"=="2" {
  5.                         di "`1' => exit"
  6.                         exit
  7.                 }
  8.                 di "`1'"
  9.                 mac shift
 10.         }
 11.         di "not to be reached"
 12. end

. test2
2 => exit

I checked the Statalist Archive and found a similar 
thread, see

Stephen Jenkins ( reported a 
problem with -exit- within a -forvalues- loop and 
Ken Higbee ( gave an answer. However, 
Ken's answer does appear incorrect (or at least 
incomplete) to me. Stephen's code looked something 

	forval i = ... {
		if mod(...) == 0 {

and part of Ken's answer was:

"-exit- without a return code (or in other words, 
with a zero return code) pops you out of the current 
process.  The -if mod(...) == 0 { ... }- block is 
considered a process, so the -exit- is only popping 
you up out of that -if- block of code, and since the 
return code is zero, there is no error and the -forval-
loop continues on its merry way."

However, -exit- does not terminate the loop even if 
not included in an -if- block: 

. prog test
  1.         local list 1 2 3
  2.         foreach l of local list {
  3.                 di "`l'"
  4.                 exit
  5.         }
  6.         di "howdy"
  7. end

. test

Furthermore note that the -display- command after the 
loop is not executed. So -exit- does not terminate 
the loop but it terminates the program? Huh?

Although it is not much of a problem to me that -exit- 
does not work within -foreach- (I just use -while- 
instead of -foreach-), I'd like make the following 

- It is very irritating that -exit- has a different 
  effect in a -while- loop than in a -foreach- or 
  -forvalues- loop. One would not expect that and it 
  is likely to cause confusion. Documentation should 
  say something about it (especially since use of -exit- 
  is documented for -while-).

- It is still unclear to me what is going on. Why does 
  -exit- not terminate the program if used in a 
  -foreach- loop? Can anyone clarify? And what exactly 
  is a "process" in [P] exit?
- Ken's advice was to use -continue, break- ("Okay, so 
  what should you do?  Use the -continue, break- command 
  to exit out of the -forval- loop; see '[P] continue'"). 
  However, the reason for using -exit- is to terminate 
  the whole -program- (also in Stephen's case).


*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index