[R] how does while work

Greg Snow snow at fisher.byu.edu
Thu Mar 21 16:22:26 CET 2002


On Wed, 20 Mar 2002, jimi adams wrote:

> i am having some problems with a program that i am writing and i think that
> knowing how the while command works will help me to figure out where i am
> going wrong
> *when do you get kicked out of a while loop?*
> (i.e., in the following example  what would the final value of 'a' be?) i
> thought it would be 99 but running it in R i get 101 (and 101 for x too!)
> 
> while (x <100) {
>   for (i in 1:101) {
>     i -> x
>     x -> a
>   }
> }

If you are trying to jump out of a for loop early then try something like:

for (i in 1:101) {
  i -> x
  if (! x<100 ) { break }
  x -> a
}

the "next" command will also skip the rest of the body of the loop and
start on the next iteration.

Hope this helps,

-- 
Greg Snow, PhD                Office: 223A TMCB
Department of Statistics      Phone:  (801) 378-7049
Brigham Young University      Dept.:  (801) 378-4505
Provo, UT  84602              email:  gls at byu.edu

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list