Lua while loop

Lua 循环Lua Loops

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

Syntax

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

while(condition)
do
   statements
end

statements (loop body statements)Can be one or more statements,condition (condition)can be any expression, whencondition (condition)is true, execute the loop body statements.

The flowchart is as follows:

In the above flowchart, we can see that whencondition (condition)is false, it will skip the current loop and begin executing the immediately following statement.

Example

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

Example

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

After executing the above code, the output result is as follows:

a 的值为:    10
a 的值为:    11
a 的值为:    12
a 的值为:    13
a 的值为:    14
a 的值为:    15
a 的值为:    16
a 的值为:    17
a 的值为:    18
a 的值为:    19

Lua 循环Lua Loops

Other Extensions