A loop in a loop, breaking both loops
Posted: Mon May 13, 2013 8:50 pm
Code: Select all
for i=1,10 do
_j = 0
for j=1,10 do
print(j)
_j = j
if (j==4) then
break
end
end
if (_j~=10) then
break
end
end
Code: Select all
for i=1,10 do
_j = 0
for j=1,10 do
print(j)
_j = j
if (j==4) then
break
end
end
if (_j~=10) then
break
end
end
goto is only in Lua 5.2 and LuaJIT, not regular 5.1 (which is what love uses by default.)adnzzzzZ wrote:You can always use goto to break from nested loops. I'm ready
ferdielance wrote:Depending on your needs, the following may work more simply, or may not be practical at all.
* A: Put the whole double loop structure inside a function, and use return to get out when the time comes. (Not good in situations where having a function call every time you do the double loop is too costly)
* B: Use while for the outer loop (while counter<= value and looping == true do) and set "looping" to false in the inner loop when the time comes. This feels neater to me than having a separate break outside.
Code: Select all
i, loop = 1, true
while (i<10 and loop) do
for j=1,10 do
print(j)
if (j==4) then
loop = false
break
end
end
i=i+1
end
I'm trying to make a tile-based platformer. I need to check all tiles, and if I hit a solid one, both loops need to break.Lafolie wrote:Why are you doing this? Sounds like a while loop would be much more useful.