HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to LuaVariablesNumbers & MathStringsTablesConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Table
🔁 Condition & Loop
🧩 Function
Lua›Loops (for / while)

Loops (for / while)

Numeric for (end INCLUSIVE), iterating tables with ipairs, and while.

Simple enough for a 5-year-old to understand

Numeric for

for i = 1, 5 → from 1 to 5 (BOTH INCLUDED). Unlike other languages!

Example code
1
for i = 1, 3 do
1, 2, 3 (3 included).
Loop1, 2, 3 (3 included).
2
print(i)
3
end
Loop
💡 Hint: for i = 1, 5 prints all five numbers — the end value is INCLUSIVE.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Use a for loop to print numbers from 1 to 5 (inclusive), each on a new line.

🎯 Expected output: 1 2 3 4 5

Loading editor…

About this topic

There are three loop forms in Lua: numeric for, generic for, and while. In addition to these, the repeat-until structure runs the body at least once and checks the condition at the end.

The numeric for loop has an important feature: both the start and end values are included. When you write from one to ten, ten runs as well. Since many other languages exclude the upper bound, this difference constantly leads to errors.

The generic for loop is used to iterate over tables and works together with ipairs or pairs. Which one you choose depends on whether the table is an array or a dictionary.

What you will learn

  • The numeric for loop
  • The inclusion of bounds
  • Iterating over a table with generic for
  • The difference between while and repeat-until
  • The use of break

Who is this for?

  • Learners of repetitive operations
  • Those who think the upper bound is not included
  • Script writers who will traverse tables

When to use?

Use numeric for when the repetition count is known, and generic for when traversing a table.

Use while for tasks that continue until a condition is met; repeat-until is suitable when the body must run at least once.

Common mistakes

Wrong

local t = {"a", "b", "c"}
for i = 1, #t - 1 do
  print(t[i])
end

Because the upper bound is included in a numeric for, subtracting one from the length skips the last element; only two elements are printed to the screen. Written out of habit from other languages, this code does not throw an error, it silently runs incompletely — the hardest type of error to notice.

Correct

local t = {"a", "b", "c"}
for i = 1, #t do
  print(t[i])
end

Wrong

local i = 1
while i <= 5 do
  print(i)
end

Since the counter is not incremented, the condition remains true forever and the script never finishes. In a script running inside a game, this causes the game to freeze completely. You have to ensure progress in while loops.

Correct

local i = 1
while i <= 5 do
  print(i)
  i = i + 1
end

Wrong

local t = {"a", "b", "c"}
for i, v in ipairs(t) do
  if v == "b" then continue end
  print(v)
end

There is no continue keyword in Lua; this code gives a syntax error. You achieve the same effect by reversing the condition or with the goto command. Its absence is one of the language's well-known topics of discussion.

Correct

local t = {"a", "b", "c"}
for i, v in ipairs(t) do
  if v ~= "b" then
    print(v)
  end
end

Frequently asked questions

How is the step determined in a numeric for?

You write a third value; the default is one. You can count backwards by providing a negative step, in which case the start must be greater than the end. If the step is given as zero, the loop runs forever.

What is the difference between repeat-until and while?

while checks the condition at the beginning and the body may never run; repeat-until checks at the end, the body runs at least once. Also, repeat-until stops when the condition becomes true, whereas while continues as long as it is true — the logic is reversed.

How do I break the outer loop in a nested loop?

break only terminates the innermost loop. To exit the outer one, you need to use a flag variable or jump outside the loop with goto. There are no labeled breaks in Lua.

Summary

In a numeric for, the start and end are included.

ipairs or pairs are used to traverse tables.

You must ensure progress in a while loop.

There is no continue keyword.

repeat-until runs the body at least once.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code