lua-Operators(运算符)

Arthmetic Operators

Similiar to other programing languages, there are also + - * / % in Lua. On top of that, lua also has ^ (exponent) operator.

1
2
3
4
5
local a = 2
print(a ^ 1) -- 2.0
print(a ^ 2) -- 4.0
print(a ^ 3) -- 8.0
print(a ^ 4) -- 16.0

Relational Operators

This section is the same to other programing language.

Note: unequal operator is ~=

Logical Operators

This section has a great distinction from others.

NOTE:

Only nil or false will be recognized as false.

and

Similiar to other language &&

or

Similiar to other language ||

not

Similiar to other language !

Misc Operators

  1. .. concatenation

    Concatenates two strings.

    1
    2
    3
    local a, b = "hello", 'lua'
    local c = a .. "-" .. b
    print(c) -- hello-lua
  2. # length

    An unary operator that return the length of the a string or a table.

    1
    2
    3
    4
    5
    6
    7
    local a = "hello"
    local b = #a
    print(b) -- 5

    local t = {"hello","lua","python"}
    local c = #t
    print(c) -- 3

lua-Operators(运算符)
https://jackiedai.github.io/2024/01/03/004lua/lua-Operators-运算符/
Author
lingXiao
Posted on
January 3, 2024
Licensed under