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
end
Example: A function that performs addition
function add(a, b)
return a + b
end
Function Invocation
A function can be invoked by passing the required arguments:
sum = add(3, 4) -- The value of `sum` is 7
Multiple 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 = 7
Conditional 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
end
if..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
end
if..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
end
Loop 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
end
for 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
end
Example:
for i = 1, 5, 1 do
print(i)
end
Generic 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)
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
:
repeat
-- Statements executed at least once
until condition
Example:
x = 1
repeat
print(x)
x = x + 1
until x > 5