Lua while cycle


Release date:2023-09-27 Update date:2023-10-13 Editor:admin View counts:382

Label:

Lua while cycle

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

Grammar

In Lua programming language while loop syntax:

while(condition)
do
   statements
end

statements (loop body statement) can be one or more statements condition can be any expression, in the condition is true , the loop body statement is executed.

The flow chart is as follows:

Image0

In the above flow chart, we can see that in condition (condition) is false skips the current loop and starts the script to execute the statement that follows.

Example

The following example loops the output a value:

Example

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

Execute the above code, and the output 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

Powered by TorCMS (https://github.com/bukun/TorCMS).