Home > Back-end >  Cannot break Shrek loop in Rstudio
Cannot break Shrek loop in Rstudio

Time:02-11

I have created a "while" loop that looks like this:

x <- 2
while (x < 8) {
    print("Shrek") 
}

Now, I have tried breaking the loop by inserting break in to the while loop, like this:

while (x < 8) {
    print("Shrek")
    break
}

but this doesn't break the loop, the console just keeps printing "Shrek". How can i make it stop? Have I put break in the wrong place?

Side question: Can there arise any problems from exiting Rstudio while it's in an on-going while() loop like this?

Thanks!

Edit: Pressing escape stopped the loop. Is there any line of code in the console that also stops it?

CodePudding user response:

Based on the code you have given I can't reproduce this. When I run everything, break works just fine. Have you potentially defined a variable called 'break' that would have overridden the default function? If not try resetting R and running this again.

  • Related