当前位置:网站首页>04 Lua operator

04 Lua operator

2022-04-23 16:21:00 Javalib of tangerine peel

Operator

Assignment operator

Assignment operator = One or more values can be assigned to one or more variables .

name = "chenpi"
age = 18
a, b = 10, 20

print(name)  -- chenpi
print(age)   -- 18
print(a)     -- 10
print(b)     -- 20

You can also simply interact with the values of two variables , as follows

a, b = 10, 20
print(a)     -- 10
print(b)     -- 20

a, b = b, a
print(a)     -- 20
print(b)     -- 10

Arithmetic operator

  • Add :+
  • Subtraction :-
  • Multiplication :*
  • division :/
  • modulus :%
  • Take the negative :-
  • Exponentiation :^
print(10 + 20)  -- 30
print(20 - 10)  -- 10
print(2 * 5)    -- 10
print(15 / 2)   -- 7.5
print(15 % 2)   -- 1
print(-20)      -- -20
print(2^5)      -- 32

Comparison operator

  • Greater than :>
  • Greater than or equal to :>=
  • Less than :<
  • Less than or equal to :<=
  • be equal to :==
  • It's not equal to :~=
print(10 > 20)   -- false
print(20 >= 10)  -- true
print(2 < 5)     -- true
print(15 <= 2)   -- false
print(15 == 12)  -- false
print(12 ~= 13)  -- true

Relational operator

  • also :and
  • perhaps :or
  • Not :not

about and Operator , If the first expression is true , And the operation result of the second expression is not a Boolean value , This value will be output .

print(10 > 5 and 2 ~= 3)    -- true
print(5 > 10 and 5)         -- false
print(5 < 10 and 5)         -- 5
print(5 < 10 and nil)       -- nil
print(5 < 10 and "chenpi")  -- chenpi

about or Operator , If the first expression is false , And the operation result of the second expression is not a Boolean value , This value will be output .

print(10 > 5 or 2 ~= 2)  -- true
print(5 > 10 or 10 > 2)  -- true
print(5 < 2 or 5)        -- 5
print(5 < 2 or nil)      -- nil
print(5 < 2 or "chenpi") -- chenpi

not Take the , That is, the Boolean value is reversed , stay Lua in , Only false and nil representative false, Other values represent true .

print(not 5)                   -- false
print(not 0)                   -- false
print(not "chenpi")            -- false
print(not nil)                 -- true
print(not true)                -- false
print(not false)               -- true
print(not (5 > 4 and 5 == 3))  -- true
print(not type)                -- false
print(not print)               -- false

Connector

Lua Use the symbol... For the connection of strings in .., Not like other languages +, Plus at Lua Represents the operation .

name = "Hello" .. " " .. "ChenPi"
print(name)  -- Hello ChenPi

print("Lua " .. name .. "!") -- Lua Hello ChenPi!

age = 18
print(age .. " years old")  -- 18 years old

b = true
print("boolean:" .. b)  --  Can't connect boolean value , Will report a mistake  attempt to concatenate global 'b' (a boolean value)

print(address .. " area")  --  Cannot connect to an undefined variable , Will report a mistake  attempt to concatenate global 'address' (a nil value)

print(nil .. " area")  --  Can't connect to one nil value , Will report a mistake  attempt to concatenate a nil value

print(type .. " function")  --  Cannot connect a function , Will report a mistake  attempt to concatenate global 'type' (a function value)


print("15" + 20)  -- 35 ,  Use + When , Will convert a numeric string into a number for operation 

print("a" + 10)   -- attempt to perform arithmetic on a string value

Find the length operator #

# The pound sign can find the length of the string , You can also find the number of elements in the table .

t = {
    1, 2, "a", true}

print(#t)         -- 4
print(#t[3])      -- 1

print(#"chenpi")  -- 6

--  Traverse all elements in the table 
for i = 1, #t do
    print(t[i])
end

With the help of well number , We can add to the array ( If all keys in the table are numbers, they can be regarded as numbers ) Add elements to the end .

t = {
    "a", "b", "c"}
t[#t + 1] = "d"
t[#t + 1] = "e"

for i = 1, #t do
    print(t[i])
end

--  Output  a b e d e

Bitwise operators

print(1 << 3)    -- 8  Bit shift left 
print(8 >> 2)    -- 2  Shift right 
print(2 | 1)     -- 3  Bit exclusive or 
print(4 & 5)     -- 4  Bit and 
print(~2)        -- -3  Bit inversion 

This sharing is over ~~

I am a Dried tangerine or orange peel , One on the Internet Coding Of ITer. If you think the article will help you , give the thumbs-up 、 Collection 、 Focus on 、 Comment on , Your support is the biggest motivation for my creation !

版权声明
本文为[Javalib of tangerine peel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231613002144.html