repeat is a reserved word in the programming language Lua.
In the BNF syntax of Lua, it appears as a terminal atom in one substitution rule:
stat → repeat block until exp1
"stat" appears as a nonterminal atom in one substitution rule:
chunk → {stat [';']}
repeat is one of the three control structures of Lua (the other two being while and if). The condition expression (also called the guard) of a control structure may return any value. All values different from nil are considered true; only nil is considered false.
The body of the loop (block) is executed. From there, the behaviour of the repeat structure is identical to that of the while structure. The condition expression is evaluated, and every time it passes (evaluates to true) the body is executed and the condition expression is evaluated again. Once it fails (evaluates to false), the loop is over.
The flow of control of a repeat loop can be broken by the break statement, as with the while loop. If, in executing the block, Lua runs into a break statement, the rest of the body is skipped, the condition expression is not evaluated, and the program resumes immediately after the end of the control structure. Of course, a break only exits from one loop - a in a loop which is itself nested within a loop, will only exit from the innermost loop. while and for also count as loops, so for example a break in a while in a repeat will break out of the while loop only.
Quite often, a programmer may wish to break out of a loop which is not the innermost loop. Some programming languages provide a feature for naming a loop (or even just a section of code), and add the ability to associate a name with a break - so, in the previous example, if the outer loop was named 'RepeatLoop', then a break RepeatLoop will break out of both loops.
repeat can be implemented in terms of while, as follows:
repeat
block
until exp1
becomes
FirstIteration = true
while (exp1 or FirstIteration)
block
FirstIteration = false
end
or, even more simply,
block
while exp1
block
end
while can be expressed in terms of repeat, too, but that requires an extra control structure (an if). Thus, while is considered to be more fundamental a structure than repeat.
The superloop of a program sometimes looks like:
repeat
block
until (true)
The program usually exits by use of the break statement, or through some operating system-provided means, usually an exception (e.g. in BASIC programs, the Escape key used to always exit the program - it was impossible, within early BASICs, to trap the Escape key), or by means of an instruction like exit (which is like a named break, where the name is associated with the entire program rather than any particular part of it).