Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years 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?

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years 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.

Useful link

Ad

Answer this question