## Contents
1. [Variables](#variables)
2. [Data Types](#data-types)
3. [Booleans](#booleans)
4. [Control Structures](#control-structures)
5. [Functions](#functions)
6. [Strings](#strings)
7. [Numbers](#numbers)
8. [Tables](#tables)
# Lua Basics
## Variables
Variables are used to store data. They are declared with the keyword `local` and can be assigned a value with the `=` operator.
```lua
local myVariable = 5
```
You can use the `=` operator to assign values to variables:
```lua
local x = 5
local y = 10
local z = x + y
```
## Data Types
Lua has 8 data types:
* `nil` - A value that represents nothing or no value
* `boolean` - A value that is either true or false
* `number` - A value that is a number
* `string` - A value that is a string of characters
* `function` - A value that is a function
* `userdata` - A value that is a userdata
* `thread` - A value that is a thread
* `table` - A value that is a table
Here are some examples of each data type and how to use them:
```lua
local number = 10 -- A number
local string = "Hello World" -- A string
local boolean = true -- A boolean
local table = {1, 2, 3} -- A table
local function = function() end -- A function
local thread = coroutine.create(function() end) -- A thread
local nil = nil -- A nil value
```
## Booleans
Booleans are used to represent true or false values. They are declared by using the `true` or `false` keywords:
```lua
local myBoolean = true
```
You can use the `and` operator to check if two conditions are true:
```lua
local x = 5
local y = 10
local z = 15
if x < y and y < z then
print("x is less than y and y is less than z")
end
```
You can use the `or` operator to check if one of two conditions is true:
```lua
local x = 5
local y = 10
local z = 15
if x < y or y < z then
print("x is less than y or y is less than z")
end
```
You can use the `not` operator to check if a condition is false:
```lua
local x = 5
local y = 10
if not x < y then
print("x is not less than y")
end
```
They all output a true or false value. If this is confusing, you can read more about booleans [here](https://www.lua.org/pil/2.2).
## Control Structures
Control structures are used to control the flow of your code. Lua has 3 control structures:
* `if` - An if statement
* `while` - A while loop
* `for` - A for loop
### If Statements
If statements are used to check if a condition is true or false. If the condition is true, the code inside the if statement will be executed. If the condition is false, the code inside the if statement will not be executed.
```lua
local x = 5
local y = 10
if x > y then
print("x is greater than y")
end
```
### While Loops
While loops are used to repeat a block of code while a condition is true.
```lua
local x = 0
while x < 10 do
print(x)
x = x + 1
end
```
### For Loops
For loops are used to repeat a block of code a certain number of times.
```lua
for i = 1, 10 do
print(i)
end
```
### If Statements
If statements are used to execute code if a condition is true. They are declared by using the `if` keyword followed by a condition and a block of code:
```lua
if condition then
-- Code to execute if condition is true
end
```
You can use the `else` keyword to execute code if a condition is false:
```lua
if condition then
-- Code to execute if condition is true
else
-- Code to execute if condition is false
end
```
You can use the `elseif` keyword to check multiple conditions:
```lua
if condition1 then
-- Code to execute if condition1 is true
elseif condition2 then
-- Code to execute if condition1 is false and condition2 is true
else
-- Code to execute if condition1 and condition2 are false
end
```
## Functions
### Built-in Functions
To create a custom function in Lua, you can use the function keyword followed by the function's name and a list of parameters in parentheses, like this:
```lua
function myFunction(parameter1, parameter2)
-- Function body
end
```
The function body is a block of code that defines what the function does. You can use any valid Lua code inside the function body, including control structures, variables, and other functions.
Here's an example of a simple function that adds two numbers and returns the result:
### User-defined Functions
```lua
function add(x, y)
return x + y
end
local result = add(5, 10)
print(result) -- 15
```
You can define functions using the function literal syntax, which allows you to create anonymous functions that you can assign to variables or pass as arguments to other functions. Here's an example:
```lua
local add = function(x, y)
return x + y
end
local result = add(5, 10)
print(result) -- 15
```
## Strings
Strings are a sequence of characters. They are declared by enclosing the characters in double quotes.
```lua
local myString = "Hello World"
```
You can use the `..` operator to concatenate strings:
```lua
local myString = "Hello" .. " " .. "World"
```
You can use the `#` operator to get the length of a string:
```lua
local myString = "Hello World"
local length = #myString
print(length) -- 11
```
```lua
local myString = "Hello World"
local length = #myString
print(length) -- 11
```
You can access individual characters in a string using the `[]` operator:
```lua
local myString = "Hello"
print (myString[1]) -- H
print (myString[2]) -- e
print (myString[3]) -- l
print (myString[4]) -- l
print (myString[5]) -- o
```
## Numbers
### Declaring Numbers
Numbers are declared by simply assigning a number to a variable:
```lua
local myNumber = 5
```
### Mathematical Operations
You can use the `+` operator to add numbers:
```lua
local result = 5 + 10
print(result) -- 15
```
You can use the `-` operator to subtract numbers:
```lua
local result = 10 - 5
print(result) -- 5
```
You can use the `*` operator to multiply numbers:
```lua
local result = 5 * 10
print(result) -- 50
```
You can use the `/` operator to divide numbers:
```lua
local result = 10 / 5
print(result) -- 2
```
### Type Conversion
If you need to convert a value to an integer, you can use the math.floor function:
```lua
local x = 10.5
local y = math.floor(x) -- 10
```
You can also use the `tonumber` function to convert a string into an integer, as long as the string contains a valid integer value:
```lua
local x = "10"
local y = tonumber(x) -- 10
```
### Extras
You can use scientific notation to represent large numbers:
```lua
local x = 1e6 -- 1,000,000
local y = 1e-6 -- 0.000001
```
You can use the `math.pi` constant to represent pi:
```lua
local x = math.pi -- 3.1415926535898
```
You can use the `math.huge` constant to represent infinity:
```lua
local x = math.huge -- Infinity
```
## Tables
Tables are used to store data in key-value pairs. They are declared by using the `{}` operator:
```lua
local myTable = {}
```
You can add values to a table by using the `[]` operator:
```lua
local myTable = {}
myTable["key"] = "value"
```
You can access values in a table by using the `[]` operator:
```lua
local myTable = {}
myTable["key"] = "value"
local value = myTable["key"]
```
You can also use the `.` operator to access values in a table:
```lua
local myTable = {}
myTable.key = "value"
local value = myTable.key
```
```lua
local myTable = {}
myTable.key = "value"
local value = myTable.key
```
You can use the `#` operator to get the length of a table:
```lua
local myTable = {1, 2, 3}
local length = #myTable
print(length) -- 3
```
You can use the `pairs` function to iterate over the key-value pairs in a table:
```lua
local myTable = {1, 2, 3}
for key, value in pairs(myTable) do
print(key, value)
end
```