Nested loops

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
end
end
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.165.62