0Pricing
Lua Academy · Lesson

Iterating with ipairs

Loop over sequences in order.

Iterating with ipairs is a free Lua Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why ipairs

Walking a sequence by index works, but ipairs is cleaner and safer. It hands you each index and value in order.

It starts at index 1 and stops at the first nil, perfect for gap-free sequences.

local fruits = {"apple", "banana", "cherry"}
for i, v in ipairs(fruits) do
  print(i, v)
end

The Loop Shape

The pattern is for index, value in ipairs(t) do ... end.

On each pass, index is the 1-based position and value is the element there. No manual counter needed.

local colors = {"red", "green", "blue"}
for i, c in ipairs(colors) do
  print("Color", i, "is", c)
end

Ignoring the Index

If you only need the values, name the index _ by convention to show it is unused.

The loop still visits items in 1-based order.

local nums = {10, 20, 30}
local total = 0
for _, n in ipairs(nums) do
  total = total + n
end
print("sum:", total)

Starts at 1, Stops at a Gap

ipairs begins at index 1 and keeps going while the next index is non-nil.

If there is a hole, it stops there. So ipairs is for clean sequences, not tables with missing positions.

local t = {"a", "b", nil, "d"}
for i, v in ipairs(t) do
  print(i, v)
end

Summing Values

A frequent task is reducing a list to one number, like a sum or a max.

ipairs makes this readable: visit each value and accumulate.

local prices = {3, 7, 2, 9}
local max = prices[1]
for _, p in ipairs(prices) do
  if p > max then max = p end
end
print("max:", max)

Building a New List

Combine ipairs with table.insert to transform a sequence into another one.

Here we double each number into a fresh table, keeping order and 1-based indices.

local src = {1, 2, 3}
local doubled = {}
for _, n in ipairs(src) do
  table.insert(doubled, n * 2)
end
print(doubled[1], doubled[2], doubled[3])

ipairs vs pairs

pairs visits every key, including string keys, in no guaranteed order. ipairs visits only the integer sequence 1, 2, 3... in order.

For ordered arrays, reach for ipairs.

local t = {"x", "y"}
t.name = "extra"
for i, v in ipairs(t) do
  print(i, v)
end

Filtering With a Condition

Use an if inside the loop to keep only matching items.

Below we collect words longer than three letters into a new sequence.

local words = {"hi", "hello", "yo", "howdy"}
local long = {}
for _, w in ipairs(words) do
  if #w > 3 then table.insert(long, w) end
end
print(long[1], long[2])

Finding an Item

To search, loop with ipairs and stop when found. The index tells you the 1-based position.

We return early using break once the target appears.

local names = {"Ann", "Bob", "Cy"}
local found = 0
for i, n in ipairs(names) do
  if n == "Bob" then found = i break end
end
print("Bob at index", found)

Counting While Iterating

You can count matches by incrementing a variable inside the loop.

This avoids touching indices directly while still scanning the whole sequence.

local marks = {5, 8, 3, 9, 6}
local passes = 0
for _, m in ipairs(marks) do
  if m >= 6 then passes = passes + 1 end
end
print("passing:", passes)

Nested ipairs

For a table of tables, nest two ipairs loops to visit every cell.

The outer loop walks rows, the inner walks columns, all 1-based.

local grid = {{1, 2}, {3, 4}}
for _, row in ipairs(grid) do
  for _, cell in ipairs(row) do
    print(cell)
  end
end

Quick Check

Recall where ipairs begins and ends.

Recap

You learned the for i, v in ipairs(t) pattern: it visits a sequence in 1-based order and stops at the first gap.

Use it to sum, filter, search, and transform lists. Next you will measure length with # and order items with table.sort.

Frequently asked questions

Is the “Iterating with ipairs” lesson free?

Yes — the full text of “Iterating with ipairs” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Iterating with ipairs”?

Loop over sequences in order. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Iterating with ipairs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays as Tables
  2. Adding and Removing Items
  3. Iterating with ipairs
  4. Length and Sorting
← Back to Lua Academy