Codectionary / Developer documentation / Lua

Lua Conditions & Loops

Lua uses then/end for conditionals and do/end for loops. In Lua, only false and nil are falsey; zero and empty strings are truthy.

Syntax

if ready then
  print("go")
end

for i = 1, 3 do
  print(i)
end

Examples

Iterating a sequence

A small, runnable example of this syntax.

local tags = { "lua", "game", "web" }
for index, tag in ipairs(tags) do
  print(index, tag)
end

Best practices

  • Use ipairs for sequential array-like tables and pairs when you want to visit all key-value entries without ordering guarantees.
  • Keep examples small while learning, then combine the idea with a real project.

At a glance

Purpose
Lightweight scripting and embedding
File extension
.lua
Runs in
Lua interpreter or a host application
Usually used with
Host APIs and Lua modules

Specifications & further reading

Related Lua documentation