step
).i
by 2. Since it begins with 1, it hops to 3, 5, 7, and 9. The stopping point is 10, and since after the loop variable is incremented from 9 to 11, the loop is terminated since the loop variable is greater than or equal to its stop variable.{5, 10, 15}
once the loop is finished. The iterator here is ipairs
and we pass the table to it, telling ipairs
to traverse that specific table for us. Each iteration, ipairs
returns a key and a value (the same key and value used in the table).ipairs
will only traverse an ordered set of data, like in the above example. To traverse an unordered set of data, we use pairs
.ipairs
will not work. By using pairs
, Lua will traverse it in any way possible, meaning it could get to c
before a
and b
, and any other possible combination.ipairs
and pairs
as your hands when looking through a book. If you have a book with 3 pages inside, with a number on each one (1, 2, 3) respectively, if your hands worked like ipairs
, you would look through them in order. Otherwise, if your hands worked like pairs
, you would flip through each one randomly, and never the same one twice.i
is never incremented, the condition would never be false and the loop would run forever. This code would print all numbers from 1 to 15.break
, you completely terminate the loop before it reaches its natural termination. This can be useful if you're looking for a specific value in a data set, like this:ipairs
to iterate in the exact sequence the table is defined), it will print that the value is found and will completely terminate the loop, preventing it from iterating any further.