Lua Loops

A loop statement allows the execution of a statement or group of statements multiple times. There are three types of loop statement in Lua.

  • ‘for’ loop – Useful when it is known in advance the number of times the statements within the loop need to be executed.
  • ‘while’ loop – Repeats a statement or group of statements while an expression evaluates to true. There is a condition to get into the loop, so if the condition evaluates to true at the beginning, then the statements inside the loop will never execute.
  • ‘repeat until’ loop – This is similar to the ‘while’ loop except the condition is at the end, so the statements within the loop will always execute once.

‘for’ Loop

The ‘for’ loop takes the following form.

for variable_name = start_value, end_value, step_value
do
    -- Statement(s) to execute.
end

The loop has a variable that is set to a value that it needs to start from and acts as the loop counter. The end value signifies the point at which the loop will stop. Finally, the step value specifies what the loop counter variable should be incremented by with each iteration of the loop.

The following example outputs to the screen the value of the loop counter for each iteration of the loop.

for i = 1, 5, 1
do
    print(i)
end

As the start and end values are set to one and five respectively, along with a step value of one, the code within the loop will execute five times and produce the below output.

1
2
3
4
5

It should be noted that where the step value is one, it can be omitted, and the same result is achieved.

for i = 1, 5
do
    print(i)
end

The step value can also be used to decrement the loop counter variable by specifying a minus value.

for i = 5, 1, -1
do
    print(i)
end

The output will now be the reverse of the above examples.

5
4
3
2
1

‘while’ Loop

As mentioned above, the ‘while’ loop has a condition, which must evaluate to true before the statements within it can be executed.

while condition
do
    -- Statement(s) to execute.
end

In the below example, a variable is declared and initialised to one, then the ‘while’ loop checks to see that the variable is less than or equal to five before outputting it to the screen and incrementing the variable by one.

i = 1

while i <= 5
do
    print(i)
    i = i + 1
end

The output will be the same as in the ‘for’ loop example above, displaying the numbers one through five to the screen. Note that if the variable ‘i’ had been declared and initialised to six or above, then the statements within the loop would not be executed at all.

‘repeat until’ Loop

The ‘repeat until’ loop is a variation of the ‘while’ loop, where there is a condition to exit the loop rather than enter it. This means that the statements within the loop will always be executed at least once.

repeat
    -- Statement(s) to execute.
until condition

The example below initialises a variable to one, outputs its value and adds one to it, until it is greater than five.

i = 1

repeat 
    print(i)
    i = i + 1
until i > 5

Again, the output will be the same as in the previous examples where one through five is output to the screen. Note that if the variable ‘i’ was initialised to six or above, it would still be output to the screen once because the condition checking whether it is greater than five is at the end of the loop.