Statalist


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

RE: st: WHILE command


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: WHILE command
Date   Mon, 17 Mar 2008 14:09:21 -0000

Good point. Even the word "immediately" is, on closer inspection, 
possibly ambiguous. 

If it does mean, hmm, immediately, without executing another line
of code, Nick is right. 

If it means at the end of that loop, the other comments are 
right too. 

Nick
n.j.cox@durham.ac.uk 

Nick Winter

I believe the answer to the first question is "no":

 > When does it evaluate the WHILE condition? If it becomes false
 > does
 >  it immediately exit the WHILE loop?

That is, Stata evaluates the condition at the beginning.  If true it 
then executes the code in the braces.  Then it evaluates the condition 
again.  So it does not exit in the middle of the block of code.

For example:

local i 1
while `i'==1 {
   local i 0
   di "this gets displayed once despite the fact"
   di "that local -i- is now zero"
}


Nick Cox wrote:
> Please note the -cmdname- convention flagged in the FAQ. 
> 
> Eva is correct. 
> 
> In addition, know that -while- knows nothing about local macros. 
> 
> In this example 
> 
> local i = 10  
> while `i' { 
> 	local --i 
> 	di `i' 
> } 
> 
> what -while- sees is the command line after the names of any local
> macros have been 
> replaced by their contents. 
> 
> At first that is 
> 
> while 10 
> 
> then it is in turn 
> 
> while 9 
> 
> and so forth. The example also shows that the expression handled by
> -while- need not be 
> one with two arguments and a binary operator. Any expression with
> numeric result is legal. 
> 
> while 1 { 
> 	...
> } 
> 
> would be legal too (and is sometimes useful). 
> 
> Nick 
> n.j.cox@durham.ac.uk 
> 
> Eva Poen
> 
> Vincent,
> 
> I believe the answers are "yes" and "yes". See the example below.
> 
> local i 0
> local j 10
> 
> while `i' < `j' {
> 	di ""
> 	di "before updating: i `i' , j `j' "
> 	di ""
> 	local ++j
> 	local i = `i' + 3
> 	di "after updating: i `i' , j `j' "
> 	di ""
> 
> }
> 
> di "end of loop: i `i' , j `j' "
> 
> Eva
> 
> 2008/3/17, Vincent Davis <vincent@aspentechpro.com>:
>> I have a few questions about the WHILE command,
>>  1. When does it evaluate the WHILE condition? If it becomes false
> does
>>  it immediately exit the WHILE loop?
>>  2. Can both sides of the WHILE condition be local variables that may
>>  be updated in the WHILE statement ?  WHILE 'i' < 'j' {  ..... local
i
>>  = 'i' + 'j' .... local j = 'j' + 1.....  }

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index