Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: -exit- within -forvalues- loop


From   [email protected]
To   [email protected]
Subject   Re: st: -exit- within -forvalues- loop
Date   Tue, 22 Nov 2005 09:48:49 -0600

Stephen Jenkins <[email protected]> asks:

> I am checking a condition within a -forvalues- loop, with the
> idea that if the condition fails, the program will be stopped
> using -exit-.  I found some unexpected behaviour with one set of
> code ("version 1" below). "Version 2" produced the expected
> behaviour.  Note that version 1 code gets through ok to the
> "Hello World" display command (which I hadn't expected), whereas
> the version 2 code stops before that display command.
> 
> Advice please concerning why "version 1" works as it does,
> together with any related tips on the code.
> 
> I note from online help for -exit- that "More generally, exit
> causes Stata to terminate the current process and return control
> to the calling process." Is it that the return in the version 1
> case was to one level higher, i.e. outside the loop?  Or
> something like that?  (I don't have access to manuals at
> present.)

The manual says the same thing, so you obtain no extra clue from
that source.  Notice the word "process" that is used in the
description of -exit-.  That will play into my response below.

> <cut>
> 
> local M = 2
> 
> tempname primes
> mat `primes' = (3, 5)
> 
> local D = 15
> 
> ***** version 1 ***********
> 
> forval i = 1/`M' {
> 	local prime = `primes'[1,`i']
> 	if mod(`D',`prime') == 0 {
> 		di as err "{p 0 0 4}Number of draws should not "
> 		di as err "be exact multiple of chosen prime. "
> 		di as err "(Problem is for equation `i' "
> 		di as err "and prime `prime'.){p_end}"
> 		exit
> 	}
> }
> di "hello world #1"
>
> <cut>
> 
> . ***** version 1 ***********
> .
> . forval i = 1/`M' {
>   2.         local prime = `primes'[1,`i']
>   3.         if mod(`D',`prime') == 0 {
>   4.                 di as err "{p 0 0 4}Number of draws should not "
>   5.                 di as err "be exact multiple of chosen prime. "
>   6.                 di as err "(Problem is for equation `i' "
>   7.                 di as err "and prime `prime'.){p_end}"
>   8.                 exit
>   9.         }
>  10. }
> Number of draws should not be exact multiple of chosen prime.  (Problem is for
> equation 1 and
> prime 3.)
> Number of draws should not be exact multiple of chosen prime.  (Problem is for
> equation 2 and
> prime 5.)
> 
> . di "hello world #1"
> hello world #1

I can feel your pain -- I lost a few hours a while ago debugging
someone elses code that had the same kind of problem as you are
experiencing above -- an -exit- (without a return code number)
inside of a conditional which was inside a loop.

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

Most often in Stata you will see -exit ###- where we are exiting
with an error code number (such as -exit 198-).  In that case,
the fact that the error code is nonzero causes the -forval- loop
to also fail and exit out, and the do or ado file that it was
within also fails and exits out with the error code, and so on
until you get to the top level (or to a -capture-).

Okay, so what should you do?  Use the -continue, break- command
to exit out of the -forval- loop; see "[P] continue".

Ken Higbee    [email protected]
StataCorp     1-800-STATAPC

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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