Basic syntax
Data Types
Lua has eight data types:
nil
: represents the absence of a value.boolean
: represents true or false.number
: represents numeric values.string
: represents text.table
: represents an associative array (i.e., a map or dictionary).function
: represents a piece of executable code.userdata
: represents arbitrary C data.thread
: represents an independent thread of execution.
Here are some examples of how to create variables of different types in Lua:
-- nil
local n = nil
-- boolean
local b = true
-- number
local x = 42
local y = 3.14
-- string
local s1 = "hello"
local s2 = 'world'
-- table
local t = { foo = "bar", baz = 42 }
-- function
local f = function(x) return x * 2 end
Variables
In Lua, variables are dynamically typed and don't require explicit declaration. You can assign any value to a variable at any time, and the type of the variable will change to match the type of the value. Variable names in Lua can contain letters, digits, and underscores, and must start with a letter or underscore.
Here's an example of how to create and use variables in Lua:
local x = 42
local y = 3.14
local z = "hello"
print(x + y) -- prints 45.14
print(z .. " world") -- prints "hello world"
Operators
Lua has a variety of arithmetic, relational, and logical operators. Here are some examples:
-- arithmetic operators
local x = 10
local y = 3
print(x + y) -- prints 13
print(x - y) -- prints 7
print(x * y) -- prints 30
print(x / y) -- prints 3.3333333333333
print(x % y) -- prints 1
print(-x) -- prints -10
-- relational operators
local a = 42
local b = 13
print(a < b) -- prints false
print(a <= b) -- prints false
print(a > b) -- prints true
print(a >= b) -- prints true
print(a == b) -- prints false
print(a ~= b) -- prints true
-- logical operators
local p = true
local q = false
print(p and q) -- prints false
print(p or q) -- prints true
print(not p) -- prints false
Control Structures
Lua has a variety of control structures, including if/then/else statements, loops, and function calls. Here are some examples:
-- if/then/else statement
local x = 42
if x < 10 then
print("x is less than 10")
elseif x < 20 then
print("x is less than 20")
else
print("x is greater than or equal to 20")
end
-- while loop
local i = 1
while i <= 10 do
print(i)
i = i + 1
end
-- for loop
for i = 1, 10 do
print(i)
end
-- function call
local function square(x)
return x * x
end
print(square(4)) -- prints 16
Control Structures (continued)
In addition to if/then/else statements, while loops, and for loops, Lua also has repeat/until loops and the ability to define functions. Here are some examples:
-- repeat/until loop
local i = 1
repeat
print(i)
i = i + 1
until i > 10
-- function definition and call
function add(a, b)
return a + b
end
print(add(2, 3)) -- prints 5
Conclusion
These are just a few examples of the basic syntax in Lua, including data types, variables, operators, and control structures. Lua is a flexible and powerful language that can be used for a wide range of applications, from scripting to game development to embedded systems. By learning the basics of Lua, you can start to explore its many possibilities and create your own programs and projects.
Leave a Comment