Skip to content

simple for loop question - how do you exit?

5 messages · dre968, Duncan Murdoch, Chuck Cleland +1 more

#
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
#
On 4/23/2009 2:29 PM, dre968 wrote:
You 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:
Type ?"if" at the R prompt, which should load the help page for
"Control Flow".
#
Nevermind i got it
dre968 wrote:

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