Endless Loops

If the condition of a while loop never becomes false, the loop never ends. This is called an endless loop (or an infinite loop), and it's one of the most common mistakes with loops. Here, the line day = day + 1 is missing:

local day = 1
while day <= 3 do
    print("Day " .. day)
end

day stays 1, the condition day <= 3 is always true, and the program prints Day 1 over and over:

Day 1
Day 1
Day 1
Day 1
...

On your own computer, such a program would run until you stopped it. On this site, it is stopped after 5 seconds, and you see the message The program ran too long and was stopped. Does it have a loop that never ends? What the program printed until then stays in the output, so you can see where it got stuck. (A very long output is cut off after about 100,000 characters and ends with ….) If the loop prints nothing, you only see the message.

When you see it, check every loop: does something in its block change the variable in the condition, so that the condition becomes false at some point? Typical causes:

  • Nothing changes the variable, as in the example above.
  • The variable changes in the wrong direction. A countdown while seeds > 0 do with seeds = seeds + 1 instead of seeds = seeds - 1 makes seeds bigger and bigger, so the condition stays true.
  • The change is after end. Only the code between do and end repeats. A line below end would run after the loop, and that never comes. Indenting the block shows you what's inside.
  • A typo on the left of =. seds = seeds - 1 doesn't change seeds. Lua doesn't report an error: it quietly creates a new global variable seds, and seeds stays the same forever.
  • The condition misses its goal. A condition with ~= (not equal) is true for every number except one. If the variable jumps over that one number, the loop goes on forever. That's why counting loops usually use <, <=, > or >=: they stop even when the variable skips past the limit.

The program in the editor has a loop with ~=. Right now it works, because plot goes up by 1 and hits 10 exactly. Let's see what happens when it goes up by 2.

Instructions

Yesterday it rained on the plots with even numbers, so today Emma waters only the odd ones: 1, 3, 5, 7 and 9. Change the line plot = plot + 1 so that plot grows by 2, and run the program. It doesn't stop by itself, and after 5 seconds it is stopped. Look at the output: which numbers does plot go through, and why is the condition never false? Then fix the condition, so that the program prints:

Watering plot 1
Watering plot 3
Watering plot 5
Watering plot 7
Watering plot 9
Done

Market day is in 3 days. Below the program, create a local variable daysLeft with the value 3, and write a while loop that runs while daysLeft is greater than 0. Inside, print Days until the market: and the value of daysLeft, and then decrease daysLeft by 1. (If you add 1 instead, the loop never ends.) After the loop, print Market day!. The end of the output is:

Days until the market: 3
Days until the market: 2
Days until the market: 1
Market day!

Start programming for free

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

Or sign up with:

2/10