In this section, we will cover the fundamental syntax and structure of Lua. Understanding these basics is crucial as they form the foundation for writing more complex Lua scripts. We will explore the following topics:

  1. Comments
  2. Variables and Data Types
  3. Basic Operators
  4. Control Structures
  5. Functions

  1. Comments

Comments are used to annotate code and are ignored by the Lua interpreter. They are useful for explaining code and making it more readable.

  • Single-line comments start with --.
  • Multi-line comments are enclosed between --[[ and ]].
-- This is a single-line comment

--[[
This is a
multi-line comment
]]

  1. Variables and Data Types

Variables in Lua are dynamically typed, meaning you don't need to declare their type explicitly. Lua supports several basic data types:

  • nil: Represents the absence of a value.
  • boolean: Can be true or false.
  • number: Represents real (floating-point) numbers.
  • string: Represents sequences of characters.
  • table: A powerful data structure that can be used as arrays, dictionaries, etc.
  • function: Represents executable code.
  • userdata: Represents arbitrary C data.
  • thread: Represents independent threads of execution.

Example:

local myVar = nil       -- nil
local isLuaFun = true   -- boolean
local age = 25          -- number
local name = "Lua"      -- string
local myTable = {}      -- table
local myFunction = function() print("Hello, Lua!") end  -- function

  1. Basic Operators

Lua supports various operators for arithmetic, relational, logical, and other operations.

Arithmetic Operators

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b
^ Exponentiation a ^ b

Relational Operators

Operator Description Example
== Equal to a == b
~= Not equal to a ~= b
> Greater than a > b
< Less than a < b
>= Greater or equal a >= b
<= Less or equal a <= b

Logical Operators

Operator Description Example
and Logical AND a and b
or Logical OR a or b
not Logical NOT not a

Example:

local a = 10
local b = 20

print(a + b)  -- 30
print(a > b)  -- false
print(a == 10 and b == 20)  -- true

  1. Control Structures

Control structures allow you to control the flow of your program. Lua supports several control structures, including if, else, elseif, while, for, and repeat.

If-Else

local x = 10

if x > 0 then
    print("x is positive")
elseif x < 0 then
    print("x is negative")
else
    print("x is zero")
end

While Loop

local i = 1

while i <= 5 do
    print(i)
    i = i + 1
end

For Loop

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

Repeat-Until Loop

local i = 1

repeat
    print(i)
    i = i + 1
until i > 5

  1. Functions

Functions are first-class values in Lua, meaning they can be stored in variables, passed as arguments, and returned from other functions.

Defining a Function

function greet(name)
    return "Hello, " .. name
end

print(greet("Lua"))  -- Output: Hello, Lua

Anonymous Functions

local greet = function(name)
    return "Hello, " .. name
end

print(greet("Lua"))  -- Output: Hello, Lua

Practical Exercise

Exercise 1: Basic Arithmetic

Write a Lua script that takes two numbers as input and prints their sum, difference, product, and quotient.

Solution:

local a = 15
local b = 5

print("Sum: " .. (a + b))         -- Output: Sum: 20
print("Difference: " .. (a - b))  -- Output: Difference: 10
print("Product: " .. (a * b))     -- Output: Product: 75
print("Quotient: " .. (a / b))    -- Output: Quotient: 3

Exercise 2: Control Structures

Write a Lua script that prints all even numbers from 1 to 10 using a for loop.

Solution:

for i = 1, 10 do
    if i % 2 == 0 then
        print(i)  -- Output: 2, 4, 6, 8, 10
    end
end

Conclusion

In this section, we covered the basic syntax and structure of Lua, including comments, variables, data types, operators, control structures, and functions. These fundamentals are essential for writing any Lua script. In the next module, we will delve deeper into basic concepts such as variables, data types, operators, control structures, loops, and functions.

© Copyright 2024. All rights reserved