I have a loop and an if statement in the loop. once the if statement is true for 1 value in the loop i'd like to exit the loop. is there a command to do this? i know its going to be something like exit and i feel stupid asking this question -- View this message in context: http://www.nabble.com/simple-for-loop-question---how-do-you-exit--tp23197504p23197504.html Sent from the R help mailing list archive at Nabble.com.
On 4/23/2009 2:29 PM, dre968 wrote:> I have a loop and an if statement in the loop. once the if statement is true > for 1 value in the loop i'd like to exit the loop. is there a command to do > this? i know its going to be something like exit and i feel stupid asking > this questionYou want "break". To see the help on this, you need the quotes around it: ?"break", but you don't use them in code, of course. Duncan Murdoch
On 4/23/2009 2:29 PM, dre968 wrote:> I have a loop and an if statement in the loop. once the if statement is true > for 1 value in the loop i'd like to exit the loop. is there a command to do > this? i know its going to be something like exit and i feel stupid asking > this questionType ?"if" at the R prompt, which should load the help page for "Control Flow". -- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Nevermind i got it dre968 wrote:> > I have a loop and an if statement in the loop. once the if statement is > true for 1 value in the loop i'd like to exit the loop. is there a > command to do this? i know its going to be something like exit and i feel > stupid asking this question >-- View this message in context: http://www.nabble.com/simple-for-loop-question---how-do-you-exit--tp23197504p23197518.html Sent from the R help mailing list archive at Nabble.com.
To "break" out of a loop early, use the "break" command (or rethink your logic and use a different type of loop, often if you will be commonly leaving the loop, a while loop may make more sense than a for loop). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of dre968 > Sent: Thursday, April 23, 2009 12:29 PM > To: r-help at r-project.org > Subject: [R] simple for loop question - how do you exit? > > > I have a loop and an if statement in the loop. once the if statement > is true > for 1 value in the loop i'd like to exit the loop. is there a command > to do > this? i know its going to be something like exit and i feel stupid > asking > this question > -- > View this message in context: http://www.nabble.com/simple-for-loop- > question---how-do-you-exit--tp23197504p23197504.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.