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
if / elseif / elsefor (numeric)ipairs / pairswhile
🧩 Function
Lua›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision making and repetition.

CommandDescription
if / elseif / elseExecutes code based on a condition.
for (numeric)Numeric loop.
ipairs / pairsIterates over table elements.
whileLoops as long as the condition is true.

if / elseif / else

Try it yourself →

Executes code based on a condition.

Syntax

if condition then ... elseif ... else ... end

Description

Written with then and end. Not equal is written with ~= (not !=). Keywords and/or/not are used.

Example

local score = 75
if score >= 50 then
  print("Passed")
else
  print("Failed")
end

Expected output

Passed

When to use?

Flow branching.

Common mistakes

  • ⚠️ Not equal is ~=, NOT !=. Blocks are closed with end.

for (numeric)

Try it yourself →

Numeric loop.

Syntax

for i = start, end, step do ... end

Description

for i = 1, 5 → from 1 to 5 (both inclusive). The third number is the step.

Example

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

Expected output

1
2
3

When to use?

Known repetition.

Common mistakes

  • ⚠️ for i=1,5 → 5 IS INCLUSIVE (unlike other languages).

ipairs / pairs

Try it yourself →

Iterates over table elements.

Syntax

for i, v in ipairs(t) do

Description

ipairs iterates over sequential arrays (index, value), pairs iterates over dictionaries (key, value).

Example

for i, v in ipairs({"al", "mor"}) do
  print(i, v)
end

Expected output

1	al
2	mor

When to use?

Table iteration.

while

Try it yourself →

Loops as long as the condition is true.

Syntax

while condition do ... end

Description

Repeats the block as long as the condition is met.

Example

local s = 0
while s < 3 do
  print(s)
  s = s + 1
end

Expected output

0
1
2

When to use?

Indefinite repetition.

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