The break
statement will skip to the line after the for
, while
, or repeat
loop it's in.
while true do
if turtle.detect() then
if turtle.getItemCount(16) == 64 then
break
end
turtle.dig() -- digs block in front of it
end
end
-- break skips to here
A quirk of lua: break
has to come just before an end
, though not necessarily the end
of the loop you want to break out of, as you can see here.
Also, if you want to exit the loop on a condition at the start or end of the loop, as above, often you can change up the loop you're using to get a similar effect. For instance, in this example we could put the condition in the while
loop:
while turtle.getItemCount(16) < 64 do
if turtle.detect() then
turtle.dig()
end
end
Note that I subtly changed the behaviour a bit there, as this new loop will stop right away when it hits the item count limit, without continuing until detect()
becomes true again.