The break Statement
Sometimes you don't need to go through the whole loop. When you're looking for something and you find it, there's no reason to keep looking. The break statement ends the loop right away, and the program continues after the loop's end.
Hugo the trader comes every other day, and it rains every 3rd day. On which day does Hugo first come in the rain? The loop prints each day it checks, so you can see where it stops:
for day = 1, 30 do
print("Checking day " .. day)
if day % 2 == 0 and day % 3 == 0 then
print("Hugo comes in the rain on day " .. day)
break
end
end
print("Done")Checking day 1
Checking day 2
Checking day 3
Checking day 4
Checking day 5
Checking day 6
Hugo comes in the rain on day 6
DoneOn day 6, the condition is true. The program prints the result, and break ends the loop: days 7 to 30 are never checked, and the program goes on with print("Done"). Without break, the loop would go all the way to day 30 and also report days 12, 18, 24 and 30.
You'll almost always put break inside an if; on its own, it would end the loop in its first iteration. It works the same way in while, for and repeat loops. You'll also see Lua programs with while true do: a loop whose condition is always true. It would never end by itself, so somewhere inside, it has an if with break.
To know after the loop what you found, store it in a variable created before the loop. If the loop finds nothing, the variable stays nil, and an if can tell. Grandpa Joe visits every 4th day and Hugo every 6th day. Do they meet in the first 10 days?
local meeting
for day = 1, 10 do
if day % 4 == 0 and day % 6 == 0 then
meeting = day
break
end
end
if meeting then
print("They meet on day " .. meeting)
else
print("They don't meet")
endThey don't meetWith for day = 1, 20 do, the program would print They meet on day 12.
Lua has no continue. In many languages, continue skips the rest of the current iteration and goes on to the next one. Lua doesn't know it: it takes continue for a variable name and then stumbles over the end after it. The program doesn't even start:
for day = 1, 4 do
if day == 2 then
continue
end
print("Day " .. day)
endmain.lua:4: syntax error near 'end'Instead, put the rest of the block in an if with the opposite condition:
for day = 1, 4 do
if day ~= 2 then
print("Day " .. day)
end
endDay 1
Day 3
Day 4(Lua also has goto, which can jump to the end of the block, but you won't need it in this course. Roblox's Luau has continue; standard Lua doesn't.)
Instructions
Emma has 40 coins and earns 15 more every day, as the loop shows. In the loop, below the line that prints the coins, add an if that prints Day 8: Emma buys the cart! (with the day's number) when she has at least 150 coins. Run the program: the message appears on every day from day 8 on, but Emma needs only one cart. Among the lines, you'll find:
Day 8: 160 coins
Day 8: Emma buys the cart!Add break to the block of the if, after the printing, so that the loop stops on the day Emma buys the cart. The last two lines of the loop's output are then:
Day 8: 160 coins
Day 8: Emma buys the cart!Below the loop, write another loop for Emma's market stall. It is open on days 1 to 10, but not on the rainy days, which are every 3rd day. Print one line for each open day. Lua has no continue, so use an if. The end of the output is:
Day 1: market
Day 2: market
Day 4: market
Day 5: market
Day 7: market
Day 8: market
Day 10: marketStart programming for free
8/10