The while Loop
Every evening, Emma collects the eggs from her hens. A program that prints her plan for a week could have seven almost identical print lines. For a whole season, it would need a hundred. Programs repeat work like this all the time: they go through every day of a season, every plot of a field, or redraw a game screen 60 times a second. That's what loops are for. A loop is a piece of code that runs again and again.
The simplest loop in Lua is while. It repeats a block of code while its condition is true:
local day = 1
while day <= 5 do
print("Day " .. day)
day = day + 1
endThis prints:
Day 1
Day 2
Day 3
Day 4
Day 5A while loop looks like an if: the keyword, a condition, and a block of code that ends with end. Only instead of then, it has do. The difference is that if runs its block at most once, but while keeps coming back to the condition. Step by step:
- The program creates
daywith the value1. - It checks the condition
day <= 5. It's true, so the program runs the block: it printsDay 1, andday = day + 1increasesdayto2. - It goes back to the condition.
2 <= 5is still true, so it printsDay 2and increasesdayto3. - And so on, until
dayis6. Then the condition is false, the loop ends, and the program continues after the loop'send.
Each run of the block is called an iteration. This loop has five iterations. For a season of 100 days, you would only change 5 to 100.
Indent the block by 4 spaces, as with if, so that you see at a glance what repeats. The code after end runs only once, when the loop has finished:
local plot = 1
while plot <= 3 do
print("Watering plot " .. plot)
plot = plot + 1
end
print("All plots are watered.")Watering plot 1
Watering plot 2
Watering plot 3
All plots are watered.Notice the line plot = plot + 1. Lua has no += or ++, so you increase a variable the long way. Something in the block must change the variable in the condition, so that the condition becomes false at some point. Without it, the loop would never end. You'll see what happens then in the next exercise.
Two mistakes you'll probably make at first. If you write then, as in an if, the program doesn't start:
local day = 1
while day <= 3 then
print("Day " .. day)
day = day + 1
endmain.lua:2: 'do' expected near 'then'And if you forget the end, Lua even tells you which loop it belongs to:
local day = 1
while day <= 3 do
print("Day " .. day)
day = day + 1main.lua:4: 'end' expected (to close 'while' at line 2) near <eof><eof> means “end of file”: Lua got to the end of the program and was still waiting for end.
Instructions
Delete the three print lines. In their place, create a local variable day with the value 1. The loop will count the days with it.
Below it, write a while loop that runs while day is less than or equal to 7. Inside the loop, print the day as the deleted lines did, and then increase day by 1. The program prints:
Day 1: collecting eggs
Day 2: collecting eggs
Day 3: collecting eggs
Day 4: collecting eggs
Day 5: collecting eggs
Day 6: collecting eggs
Day 7: collecting eggsAfter the loop, below its end, print After the loop, day is and the value of day. The output ends with:
Day 7: collecting eggs
After the loop, day is 8The variable ends up as 8, not 7, because the loop stops only when the condition is false.
Start programming for free
1/10