Lua repeat...until Loop

Lua 循环Lua Loops

In the Lua programming language, the repeat...until loop statement is different from the for and while loops. The condition of the for and while loops is evaluated at the beginning of the current loop execution, while the condition of the repeat...until loop is evaluated after the current loop ends.

Syntax

The syntax format of the repeat...until loop in the Lua programming language is:

repeat
   statements
until( condition )

We notice that the loop condition check (condition) is at the end of the loop body, so the loop body will be executed once before the condition is checked.

If the condition is false, the loop will restart execution, and will not stop until the condition becomes true.

The flowchart of the Lua repeat...until loop is as follows:

Example

Example

--[ Variable definition --]
a = 10
--[ Execute loop --]
repeat
   print("The value of a is:", a)
   a = a + 1
until( a > 15 )

After executing the above code, the program output is:

a的值为:    10
a的值为:    11
a的值为:    12
a的值为:    13
a的值为:    14
a的值为:    15

Lua 循环Lua Loops

Other Extensions