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

Lua While Loop

Lua Loops

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

Syntax

The syntax of while loop in Lua programming language:

while(condition)
do
   statements
end

statements (loop body statements) It can be one or more statements,condition (condition) It can be any expression, in condition (condition)  When the condition is true, execute the loop body statements.

The flowchart is as follows:

From the above flowchart, we can see that incondition (condition)When the condition is false, it will skip the current loop and start executing the next statement in the script.

Online Example

The following example loops to output the value of 'a':

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

Execute the above code, and the output result is as follows:

The value of 'a' is:    10
The value of 'a' is:    11
The value of 'a' is:    12
The value of 'a' is:    13
The value of 'a' is:    14
The value of 'a' is:    15
The value of 'a' is:    16
The value of 'a' is:    17
The value of 'a' is:    18
The value of 'a' is:    19

Lua Loops