Skip to content

Basic Syntax of Lua

Variable Types

Lua supports several fundamental data types, as outlined in the table below:

TypeDescriptionExample
nilRepresents an invalid or uninitialized value. When a variable is not assigned a value, it defaults to nil.nil
booleanRepresents a boolean value, which can be either true or false.true, false
numberRepresents numeric values, including both integers and floating-point numbers.1, -5, 3.1415
stringRepresents text enclosed in either single or double quotation marks."hello", 'Lua'
functionRepresents a function, which can be either built-in or user-defined.print, function add(a, b) return a + b end
tableRepresents a table, which is Lua’s primary data structure. Tables are defined using curly brackets {} and use 1-based indexing by default.{1, 2, 3}

Functions

Function Definition

The syntax for defining a function in Lua is as follows:

lua
function functionName(parameter)
    -- Function body
end

Example: A function that performs addition

lua
function add(a, b)
    return a + b
end

Function Invocation

A function can be invoked by passing the required arguments:

lua
sum = add(3, 4)  -- The value of `sum` is 7

Multiple Return Values

A function in Lua can return multiple values:

lua
function add(a, b)
    return a, b, (a + b)
end

x, y, z = add(3, 4)  -- x = 3, y = 4, z = 7

Conditional Statements

if Statement

The if statement executes a block of code only if the given condition evaluates to true:

lua
if condition then
    -- Code to execute if the condition is true
end

if..else Statement

If the condition evaluates to false, the else block is executed:

lua
if condition then
    -- Code to execute if the condition is true
else
    -- Code to execute if the condition is false
end

if..elseif..else Statement

For multiple conditions, elseif can be used:

lua
if condition1 then
    -- Code executed if condition1 is true
elseif condition2 then
    -- Code executed if condition2 is true
else
    -- Code executed if none of the conditions are true
end

Loop Statements

while Loop

The while loop executes a block of code repeatedly as long as the condition remains true:

lua
while condition do
    -- Statements executed while the condition is true
end

for Loop

Numeric for Loop

The for loop iterates over a sequence of numbers, with an optional step value:

lua
for variable = startValue, endValue, step do
    -- Loop body
end

Example:

lua
for i = 1, 5, 1 do
    print(i)
end

Generic for Loop

The generic for loop is commonly used for iterating over tables:

lua
-- Iterating over an array
numbers = {"one", "two", "three"}
for index, value in ipairs(numbers) do
    print(index, value)
end

Explanation:

  • index represents the position in the table.
  • value represents the corresponding element.
  • ipairs() is a built-in iterator function used to traverse arrays.

repeat...until Loop

The repeat...until loop executes a block of code at least once and continues looping until the condition evaluates to true:

lua
repeat
    -- Statements executed at least once
until condition

Example:

lua
x = 1
repeat
    print(x)
    x = x + 1
until x > 5

References