Loops in Lua allow you to execute a block of code repeatedly until a certain condition is met. They are indispensable tools for performing iterative tasks such as iterating over arrays, processing data, and controlling game logic. In lua, there are 3 main types of loops:
The while loop in Lua executes a block of code as long as a specified condition is true.
Example:
-- Basic while loop local count = 0 while count < 5 do --While count is less than 5 then print("Count:", count) count = count + 1 end
The repeat-until loop is similar to the while loop, but it executes the block of code at least once before checking the condition.
Example:
-- Basic repeat-until loop local num = 1 repeat print("Number:", num) num = num + 1 until num > 5
The for loop in Lua is typically used for iterating over a range of values or elements in a collection. It consists of an initialization, a condition, and an optional step (value added to the initialization each iteration), there are two types of for loops, a numeric one, and a generic one
The numeric for loop is used to iterate over a range of numeric values. It has three parts: the loop variable initialization, the condition, and the step. The loop variable is initialized to the starting value, and the loop continues as long as the loop variable does not meet the condition. After each iteration, the loop variable is incremented by the step value. If no step value is specified, it will be 1
-- Numeric for loop iterating from 1 to 5 for i = 1, 5 do print("Iteration", i) end -- Numeric for loop with a step for i = 1, 10, 2 do print("Iteration", i) end
In the first example, the loop iterates from 1 to 5, incrementing i by 1 in each iteration. In the second example, the loop iterates from 1 to 10 with a step of 2, meaning i is incremented by 2 in each iteration.
The generic for loop is used to iterate over the elements of a table. It iterates over each key-value pair, or index-value pair in the table.
Example:
-- Generic for loop iterating over values in a table using ipairs local fruits = {"apple", "banana", "orange"} for index, value in ipairs(fruits) do print("Fruit:" .. value) print("Current index: ".. index) end -- Generic for loop iterating over key-value pairs in a table using pairs local player = {name = "Alice", score = 100} for key, value in pairs(player) do print(key .. ":" .. value) end
In the first example, ipairs is used to iterate over the values of a table with sequential numeric indices the loop iterates over each element of the fruits table, storing the value in the fruit variable and also giving us the index
In the second example, pairs is used to iterate over all key-value pairs in the player table, including non-numeric keys. Each iteration assigns the key to the key variable and the corresponding value to the value variable.
Lua provides loop control statements such as break and a way to implement the continue to alter the flow of loop execution.
-- Using break statement for i = 1, 10 do if i == 5 then break end print("Iteration", i) end
This code will exit the loop when the iteration is equal to five, so it will only print until 4, and will stop at 5
Lua by default, does not provide support for a continue statement, but we can make a kind of workaround, with a statement called goto this way
for i = 1, 5 do if i == 3 then goto continue end print("Iteration", i) ::continue:: end
However, try to use goto with caution, as it can cause something called as Spaghetti code, which is more difficult to maintain and read