【CS.PL】Lua 编程之道: 基础语法和数据类型 - 进度16%

2 初级阶段 —— 基础语法和数据类型


在这里插入图片描述

2.0 关键字(keywords) 🔥

The following keywords are reserved and cannot be used as names:

andbreakdoelseelseifendfalse
forfunctionifinlocalnilnot
orrepeatreturnthentrueuntilwhile

Lua 与 C++对比:

  • do ... end 相当于 { ... },用于代码块的定义。
  • 除非声明为 local,否则变量不以 do ... end 区分生命周期,默认都是全局变量。
  • 只有 nil 和布尔类型的 false 会使条件表达式为假,0"" 都会判为真。
  • 语法糖:
    • lhs and rhs:如果 lhs 为真,返回 rhs;如果 lhs 为假,返回 lhs
    • lhs or rhs:如果 lhs 为真,返回 lhs;如果 lhs 为假,返回 rhs

2.1 注释与标识符

2.1.1 注释

在 Lua 中,注释用于对代码进行解释说明,而不会被实际执行。Lua 支持两种注释形式:

  • 单行注释:使用两个连字符 -- 开头,后面跟着注释内容。
  • 多行注释:使用两个连续的方括号 --[[ 开头,以及两个连续的方括号 --]] 结尾,之间是注释内容。
-- 这是单行注释

--[[
这是
多行
注释
--]]

2.1.2 标识符

在 Lua 中,标识符用于命名变量、函数和其他数据。标识符的命名规则如下:

  • 以字母(大小写均可)或下划线 _ 开头,后面可以跟字母、数字和下划线。
  • Lua 是区分大小写的,因此 myVarmyvar 是不同的标识符。

2.2 变量与赋值

2.2.1 所有变量默认是全局变量 ≠ local, 有一个例外

print(daokeke) -- nil
do  
    daokeke= "myself" -- 所有变量默认是全局变量  
end  
print(daokeke) -- myself

except:

for i = 1, 5 do -- 这个i为局部变量  
    print(i)  
end  
print(i) -- nil

2.2.2 local变量是局部变量, 以end作为边界

print(daokeke) -- nil
do  
    local daokeke= "myself" -- local变量是局部变量
end  
print(daokeke) -- nil

2.2.3 多个变量同时赋值且变量没有类型, 只是一个引用

local x, y = 10, 20
x, y = y, x -- 先取值, 再赋值, 效率不高, 但是写法简单, 不需手动引入第三个变量
print(x, y) -- 20      10

x = "a"  
y = "b"  
print(x, y) -- a   b

a = 1,2,3  
print(a)  -- 1
a,b = 1,2,3  
print(a, b)  -- 1     2
a,b,c = 1,2,3  
print(a, b, c) -- 1    2    3

2.2.4 用全局变量给局部变量赋值

print(daokeke) -- nil
daokeke= "good!"  
do  
    local daokeke= daokeke-- 用全局变量给局部变量赋值 
    daokeke= "boy!"  
    print(daokeke) -- boy!
end  
print(daokeke) -- good!

2.3 数据类型:数字、字符串、布尔值

2.3.1 数值类型

不区分整形(32bit)和浮点型, 认为全部都是浮点型即可(64bit); 可以完全当成计算器来用

print(type(123)) -- number
print(type(3.14)) -- number
print(type(2.99e10)) -- number

print(1/2) -- 0.5
print(2^0.5) -- 1.4142135623731
print(12345 == 1.2345e4) -- true

2.3.2 字符串 —— #获取长度/..连接符 🔥

单引号/双引号都可以, 根据内容自由选择

Ref. [[1000.03.CS.PL-Lua-Lua字符串的实现及其编码实践]]

print("I am so cool!")  
print("\"I am so cool!\"she said") -- "I am so cool!"she said  
print('"I am so cool!"she said') -- "I am so cool!"she said

-- [[ 后跟换行, 会保留到内容中  
ctx = [[  
"I'm so cool!"she said]]  
print(ctx) -- "I'm so cool!"she said

-- 可以自定义界定符, 任意数量=号都可以, 比如包含65535(高版本更多)以内的html  
ctx = [===["I'm so cool!"she said]===]  
print(ctx) -- "I'm so cool!"she said 
print(#ctx) -- 22
print(ctx.." with smiles.") -- "I'm so cool!"she said with smiles.

2.3.3 只有nil和boolean的false会使条件表达式为假, 0或""都会判为真

-- 只有`nil`和boolean type中的`false`会使条件表达式为假, 0或""都会判为真  
print(type(nil)) -- nil 
print(type(true)) -- boolean

2.4 运算符和表达式

2.4.0 算数运算符

Lua 支持常见的算术运算符,如 +-*/ 等。

  • +:加法
  • -:减法
  • *:乘法
  • /:除法
  • %:取模(求余数)
  • ^:幂运算
print(5 + 3)    -- 8
print(5 - 3)    -- 2
print(5 * 3)    -- 15
print(5 / 3)    -- 1.6666666666667
print(5 % 3)    -- 2
print(5 ^ 3)    -- 125

2.4.1 逻辑运算符and/or/not

-- 语法糖: lhs and rhs ==> 如果lhs为真, 返回rhs; 如果lhs为假, 返回lhs
print(0 and 123) -- 123  
print(nil and 123) -- nil  
print(false and 123) -- false  
  
-- 语法糖: lhs or rhs ==> 如果lhs为真, 返回lhs; 如果lhs为假, 返回rhs
print(0 or 123) -- 0  
print(nil or 123) -- 123  
print(false or 123) -- 123

print(false or nil) -- nil  
print(not nil) -- true  
print(not 0) -- false

2.4.2 比较运算符 == / ~= / > / < / >= / <=

比较:

print(5 == 5)    -- true
print(5 ~= 5)    -- false
print(5 > 3)     -- true
print(5 < 3)     -- false
print(5 >= 5)    -- true
print(5 <= 5)    -- true

2.4.2 特殊的表达式

初始化:

-- x = x1 or {xx} x1存在, x为x1;否则为{}
a = a1 or 0
print(a)  -- 0

三目运算符A?B:C:

-- 已知B为真, A真 -> A and B -> B, B or C -> B; A假 -> A and B -> A, B or C -> C

a = true
b = "yes"
c = "no"
result = a and b or c
print(result)  -- "yes"

a = false
result = a and b or c
print(result)  -- "no"

这样,我们在 Lua 中通过一些技巧也能实现类似其他语言的三目运算符功能。这种表达方式非常简洁,但在使用时要确保 B 永远为真,否则会返回意想不到的结果。

往期回顾

【CS.PL】Lua 编程之道: 简介与环境设置 - 进度8%

  • 36
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值