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

Lua repeat…until Loop

Lua repeat...until Loop

Lua loops Lua Loops

In the Lua programming language, the repeat...until loop statement differs from the for and while loops: the condition of for and while loops is evaluated at the start of the current loop iteration, while the condition of a repeat...until loop is evaluated after the current loop ends.

Syntax

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


repeat
   statements
until( condition )

We note that the loop condition (condition) is at the end of the loop body, so the loop body is executed once before the condition is evaluated.

If the condition (condition) is false, the loop starts over, and it stops only when the condition (condition) is true.

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

Example

Example

--[ Variable definition --]

a

=

10

--[ Execute the loop --]

repeat

   

print

(

"The value of a is:"

,

a

)

   a

=

a

1

until

(

a

15

)

After running the above code, the program outputs:


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

Lua loops Lua Loops