Iterators
An iterator repeatedly executes a code block a number of times. This number may be determined or indefinite. In the latter case, an iterator will endlessly execute a code block until some external event occurs, such as the user closing the program.
Common types of iterators in Luau are described below.
while
Loops
A while
loop executes a code block repeatedly until a specific condition is evaluated as false
.
while
Loop Examples and Syntax
remaining_loops = 5while remaining_loops > 0 doprint("There are " .. remaining_loops .. " loops remaining.")remaining_loops -= 1endprint("Enough with the loops!")
In the example above, the code block between while
and end
will be executed repeatedly until remaining_loops > 0
is false
. This results in the following output:
There are 5 loops remaining.There are 4 loops remaining.There are 3 loops remaining.There are 2 loops remaining.There are 1 loops remaining.Enough with the loops!
By using a condition that will never evaluate as false
, while
loops can run indefinitely:
while true do-- Add code here that will run indefinitelytask.wait(0.1)--A delay is needed when using an indefinite loop to prevent crashes.end
Indefinite loops are useful for programs that are intended to run until a user chooses to end them.
repeat
Loops
A repeat
loop is similar to a while loop. The key difference is that a repeat loop evaluates its condition after the first run of its code block.
repeat
Loop Example and Syntax
repeatprint("This will always run at least once.")until false
This results in the output:
This will always run at least once.
for
Loops
A for
loop iterates through a series of elements and executes a code block at each one of those elements. This series could be a series of numbers, elements in a table, or characters in a string.
Numeric for
Loops
A numeric for
loop iterates through a series of numbers. A numeric for
loop includes a start value, end value, and an optional increment value.
for i = start_value, end_value, increment_value do
--Add code to be repeated here
end
start_value
initializes the value of i
. increment_value
increments the value of i
after each run of the loop. If increment_value
is positive, the loop will end once i
is greater than or equal to end_value
. If increment_value
is negative, the will end when i
is less than or equal to end_value
.
Numeric for
Loop Examples
Excluding the optional increment value parameter results in a default increment value of 1
:
for i = 1, 3 doprint(i)end
Output:
123
An example using a negative increment value:
for i = 10, 1, -2 doprint(i)end
Output:
108642
Generic for
Loops
A generic for
loop iterates through the indices of a table or the characters in a string.
pairs()
The pairs()
function can be used to iterate through all elements in a table:
mixed_table = {}mixed_table[1] = 1mixed_table[2] = 0.5mixed_table[5] = "biff"mixed_table["a"] = 7for i,v in pairs(mixed_table) doprint("The value at index " .. i .. " is " .. v)end
Output:
The value at index 1 is 1The value at index 2 is 0.5The value at index 5 is biffThe value at index a is 7
Note: The order in which the elements of the table are looped through is not guaranteed when using
pairs()
.
ipairs()
The ipairs()
function can also be used to iterate through a table, but it has a few key differences from pairs()
:
ipairs()
will ignore any elements with non-integer indices.ipairs()
will begin at index1
and ignore any elements at earlier indices.ipairs()
will stop iterating if there are any integer gaps in the indices.ipairs()
guarantees the order in which the elements are looped through.
Due to these differences, ipairs()
is best used for iterating through arrays.
Compare the behavior of ipairs()
when used on the same table as above:
mixed_table = {}mixed_table[1] = 1mixed_table[2] = 0.5mixed_table[5] = "biff"mixed_table["a"] = 7for i,v in ipairs(mixed_table) doprint("The value at index " .. i .. " is " .. v)end
Output:
The value at index 1 is 1The value at index 2 is 0.5
string.gmatch()
The string.gmatch()
function can be used to iterate through characters in a string:
for char in string.gmatch("sarsaparilla", ".") doprint(char)end
Output:
sarsaparilla
In the above example, "."
is used as a special character that matches all other characters.
break
The keyword break
can be used in any type of loop to instantly force a loop to end.
for i = 1, 10 doif(i == 6) thenbreakendprint(i)end
Output:
12345
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Luau on Codecademy
- Free course
Introduction to Game Development
Explore video game development, including game design, gameplay development, and asset creation.Beginner Friendly1 hour - Free course
Learn Lua
Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.Beginner Friendly4 hours