[Lua syntax] Arithmetic, conditional, logical, bitwise, ternary operators

1. Arithmetic operators

Remainder of addition, subtraction, multiplication and division: + - * / %
Unique in Lua: Power operation^

Note:
1. There are no increments and decrements (++, –) in Lua, nor compound operators (+=, -=).
2. Strings in Lua can be operated by arithmetic operators and will be automatically converted into numbers,
such as: "10.3" + 1 results in 11.3

a = 2
b = 3
print("加法运算:" .. a + b)
print("减法运算:" .. a - b)
print("乘法运算:" .. a * b)
print("除法运算:" .. a / b)
print("取余运算:" .. a % b)
print("幂运算:" .. a ^ b) --a的b次方

2. Conditional operator

Greater than>
Less than<
Greater than or equal>=
Less than or equal to<=
Equal to==
Not equal to~=

The return value is still bool

a = 2
b = 3
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
print(a == b)
print(a ~= b)

3. Logical operators

Note:
1. The symbols are different from those of C#
2. They have the same "short circuit" characteristics as C#

If it is true with and, then it is true,
or if it is the same with or, it is true,
if it is not, it is negated.

print(true and false)
print(true or false)
print(not true)

4. Bit operators

! Lua does not support bit operations,
you need to implement them yourself

5.Ternary operator

! The ternary operator is not supported in Lua

Guess you like

Origin blog.csdn.net/SEA_0825/article/details/132366179