In Lua, next and continue are keywords that are used in loops to control the iteration process.
next is used primarily with tables, and it returns the next key-value pair in the table. It takes two arguments, the table to iterate over (required), and the current key (optional). Here's an example:
-- a table
local myTable = {a = 1, b = 2, c = 3, d = 4}
-- iterate over the table using next and print each key-value pair
local key, value = nil, nil
while true do
key, value = next(myTable, key)
if not key then break end -- exit the loop once all key-value pairs have been seen
print(key, value)
end
In this example, we create a table called myTable with four key-value pairs. We then use a while loop to iterate over the table using next. The loop will run until all key-value pairs have been seen, which is detected by the if not key then break end statement. Each time through the loop, next returns the next key-value pair, which we store in the key and value variables, and then we print these variables.
continue is used in loops to skip a particular iteration of the loop and move on to the next. It is not a keyword in Lua, but it can be simulated using the goto statement. Here's an example:
-- iterate over a table and print each even number
local myTable = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
for _, v in ipairs(myTable) do
if v % 2 == 1 then goto continue end -- skip to the next iteration if the number is odd
print(v)
::continue:: -- label for the skipped iteration
end
In this example, we create a table called myTable with ten values. We then use a for loop to iterate over the table. For each value, we check if it is even or odd using the if v % 2 == 1 then goto continue end statement. If the value is odd, we skip to the next iteration using a goto statement to the continue label. If the value is even, we print it using print(v). The ::continue:: label is used to mark the skipped iteration, and it is required for the goto statement to work properly.