Accidentally created an infinite loop?

Infinite Loops and What to do About Them

First off, what is an infinite loop?

It's a bit of code that will continue to run forever. Usually it's something like a for loop or a while loop that has no ending condition. This kind of mistake is surprisingly easy to make and can make an app unusable.

Important note: The code examples in this article are dangerous. We urge you to look at them, but not to type them into Bitsbox. You have been warned!

Here's a simple for loop that will put an infinite number of walruses on your screen:

for (i = 10; i > 9; i++) {
stamp('walrus').move()
}

Can you see what went wrong there? The variable i is initialized with a value greater than 9, and the ending condition says to keep running as long as the values are greater than 9. This is kind of an unusual way to use a for loop, but it demonstrates the point.

So what's so bad about an infinite loop?

Putting an infinite loop in your Bitsbox app will make your browser lock up. You won't be able to hit the home button on your virtual tablet, or interact with the app, and it may even make the whole browser application unusable. This will happen every time you open that app.

Ack! Then what do I do? How do I fix my app?

Unfortunately, there's no way for you to do it directly. The app always runs immediately when you open it, so it will lock up before you can edit it. The best course of action is to email Bitsbox Customer Support at help@bitsbox.com and tell us which app it is. We'll have to go into the database and edit the code directly in order to fix the app.

Okay, but what about the loop function? Isn't that infinite?

Yes, it is. Good catch. The difference there is that usually the loop function runs twenty times a second, and allows other bits of code to run in the meantime. It may not seem like there's a lot of free time per second, but Bitsbox can run hundreds of thousands of operations per second, which is still just a fraction of the millions of operations the computer as a whole can do. A true infinite loop tries to take up all those spots and won't let any other bits of code run. Which is why they can be so disruptive.

Non-infinite loops that can still crash your app

An error we often see is in an app called Run Dodo Run. Kids are encouraged to modify a speed variable in order to make a windmill scroll across the screen faster.

fill('jungle')
mill = stamp('windmill',700,780,120)
dodo = stamp('dodo2',200,800,150)
speed = 10


function loop() {
mill.move(LEFT,speed)
 mill.wrap()
 if (dodo.hits(mill)) {
dodo.change('dodo3')
  }
}

function tap() {
dodo.move(UP,400)
 dodo.move(200,800,2000)
 speed = speed + 1

}

Sometimes kids will set the speed variable to a ridiculously large number, like 1000000000000000000000000000. (That's 10 to the 27th, or about a trillion trillion trillion.) While this is technically not an infinite loop, it will still lock up the app. Why? Well, when the windmill hits the left side of the screen, we call wrap() which puts it back on the right side of the screen. It then quickly hits the left again, and wraps again, and on and on. We're effectively trying to scroll the windmill across the screen several trillion times. That's too much for Bitsbox to handle and the app freezes.

This is a particularly insidious kind of bug, since there doesn't seem to be anything wrong at first glance. In general, it's advisable to avoid big numbers in loops. Incrementing or moving or rotating or what-have-you anything by more than a few thousand in a loop has the potential for problems.

Ugh. I feel like my brain is looping-the-loop after all that

It may seem like a lot, but there are just a couple of things to remember. When writing a loop, take an extra second to think through what the conditions are to stop the loop. Try to avoid super large numbers in loops. And if you get into trouble reach out to Customer Support. We're always happy to help.

Feedback and Knowledge Base