Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Lua while Loop

Lua while Loop

Lua loops 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 the while loop in the Lua programming language:


while(condition)
do
   statements
end

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

The flowchart is as follows:

In the above flowchart we can see that when **condition** is false, the current loop is skipped and the script starts executing the immediately following statement.

Example

The following example loops to output the value of a:

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 is as follows:


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

Lua loops Lua Loops