[R] Forcing a break/end in a loop

Adaikalavan Ramasamy ramasamy at stats.ox.ac.uk
Sat Jul 27 22:51:47 CEST 2002


This is probably some basic programming question. I am testing a complicated
and time consuming loops. I have also built in an internal check for the
values, which prints something like OK or Not OK to the screen so that I can
see if anything has gone wrong.

There errors are not logical errors so basically the loop runs to
completion. Is there a way of forcing the loop to stop/exit immediately when
it first fails my internal check ?

A simplified example follows:

x <- c(1:5, -6, 7:10)    # say due to mis-typing

for (i in 1:10){
 { if(is.na(log(x[i]))==FALSE) print("OK")
   else print("Not OK check your values") }
}

The output of the above is "OK", .... , "Not OK check your values", "OK",
...,"OK".
So I want the loop to stop when the 6th value fails.

This would save me a lot of time waiting for the loop to end just because I
made an typing error.
Many thanks.

Adai.


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