Skip to content

problem with while loop with next

2 messages · Liaw, Andy, Luke Tierney

#
Prof. Tierney,

Thanks very much for the info.  

Why does the loop work if I move the assignment out of the condition?  E.g.,
the following works:

i <- 0
while(i < 5) {
  i <- i + 1
  if(i < 3) next
  print(i)
}

Regards,
Andy
http://www.stat.umn.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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Tue, Oct 02, 2001 at 04:20:36PM -0400, Liaw, Andy wrote:
The bug in the code is that `next' causes a jump directly to
evaluating the body for the next iteration without first evaluating
the test expression.  That happens to be OK with this way of writing
the loop since there are no side effects in the condition and the
condition is always true if the jump occurs.

luke