July 2018
Beginner
202 pages
5h 42m
English
Much like control structures, loops operate on a chunk of code. Also, similar to control structures, loops can be nested. You can nest different types of loops within each other. The same scope rules apply to nesting loops as to everything else so far.
If you have a break statement, it will only break one loop, the innermost loop closest to the statement. The following piece of code demonstrates breaking out of nested loops:
for i = 0, 10 do local j = 0 while j < 10 do print ("j: " .. j) -- Will never be > 2 if j == 2 then print ("j is: " .. j .. ", i is:" .. i) break end j = j + 1 endend