Hi Salih,

here you go:

############################################
dummy <- FALSE
for ( ii in 1:5 ) {
  for ( jj in 3:6 ) {
    cat("ii=",ii,"; jj=",jj,"\n",sep="")
    if ( ii == jj ) {
      dummy <- TRUE
      break
    }
  }
  if ( dummy ) break
}
###########################################

Note that I am using "ii" and "jj" as loop indices, not "i" and "j". This makes it a lot easier to search for the loop counter in more complex scripts - if you just search for "i", most of your hits will be something else than the loop counter.

HTH,
Stephan


Am 01.06.2011 22:06, schrieb Salih Tuna:
Hi Stephan,
Thanks a lot. But i am not very good at R yet so i dont know how to set the
dummy variable to FALSE. Can you please help me with that as well?

On Wed, Jun 1, 2011 at 8:34 PM, Stephan Kolassa<stephan.kola...@gmx.de>wrote:

Hi,

you could set a dummy variable to FALSE outside the outermost loop. If the
break condition is met in the inner loop, set the dummy variable to TRUE
before breaking and test its truth status in the outer loop.

HTH
Stephan


Am 01.06.2011 21:25, schrieb Salih Tuna:

Hi,
I am looking for a command in R that would force the for loop to stop
after
it finds what it is looking for.
As an example
for(i in 1:5){
    for(j in 3:6){
       if(i==j)
         # do something...
        break;
}
}
And i don't want the loop to execute once i = 3 and stop.

Is there a way to do this?

best,
salih

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to