English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Lua Nested Loops

Lua Loops

Lua programming language allows nested loops within loops. The following example demonstrates the application of nested loops in Lua.

syntax

In Lua programming language for Nested loop syntax format:

for init,max/min value, increment
do
   for init,max/min value, increment
   do
      statements
   end
   statements
end

In Lua programming language while Nested loop syntax format:

while(condition)
do
   while(condition)
   do
      statements
   end
   statements
end

In Lua programming language repeat...until Nested 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 use different loop types for nesting, such as nesting a while loop within a for loop body.

Online Example

The following example uses nested for loops:

j =2
for i=2,10 do
   for j=2(i/j) , 2 do
      if(not(i%j)41;
      then
         break
      end
      if(j > (i/j)41;then
         print("The value of i is:",i)
      end
   end
end

The execution result of the above code is:

The value of i is:    8
The value of i is:    9
The value of i is:    10

Lua Loops