if i have 2 "for loops" inside each other, and i use "break" in the inner one does it exit from both loops or only the inner one?
✓ ANSWER ACCEPTED
i dont understand how does "break" work exactly, does it break all loops??
1 answer
✓ Accepted answer
The break statement only breaks the innermost loop. So even in nested loops, it only breaks the loop the statement is inside of.
This example only breaks the while loop:
for i = 1, 10 do
while true do
print("hi\n------")
break -- while loop broken out of
end
print(i)
end
And you can have as many nested loops as you want, only the innermost loop is broken.
This discussion is preserved as it was. New answers, comments, and votes are closed.
