Still have questions? Join our Discord server and get real time help.
0

# i dont understand how does "break" work exactly, does it break all loops??

Asked by 1 year ago

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?

0
Answered by 1 year ago
Edited 1 year ago

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.