第二章:Lua基础语法
●变量和数据类型
●运算符和表达式
●条件语句和循环语句

变量和数据类型
在Lua中,变量用于存储数据,并且不需要事先声明变量的类型。
以下是一些常见的数据类型:

字符串(string):用于表示文本数据,可以使用单引号或双引号括起来。
数字(number):用于表示数值,包括整数和浮点数。
布尔值(boolean):用于表示真(true)或假(false)。
空值(nil):表示一个无效或未定义的值。
表(table):用于表示复杂的数据结构

以下是一些示例代码,演示如何声明和使用变量:

-- 字符串
local name = "Alice"
print("Hello, " .. name)  -- 输出:Hello, Alice

-- 数字
local age = 25
local height = 1.75
print("Age: " .. age .. ", Height: " .. height)  
-- 输出:Age: 25, Height: 1.75

-- 布尔值
local isStudent = true
if isStudent then
    print("You are a student.")
else
    print("You are not a student.")
end

-- 空值
local x = nil
print(x)  -- 输出:nil
作者:admin  创建时间:2023-11-09 22:09
最后编辑:xiazm  更新时间:2025-03-16 12:21