Basic Syntax of Lua
Variable Types
Lua supports several fundamental data types, as outlined in the table below:
| Type | Description | Example |
|---|---|---|
nil | Represents an invalid or uninitialized value. When a variable is not assigned a value, it defaults to nil. | nil |
boolean | Represents a boolean value, which can be either true or false. | true, false |
number | Represents numeric values, including both integers and floating-point numbers. | 1, -5, 3.1415 |
string | Represents text enclosed in either single or double quotation marks. | "hello", 'Lua' |
function | Represents a function, which can be either built-in or user-defined. | print, function add(a, b) return a + b end |
table | Represents 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:
function functionName(parameter)
-- Function body
endExample: A function that performs addition
function add(a, b)
return a + b
endFunction Invocation
A function can be invoked by passing the required arguments:
sum = add(3, 4) -- The value of `sum` is 7Multiple Return Values
A function in Lua can return multiple values:
function add(a, b)
return a, b, (a + b)
end
x, y, z = add(3, 4) -- x = 3, y = 4, z = 7Conditional Statements
if Statement
The if statement executes a block of code only if the given condition evaluates to true:
if condition then
-- Code to execute if the condition is true
endif..else Statement
If the condition evaluates to false, the else block is executed:
if condition then
-- Code to execute if the condition is true
else
-- Code to execute if the condition is false
endif..elseif..else Statement
For multiple conditions, elseif can be used:
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
endLoop Statements
while Loop
The while loop executes a block of code repeatedly as long as the condition remains true:
while condition do
-- Statements executed while the condition is true
endfor Loop
Numeric for Loop
The for loop iterates over a sequence of numbers, with an optional step value:
for variable = startValue, endValue, step do
-- Loop body
endExample:
for i = 1, 5, 1 do
print(i)
endGeneric for Loop
The generic for loop is commonly used for iterating over tables:
-- Iterating over an array
numbers = {"one", "two", "three"}
for index, value in ipairs(numbers) do
print(index, value)
endExplanation:
indexrepresents the position in the table.valuerepresents 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:
repeat
-- Statements executed at least once
until conditionExample:
x = 1
repeat
print(x)
x = x + 1
until x > 5