Lua Nested Loops
In the Lua programming language, loops can be embedded within loops. The following example demonstrates the application of Lua nested loops.
Syntax
In the Lua programming languageforNested loop syntax format:
for init,max/min value, increment
do
for init,max/min value, increment
do
statements
end
statements
end
In the Lua programming languagewhileNested loop syntax format:
while(condition)
do
while(condition)
do
statements
end
statements
end
In the Lua programming languagerepeat...untilNested loop syntax format:
repeat
statements
repeat
statements
until( condition )
until( condition )
In addition to the above nested loops of the same type, we can also nest different types of loops, such as nesting a while loop inside a for loop body.
Example
The following example uses for loop nesting:
Example
j =2
for i=2,10 do
for j=2,(i/j) , 2 do
if(not(i%j))
then
break
end
if(j > (i/j))then
print("The value of i is:",i)
end
end
end
for i=2,10 do
for j=2,(i/j) , 2 do
if(not(i%j))
then
break
end
if(j > (i/j))then
print("The value of i is:",i)
end
end
end
The execution result of the above code is:
i 的值为: 8 i 的值为: 9 i 的值为: 10Other Extensions
Lua Loops