Use table.clear() instead of break

I find that when I do this:

local array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
for i, v in pairs(array) do
   print(v)
   if v == 10 then
      table.clear(array)
   end
end

The output shows this:

1
2
3
4
5
6
7
8
9
10

Then I do a second try by using break and it gives me the same result.
So I’m confused that if I can use table.clear() instead of break to break the loop

It’s because you’re clearing the table so there is nothing to loop through anymore.

Edit: so yes you could use table.clear to stop it instead of break

2 Likes