Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Re: re: st code skips based on conditions


From   [email protected]
To   "[email protected]" <[email protected]>
Subject   Re: st: Re: re: st code skips based on conditions
Date   Fri, 29 Mar 2013 21:42:10 +0000

I don't mind being credited with the recommendation to use -continue-. But the two recommendations were from Maarten and Daniel in response to the original poster.

On the issues that Clyde raises, it does seem to me that -continue- is as about as self-explanatory as possible.

Nick

On 29 Mar 2013, at 18:55, Clyde B Schechter <[email protected]> wrote:

> Yesterday, Dan Klein wrote:
> 
> "I am running some foreach tabulate loops the results of which are
> accumulat ed by matrix commands. (see code below).
> I would like skip (at the point *** in the code) to the next level of
> local levels based on a conditional test of the number
> of observations contained in the scalar e(N) which is returned after
> each tabulated combination of the foreach variables.
> Have not been able to find anything (or even a lead) that could help me do this.
> 
> Any suggestions or ideas of how to go about this would be much appreciated.
> "
> 
> and both Nick Cox and Maarten Buis advised him to use the -continue- command.
> 
> That will certainly work.  Some of us who lived through the birth of "structured programming" might argue that -continue- represents a "dangerous" style of programming.  While I think the word "dangerous" greatly overstates the case, you can accomplish the same thing with:
> 
> foreach [whatever] {
>         [code to be done in all cases]
>         if ![skip condition] {
>               [code to be done only if skip condition is not met]
>        }
> }
> 
> Some advantages of this approach, not necessarily in order of importance, are:
> 
> 1.  You don't have to learn a new command that may be infrequently needed.
> 2.  When you go back to your code 6 months from now, you won't have to remember what that command, which you have not used again since then, and had never used before, does.
> 3.  You won't confuse the meaning of -continue- with the possibly different meaning assigned to that word in other programming languages you may use.
> 4.  The semantics of the foreach loop is retained in its purest form, with each iteration having the same entry and exit points.
> 5.  If you later need to change the algorithm so that something else is actually done to all cases, but must be done after the code that is sometimes skipped, then you can just insert the additional code at the end of the foreach block without any other changes to the code.  If you used -continue- there, then the code surgery gets a bit more complicated.
> 
> It's just a matter of style and, to be honest, I do use -continue- myself.  But I generally reserve it for situations where the code that would go in the block begun by "if ![skip condition]" is itself long and complicated, and especially if that block itself contains multiple levels of indentation.
> 
> 

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index