Lua Loops - Lua (programming language)

What are different types loop types in Lua?

Using loops, statements can be executed sequentially − first statement in a function will be executed first, followed by the second and so on. Programming languages provide a variety of control structures which allow more complicated execution paths.

A loop statement helps in executing a statement or group of statements multiple times. Below is the general form of a loop statement in most of the programming languages.
Lua provides below types of loops for handling looping requirements.

Loop Type


while loop

while loop will repeat a statement or group of statements when a given condition is true. It will test the condition before executing the loop body.

for loop

for loop will execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

repeat...until loop

repeat...until loop will repeat the operation of group of statements until the until condition is met.

nested loops

You can use one or more loop inside any another while, for or do while loop.

Loop Control Statement

Loop control statement will change the execution from its normal sequence. When execution leaves a scope, all automatic objects which were created in that scope will be destroyed.

Lua supports below control statements.

Control Statement


break statement

Break statement will terminate the loop and it will transfer execution to the statement immediately following the loop or switch.

Infinite Loop

A loop will become an infinite loop if a condition never becomes false. While loop is frequently used for this purpose. As we are directly giving true for the condition, it will keep executing forever. Break statement can be used to break this loop.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Lua (programming language) Topics