An Endless Loop

Run the program in the editor. The dog turns and then… just stands there. The program keeps running, though, and after a while the editor stops it with a message that it ran for too long.

Here's what happened: the Repeat until at the goal loop is empty. Before every pass, the dog checks whether it's at the goal. It isn't, so it carries out the blocks in the loop: none. And it checks again whether it's at the goal. It still isn't, because it hasn't moved. And so on, over and over, forever. The Move forward block that should have been in the loop is under it, and the program never gets there.

Such a loop is called an endless loop (or an infinite loop), and it's one of the most common bugs in programs. The computer gets "stuck" in it: it doesn't crash or report anything, it just keeps doing the same thing. That's why the editor stops it by itself after a while.

How to avoid endless loops: for every loop with a condition, ask yourself whether every pass brings it closer to the end. Here, every pass has to move the dog one step closer to the goal.

Instructions

Fix the program so that the dog reaches the goal on both maps.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

4/4