Easy Tutorial
❮ Lua Metatables Lua Nested Loops ❯

Lua while Loop

Lua Loops

In the Lua programming language, the while loop statement repeatedly executes the loop body statements as long as the condition is true.

Syntax

The syntax for a while loop in the Lua programming language is:

while(condition)
do
   statements
end

statements (loop body statements) can be one or more statements, and condition (condition) can be any expression. The loop body statements are executed when the condition (condition) is true.

The flowchart is as follows:

From the flowchart, we can see that when the condition (condition) is false, the current loop is skipped and the script proceeds to execute the next statements.

Example

The following example outputs the value of a in a loop:

Example

a=10
while( a < 20 )
do
   print("Value of a is:", a)
   a = a+1
end

Executing the above code produces the following output:

Value of a is: 10
Value of a is: 11
Value of a is: 12
Value of a is: 13
Value of a is: 14
Value of a is: 15
Value of a is: 16
Value of a is: 17
Value of a is: 18
Value of a is: 19

Lua Loops

❮ Lua Metatables Lua Nested Loops ❯